├── PageObjectExampleTest.tsv ├── PageObjectExampleTest.robot ├── PageObjectExampleTest.txt ├── GoogleSearchPage.tsv ├── GoogleSearchPage.robot ├── GoogleSearchPage.txt ├── PageObjectExampleTest.html ├── README.md └── GoogleSearchPage.html /PageObjectExampleTest.tsv: -------------------------------------------------------------------------------- 1 | *Setting* 2 | Test Setup GoogleSearchPage.Open ${URL} 3 | Test Teardown GoogleSearchPage.Close 4 | Resource GoogleSearchPage.html 5 | 6 | *Variable* *Value* 7 | ${BROWSER_OPENED} ${false} 8 | ${BROWSER} firefox 9 | ${URL} http://www.google.com 10 | ${SEARCH_TERM} selenium 11 | 12 | *Test Case* 13 | When The User Searches For Selenium The Results Page Title Should Contain Selenium GoogleSearchPage.Search For ${SEARCH_TERM} 14 | ${result} = GoogleSearchPage.Get Title 15 | Should Contain ${result} ${SEARCH_TERM} 16 | -------------------------------------------------------------------------------- /PageObjectExampleTest.robot: -------------------------------------------------------------------------------- 1 | *** Setting *** 2 | Test Setup GoogleSearchPage.Open ${URL} 3 | Test Teardown GoogleSearchPage.Close 4 | Resource GoogleSearchPage.html 5 | 6 | *** Variable *** Value 7 | ${BROWSER_OPENED} ${false} 8 | ${BROWSER} firefox 9 | ${URL} http://www.google.com 10 | ${SEARCH_TERM} selenium 11 | 12 | *** Test Case *** 13 | When The User Searches For Selenium The Results Page Title Should Contain Selenium 14 | GoogleSearchPage.Search For ${SEARCH_TERM} 15 | ${result} = GoogleSearchPage.Get Title 16 | Should Contain ${result} ${SEARCH_TERM} 17 | -------------------------------------------------------------------------------- /PageObjectExampleTest.txt: -------------------------------------------------------------------------------- 1 | | *** Setting *** | 2 | | Test Setup | GoogleSearchPage.Open | ${URL} | 3 | | Test Teardown | GoogleSearchPage.Close | 4 | | Resource | GoogleSearchPage.html | 5 | 6 | | *** Variable *** | Value | 7 | | ${BROWSER_OPENED} | ${false} | 8 | | ${BROWSER} | firefox | 9 | | ${URL} | http://www.google.com | 10 | | ${SEARCH_TERM} | selenium | 11 | 12 | | *** Test Case *** | 13 | | When The User Searches For Selenium The Results Page Title Should Contain Selenium | 14 | | | GoogleSearchPage.Search For | ${SEARCH_TERM} | 15 | | | ${result} = | GoogleSearchPage.Get Title | 16 | | | Should Contain | ${result} | ${SEARCH_TERM} | 17 | -------------------------------------------------------------------------------- /GoogleSearchPage.tsv: -------------------------------------------------------------------------------- 1 | *Setting* *Value* 2 | Documentation Page Object as Resource File Example for Robot Framework 3 | Library Selenium2Library 4 | 5 | *Variable* *Value* 6 | ${SEARCH_FIELD} name=q 7 | ${SEARCH_BUTTON} name=btnG 8 | 9 | *Keyword* 10 | Click On Search [Documentation] Click the search button to proceed with the search 11 | Selenium2Library.Click Button ${SEARCH_BUTTON} 12 | 13 | Close [Documentation] Close the browser window 14 | Selenium2Library.Close Browser # or is it Selenium2Library.Close Window? 15 | 16 | Get Title [Documentation] Get the page title for Google search page 17 | ${result} = Selenium2Library.Get Title 18 | [Return] ${result} 19 | 20 | Open [Arguments] ${url} 21 | [Documentation] Open (or go to) the Google search page 22 | Run Keyword And Return If ${BROWSER_OPENED} == ${true} Selenium2Library.Go To ${url} 23 | Selenium2Library.Open Browser ${url} ${BROWSER} #other optional args 24 | ${BROWSER_OPENED} = Set Variable ${true} 25 | 26 | Search For [Arguments] ${searchTerm} 27 | [Documentation] Perform a search on Google (enter search text, then click search) 28 | GoogleSearchPage.Type Search Term ${searchTerm} 29 | Sleep 1 seconds Arbitrary delay for page load and action to take effect correctly in automation 30 | GoogleSearchPage.Click On Search 31 | 32 | Type Search Term [Arguments] ${searchTerm} 33 | [Documentation] Enter search term into Google search field. This doesn't click the search button as a follow up. 34 | Selenium2Library.Input Text ${SEARCH_FIELD} ${searchTerm} 35 | -------------------------------------------------------------------------------- /GoogleSearchPage.robot: -------------------------------------------------------------------------------- 1 | *** Setting *** Value 2 | Documentation Page Object as Resource File Example for Robot Framework 3 | Library Selenium2Library 4 | 5 | *** Variable *** Value 6 | ${SEARCH_FIELD} name=q 7 | ${SEARCH_BUTTON} name=btnG 8 | 9 | *** Keyword *** 10 | Click On Search 11 | [Documentation] Click the search button to proceed with the search 12 | Selenium2Library.Click Button ${SEARCH_BUTTON} 13 | 14 | Close 15 | [Documentation] Close the browser window 16 | Selenium2Library.Close Browser # or is it Selenium2Library.Close Window? 17 | 18 | Get Title 19 | [Documentation] Get the page title for Google search page 20 | ${result} = Selenium2Library.Get Title 21 | [Return] ${result} 22 | 23 | Open 24 | [Arguments] ${url} 25 | [Documentation] Open (or go to) the Google search page 26 | Run Keyword And Return If ${BROWSER_OPENED} == ${true} Selenium2Library.Go To ${url} 27 | Selenium2Library.Open Browser ${url} ${BROWSER} #other optional args 28 | ${BROWSER_OPENED} = Set Variable ${true} 29 | 30 | Search For 31 | [Arguments] ${searchTerm} 32 | [Documentation] Perform a search on Google (enter search text, then click search) 33 | GoogleSearchPage.Type Search Term ${searchTerm} 34 | Sleep 1 seconds Arbitrary delay for page load and action to take effect correctly in automation 35 | GoogleSearchPage.Click On Search 36 | 37 | Type Search Term 38 | [Arguments] ${searchTerm} 39 | [Documentation] Enter search term into Google search field. This doesn't click the search button as a follow up. 40 | Selenium2Library.Input Text ${SEARCH_FIELD} ${searchTerm} 41 | -------------------------------------------------------------------------------- /GoogleSearchPage.txt: -------------------------------------------------------------------------------- 1 | | *** Setting *** | Value | 2 | | Documentation | Page Object as Resource File Example for Robot Framework | 3 | | Library | Selenium2Library | 4 | 5 | | *** Variable *** | Value | 6 | | ${SEARCH_FIELD} | name=q | 7 | | ${SEARCH_BUTTON} | name=btnG | 8 | 9 | | *** Keyword *** | 10 | | Click On Search | 11 | | | [Documentation] | Click the search button to proceed with the search | 12 | | | Selenium2Library.Click Button | ${SEARCH_BUTTON} | 13 | 14 | | Close | 15 | | | [Documentation] | Close the browser window | 16 | | | Selenium2Library.Close Browser | # or is it Selenium2Library.Close Window? | 17 | 18 | | Get Title | 19 | | | [Documentation] | Get the page title for Google search page | 20 | | | ${result} = | Selenium2Library.Get Title | 21 | | | [Return] | ${result} | 22 | 23 | | Open | 24 | | | [Arguments] | ${url} | 25 | | | [Documentation] | Open (or go to) the Google search page | 26 | | | Run Keyword And Return If | ${BROWSER_OPENED} == ${true} | Selenium2Library.Go To | ${url} | 27 | | | Selenium2Library.Open Browser | ${url} | ${BROWSER} | #other optional args | 28 | | | ${BROWSER_OPENED} = | Set Variable | ${true} | 29 | 30 | | Search For | 31 | | | [Arguments] | ${searchTerm} | 32 | | | [Documentation] | Perform a search on Google (enter search text, then click search) | 33 | | | GoogleSearchPage.Type Search Term | ${searchTerm} | 34 | | | Sleep | 1 seconds | Arbitrary delay for page load and action to take effect correctly in automation | 35 | | | GoogleSearchPage.Click On Search | 36 | 37 | | Type Search Term | 38 | | | [Arguments] | ${searchTerm} | 39 | | | [Documentation] | Enter search term into Google search field. This doesn't click the search button as a follow up. | 40 | | | Selenium2Library.Input Text | ${SEARCH_FIELD} | ${searchTerm} | 41 | -------------------------------------------------------------------------------- /PageObjectExampleTest.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 48 | PageObjectExampleTest 49 | 50 | 51 |

