Revision: 4660
Author: [email protected]
Date: Thu Mar  3 03:10:47 2011
Log: Edited wiki page TestStructure through web user interface.
http://code.google.com/p/robotframework/source/detail?r=4660

Added:
 /wiki/WhereToAddKeyword.wiki

=======================================
--- /dev/null
+++ /wiki/WhereToAddKeyword.wiki        Thu Mar  3 03:10:47 2011
@@ -0,0 +1,113 @@
+#summary Where should new keywords go
+
+<wiki:toc max_depth="2" />
+
+= Introduction =
+
+This document contains notes on what to take into account when creating a new keyword and trying to decide if ti should be a library keyword, test suite keyword, or a resource file keyword.
+
+= Should my keyword be in this file or library or some shared resource file? =
+
+ * If your keyword has complicated logic -> In a library!
+ * When things seem too complicated in Robot Framework, its best not to force it
+
+<table>
+<tr><th>`handle_steps.txt`</th><th>`handle_steps.py`</th></tr>
+<tr><td>
+{{{
+*** Keywords ***
+Get Step Message     [Arguments]    ${step}
+  ${init result} =  Run Keyword If  ${step}==0
+                  ...  System Initialization
+  ${step result} =  Run Keyword If  ${step}>0
+                  ...  Call System Step  ${step}
+  ${illegal result} Run Keyword If  ${step}<0  Illegal Step
+  ${result} =  Set Variable If  ${step}==0  ${init result}
+               ...              ${step}>0   ${step result}
+               ...              ${illegal result}
+  [Return]    ${result}
+}}}
+</td>
+<td>
+{{{
+def get_step_message_python(step):
+     if (step == 0):
+          return system_initialization()
+     if (step > 0):
+          return call_system_step(step)
+     else:
+          return illegal_step()
+}}}
+</td></tr>
+</table>
+
+ * Remember that you can call Robots keyword libraries from python
+
+{{{
+from robot.libraries.BuiltIn import BuiltIn
+
+def do_something(argument):
+    output = do_something_that_creates_a_lot_of_output(argument)
+    outputdir = BuiltIn().replace_variables('${OUTPUTDIR}')
+    write_output(outputdir)
+}}}
+
+
+ * From Robot 2.5 onwards you can get an active library instance also in keyword
+
+{{{
+from robot.libraries.BuiltIn import BuiltIn
+
+def title_should_start_with(expected):
+    seleniumlib = BuiltIn().get_library_instance('SeleniumLibrary')
+    title = seleniumlib.get_title()
+    if not title.startswith(expected):
+        raise AssertionError("Title '%s' did not start with '%s'"
+                             % (title, expected))
+}}}
+
+ * Choosing between test suite file and a shared resource file depends from the situation.
+
+ * If your keyword could be useful to others its best to move it shared resource. Always better to avoid duplicate work. + * Document your keyword. Especially if you take arguments or return values. + * Make sure that you dont do unnessary sleeps. Always use Wait Until Keyword Succeeds if possible. + * Avoid "Set Test Variable", "Set Suite Variable", and "Set Global Variable" in shared keywords.
+
+<table>
+<tr><th>`Withdraw.txt`</th><th>`Withdraw.txt`</th></tr>
+<tr><td>
+{{{
+*** Test Cases ***
+Withdraw From Account
+  ${status} =  Withdraw From Account   50$
+  Withdraw Should Have Succeeded    ${status}
+
+*** Keywords ***
+Withdraw From Account
+  [arguments]  ${amount}
+  ${status} =  Withdraw From  ${USER}  ${amount}
+  [return]     ${status}
+
+Withdraw Should Have Succeeded
+  [arguments]   ${status}
+  Should Be Equal   ${status}   SUCCESS
+}}}
+</td>
+<td>
+{{{
+*** Test Cases ***
+Withdraw From Account
+  Withdraw From Account    50$
+  Withdraw Should Have Succeeded
+
+*** Keywords ***
+Withdraw From Account
+  [arguments]  ${amount}
+  ${status} =  Withdraw From  ${USER}  ${amount}
+  Set Test Variable   ${status}
+
+Withdraw Should Have Succeeded
+  Should Be Equal   ${status}   SUCCESS
+}}}
+</td></tr>
+</table>

Reply via email to