PageObjectExampleTest - using resource files for page objects

52 |

Mapped from (non-Robot Framework) page object example at http://assertselenium.com/automation-design-practices/page-object-pattern/

53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 |
Setting
Test SetupGoogleSearchPage.Open${URL}
Test TeardownGoogleSearchPage.Close
ResourceGoogleSearchPage.html
86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 |
Variable
${BROWSER_OPENED}${false}
${BROWSER}firefox
${URL}http://www.google.com
${SEARCH_TERM}selenium
126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 |
Test Case
When The User Searches For Selenium The Results Page Title Should Contain SeleniumGoogleSearchPage.Search For${SEARCH_TERM}
${result} =GoogleSearchPage.Get Title
Should Contain${result}${SEARCH_TERM}
152 | 153 | 154 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | robotframework-simple-page-object-example 2 | ========================================= 3 | 4 | A simple example of page objects implemented as resource file for Robot Framework, converted from a [Java test example] (http://assertselenium.com/automation-design-practices/page-object-pattern/). 5 | 6 | **Prerequisites:** 7 | 8 | * some version of Python installed (2.x or 3.x) 9 | * a version of RobotFramework (RF) installed 10 | * a version of RF's Selenium2Library installed (Python or Java version should technically not matter) 11 | * go to www.robotframework.org for installation instructions 12 | 13 | **To run the test:** after getting a copy of the files off this Github repo, simply run like `pybot PageObjectExampleTest.extension`, where extension is your desired file format to work with (.robot, .txt, .tsv, or .html). If using Java version, run with `jybot` instead. 14 | 15 | **The page object example is structured as follows:** 16 | 17 | * PageObjectExampleTest calls keywords from... 18 | * GoogleSearchPage resource file, which in turn calls keywords from... 19 | * Selenium2Library, Python version or Java version 20 | * Both the test and page object resource file(s) call RobotFramework BuiltIn library keywords 21 | 22 | **NOTE:** for better viewing of the HTML format of the test files, go to the [Github.io page](http://daluu.github.io/robotframework-simple-page-object-example/). For the other formats, you can just directly view in this repo. 23 | 24 | **Reasons why I created this example:** 25 | 26 | This may not be the best (simple) example, and there are other examples you should see like: 27 | 28 | http://www.beer30.org/2012/05/26/using-the-page-object-pattern-with-robot-framework/ 29 | 30 | https://blog.codecentric.de/en/2010/07/how-to-structure-a-scalable-and-maintainable-acceptance-test-suite/ 31 | 32 | but I wanted to offer: 33 | 34 | an example with the code/files available to run "as is" after grabbing them from the repo, and more importantly... 35 | 36 | an example that highlights good page object design practices - normally done in code but adapted as resource file(s) for robot framework, e.g. 37 | * locators defined separately as members (variables/properties) of page object, not embedded within the methods that use them 38 | * page objects don't perform assertions/verifications, but may throw exception/failure if in wrong state (no example of throwing failure yet) 39 | * tests do all the assertions & verifications 40 | * we specifically call out what test library (i.e. Selenium2Library) and page object (i.e. resource file) methods (i.e. keywords) we invoke for clarity (though more text/typing) just like we do in code for things like `driver.findElement(By.id("some ID")).click()`, in Java, called by a page object method, etc. to provide a (re)semblance to object-oriented programming often used with page objects in code 41 | * an example showing code/keyword reuse within a page object 42 | 43 | It is still recommended for one to go through the blog posts referenced above to get visual diagrams, more details, and examples of perhaps more complex implementations with multiple page objects. 44 | 45 | I do hope that this example here will be useful to those not sure how to adapt page objects from code to resource files. 46 | 47 | **NOTES:** 48 | 49 | * the files can be organized within folders for a hierarchical structure, but I kept them all in one place for simplicity. e.g. separate folders for page objects and tests, and perhaps separate folders for different page objects and different tests. 50 | * page objects are supposedly to return other page objects when navigating away to another page (object), though one doesn't have to follow the page object patterns 100%, which is a good thing. With the Robot Framework resource file and test library input/output design, where only simple data structures are used, it's likely not feasible to adopt returning "page objects/resources" from another one. Best to just have to know what page object resource files and their keywords to invoke when transitioning between page objects. 51 | 52 | 53 | -------------------------------------------------------------------------------- /GoogleSearchPage.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 51 | GoogleSearchPage 52 | 53 | 54 |

GoogleSearchPage

55 |

Resource file mapping of an equivalent page object model/pattern class in code

56 |

One could expand this to include nesting/layering/inheriting of page objects from a base object to more specific page objects, but this is just a simple example for now.

57 |

Mapped from example at http://assertselenium.com/automation-design-practices/page-object-pattern/

58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 |
Setting
DocumentationPage Object as Resource File Example for Robot Framework
LibrarySelenium2Library
81 |

Page locators, with location strategy identifier, as variables of the resource file "class"

82 |

The page locators could be separated out in separate variable file(s), for those who prefer page object modeling where locators are kept separate from the page object methods. But best to have them together to represent the page object.

83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 |
Variable
${SEARCH_FIELD}name=q
${SEARCH_BUTTON}name=btnG
109 |

Page object methods as user keywords of resource file "class"

110 |

Since there is no "this" or "self" (I think?) referral of keywords or variables within resource file, we shall just append resource file name to local keyword methods in scope just for clarification in case of keyword name conflicts (even though presently not).

111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 |
Keyword
Click On Search[Documentation]Click the search button to proceed with the search
Selenium2Library.Click Button${SEARCH_BUTTON}
Close[Documentation]Close the browser window
Selenium2Library.Close Browser# or is it Selenium2Library.Close Window?
Get Title[Documentation]Get the page title for Google search page
${result} =Selenium2Library.Get Title
[Return]${result}
Open[Arguments]${url}
[Documentation]Open (or go to) the Google search page
Run Keyword And Return If${BROWSER_OPENED} == ${true}Selenium2Library.Go To${url}
Selenium2Library.Open Browser${url}${BROWSER}#other optional args
${BROWSER_OPENED} =Set Variable${true}
Search For[Arguments]${searchTerm}
[Documentation]Perform a search on Google (enter search text, then click search)
GoogleSearchPage.Type Search Term${searchTerm}
Sleep1 secondsArbitrary delay for page load and action to take effect correctly in automation
GoogleSearchPage.Click On Search
Type Search Term[Arguments]${searchTerm}
[Documentation]Enter search term into Google search field. This doesn't click the search button as a follow up.
Selenium2Library.Input Text${SEARCH_FIELD}${searchTerm}
279 | 280 | 281 | --------------------------------------------------------------------------------