Skip to content

Commit 89c417e

Browse files
authored
Merge pull request #9 from GLMeece/master
Documentation tweaks; disabled 'trying to go to'
2 parents 03a63d1 + f50e3b1 commit 89c417e

File tree

6 files changed

+257
-238
lines changed

6 files changed

+257
-238
lines changed

PageObjectLibrary/__init__.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,27 @@ class PageObjectLibrary(PageObjectLibraryKeywords):
1414
[http://robotframework.org/Selenium2Library/doc/Selenium2Library.html|Selenium2Library].
1515
This library does not replace Selenium2Library; rather, it
1616
provides a framework around which to use Selenium2Library and the
17-
lower-level [http://selenium-python.readthedocs.org/|python
18-
bindings to selenium]
17+
lower-level [http://selenium-python.readthedocs.org/|Python
18+
bindings to Selenium]
1919
2020
This library provides the following keywords:
2121
2222
| =Keyword Name= | =Synopsis= |
23-
| Go to page | goes to the given page in the browser |
24-
| The current page should be | assert that the given page is displayed in the browser |
25-
| Get page name | returns the name of the current page
23+
| Go to page | Goes to the given page in the browser |
24+
| The current page should be | Assert that the given page is displayed in the browser |
25+
| Get page name | Returns the name of the current page |
2626
2727
PageObjectLibrary provides a PageObject class which should be used
2828
as the base class for other page objects. By inheriting from this
2929
class your keywords have access to the following pre-defined
3030
attributes and methods:
3131
3232
| =Attribute/method= | =Description= |
33-
| self.se2lib | A reference to the selenium2library instance |
34-
| self.browser | A reference to the currently open browser |
35-
| self.locator | A wrapper around the _locators dictionary |
36-
| self.logger | A reference to the robot.api.logger instance |
37-
| self._wait_for_page_refresh() | a context manager for doing work that causes a page refresh |
33+
| ``self.se2lib`` | A reference to the Selenium2Library instance |
34+
| ``self.browser`` | A reference to the currently open browser |
35+
| ``self.locator`` | A wrapper around the ``_locators`` dictionary |
36+
| ``self.logger`` | A reference to the ``robot.api.logger`` instance |
37+
| ``self._wait_for_page_refresh()`` | a context manager for doing work that causes a page refresh |
3838
3939
= Using Selenium2Library Keywords =
4040
@@ -45,11 +45,11 @@ class your keywords have access to the following pre-defined
4545
4646
| self.se2lib.capture_page_screenshot()
4747
48-
= Using Selenium methods =
48+
= Using Selenium Methods =
4949
50-
The attribute ``self.browser`` is a reference to a selenium
50+
The attribute ``self.browser`` is a reference to a Selenium
5151
webdriver object. With this reference you can call any of the
52-
standard selenium methods provided by the selenium library. The
52+
standard Selenium methods provided by the Selenium library. The
5353
following example shows how to find all link elements on a page:
5454
5555
| elements = self.browser,find_elements_by_tag_name("a")
@@ -60,9 +60,9 @@ class your keywords have access to the following pre-defined
6060
the class should define the following attributes:
6161
6262
| =Attribute= | =Description= |
63-
| PAGE_URL | The path to the current page, without the \
64-
hostname and port (eg: /dashboard.html) |
65-
| PAGE_TITLE | The web page title. This is used by the \
63+
| ``PAGE_URL`` | The path to the current page, without the \
64+
hostname and port (eg: ``/dashboard.html``) |
65+
| ``PAGE_TITLE`` | The web page title. This is used by the \
6666
default implementation of ``_is_current_page``. |
6767
6868
When using the keywords `Go To Page` or `The Current Page Should Be`, the
@@ -71,12 +71,12 @@ class your keywords have access to the following pre-defined
7171
of the page. If you are working on a site where the page titles are not unique,
7272
you can override this method to do any type of logic you need.
7373
74-
= Page Objects are normal robot libraries =
74+
= Page Objects are Normal Robot Libraries =
7575
7676
All rules that apply to keyword libraries applies to page objects. For
77-
example, the libraries must be on PYTHONPATH. You may also want to define
77+
example, the libraries must be on ``PYTHONPATH``. You may also want to define
7878
``ROBOT_LIBRARY_SCOPE``. Also, the filename and the classname must be identical (minus
79-
the .py suffix on the file).
79+
the ``.py`` suffix on the file).
8080
8181
= Locators =
8282
@@ -88,9 +88,9 @@ class your keywords have access to the following pre-defined
8888
the locators via dot notation within your keywords as ``self.locator.<name>``. The
8989
``_locators`` dictionary may have nested dictionaries.
9090
91-
= Waiting for a page to be ready =
91+
= Waiting for a Page to be Ready =
9292
93-
One difficulty with writing selenium tests is knowing when a page has refreshed.
93+
One difficulty with writing Selenium tests is knowing when a page has refreshed.
9494
PageObject provides a context manager named ``_wait_for_page_refresh()`` which can
9595
be used to wrap a command that should result in a page refresh. It will get a
9696
reference to the DOM, run the body of the context manager, and then wait for the
@@ -120,27 +120,26 @@ class your keywords have access to the following pre-defined
120120
| with self._wait_for_page_refresh():
121121
| self.click_the_submit_button()
122122
123-
= Using the page object in a test =
123+
= Using the Page Object in a Test =
124124
125125
To use the above page object in a test, you must make sure that
126-
robot can import it, just like with any other keyword
126+
Robot can import it, just like with any other keyword
127127
library. When you use the keyword `Go to page`, the keyword will
128128
automatically load the keyword library and put it at the front of
129-
the robot framework library search order (see
130-
[http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set
131-
Library Search Order|Set library search order])
129+
the Robot Framework library search order (see
130+
[http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Library%20Search%20Order|Set Library Search Order])
132131
133132
In the following example it is assumed there is a second page
134133
object named ``DashboardPage`` which the browser is expected to go to
135134
if login is successful.
136135
137-
| *** Settings ***
138-
| Library PageObjectLibrary
139-
| Library Selenium2Library
140-
| Suite Setup Open browser http://www.example.com
136+
| ``*** Settings ***``
137+
| Library PageObjectLibrary
138+
| Library Selenium2Library
139+
| Suite Setup Open browser http://www.example.com
141140
| Suite Teardown Close all browsers
142141
|
143-
| *** Test Cases ***
142+
| ``*** Test Cases ***``
144143
| Log in to the application
145144
| Go to page LoginPage
146145
| Log in as a normal user

PageObjectLibrary/keywords.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ def se2lib(self):
4141
def the_current_page_should_be(self, page_name):
4242
"""Fails if the name of the current page is not the given page name
4343
44-
page_name is the name you would use to import the page
44+
``page_name`` is the name you would use to import the page.
4545
4646
This keyword will import the given page object, put it at the
47-
front of the robot library search order, then call the method
47+
front of the Robot library search order, then call the method
4848
``_is_current_page`` on the library. The default
4949
implementation of this method will compare the page title to
5050
the ``PAGE_TITLE`` attribute of the page object, but this
@@ -70,29 +70,29 @@ def the_current_page_should_be(self, page_name):
7070
raise Exception("Expected page to be %s but it was not" % page_name)
7171

7272
def go_to_page(self, page_name, page_root = None):
73-
"""Go to the url for the given page object
73+
"""Go to the url for the given page object.
7474
7575
Unless explicitly provided, the URL root will be based on the
7676
root of the current page. For example, if the current page is
7777
http://www.example.com:8080 and the page object URL is
78-
/login, the url will be http://www.example.com:8080/login
78+
``/login``, the url will be http://www.example.com:8080/login
7979
80-
Example:
80+
== Example ==
8181
82-
Given a page object named `ExampleLoginPage` with the URL
83-
`/login`, and a browser open to `http://www.example.com`, the
84-
following statement will go to `http://www.example.com/login`,
85-
and place `ExampleLoginPage` at the front of robot's library
82+
Given a page object named ``ExampleLoginPage`` with the URL
83+
``/login``, and a browser open to ``http://www.example.com``, the
84+
following statement will go to ``http://www.example.com/login``,
85+
and place ``ExampleLoginPage`` at the front of Robot's library
8686
search order.
8787
88-
| go to page ExampleLoginPage
88+
| Go to Page ExampleLoginPage
8989
9090
The effect is the same as if you had called the following three
91-
keywords
91+
keywords:
9292
93-
| Selenium2Library.go to http://www.example.com/login
93+
| Selenium2Library.Go To http://www.example.com/login
9494
| Import Library ExampleLoginPage
95-
| Set library search order ExampleLoginPage
95+
| Set Library Search Order ExampleLoginPage
9696
9797
Tags: selenium, page-object
9898
@@ -105,7 +105,7 @@ def go_to_page(self, page_name, page_root = None):
105105
url = "%s://%s%s" % (scheme, netloc, page.PAGE_URL)
106106

107107
with page._wait_for_page_refresh():
108-
self.logger.console("\ntrying to go to '%s'" % url)
108+
# self.logger.console("\ntrying to go to '%s'" % url) # <-- Remove hashmark if you want this message on console
109109
self.se2lib.go_to(url)
110110
# should I be calling this keyword? Should this keyword return
111111
# true/false, or should it throw an exception?

PageObjectLibrary/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "1.0.0-beta.1"
1+
__version__ = "1.0.0-beta.2"
22

README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# PageObjectLibrary
2+
3+
## Overview
4+
5+
PageObjectLibrary is a lightweight [Robot Framework] keyword library that makes it possible to use the page object pattern when testing web pages with the keyword based approach of robot framework.
6+
7+
## Installing
8+
9+
```bash
10+
pip install --upgrade robotframework-pageobjectlibrary
11+
```
12+
13+
## Source code
14+
15+
The source code is hosted on github at the following url:
16+
17+
* https://github.com/boakley/robotframework-pageobjectlibrary.git
18+
19+
## Running the demo
20+
21+
In the github repository is a small demonstration suite that includes a self-contained webserver and web site.
22+
23+
For the demo to run you must have robotframework 2.9+ and Selenium2Library installed. You must also have cloned the github repository to have access to the demo files.
24+
25+
To run the demo, clone the github repository, cd to the folder that contains this file, and then run the following command: :
26+
27+
```bash
28+
robot -d demo/results demo
29+
```
30+
### A simple tutorial
31+
32+
For a simple tutorial, see <https://github.com/boakley/robotframework-pageobjectlibrary/wiki/Tutorial>
33+
34+
## How it works
35+
36+
The page object library is quite simple. Page object classes are implemented as standard robot keyword libraries, and relies on robot frameworks built-in [Set library search order keyword].
37+
38+
The core concept is that when you use PageObjectLibrary keywords to go to a page or assert you are on a specific page, the keyword will automatically load the library for that page and put it at the front of the library search order, guaranteeing that the page object keywords are available to your test case.
39+
40+
## Why page objects makes writing tests easier
41+
42+
The purpose of the page object pattern is to encapsulate the knowledge of how a web page is constructed into an object. Your test uses the object as an interface to the application, isolating your test cases from the details of the implementation of a page.
43+
44+
With page objects, developers are free to modify web pages as much as they want, and the only thing they need to do to keep existing tests from failing is to update the page object class. Because test cases aren’t directly tied to the implementation, they become more stable and more resistent to change as the website matures.
45+
46+
## A typical test without page objects
47+
48+
With traditional testing using Selenium, a simple login test might look something like the following: (using the pipe-separated format for clarity):
49+
50+
```robotframework
51+
*** Test Cases ***
52+
| Login with valid credentials
53+
| | Go to | ${ROOT}/Login.html
54+
| | Wait for page to contain | id=id_username
55+
| | Input text | id=id_username | ${USERNAME}
56+
| | Input text | id=id_password | ${PASSWORD}
57+
| | Click button | id=id_form_submit
58+
| | Wait for page to contain | Your Dashboard
59+
| | Location should be | ${ROOT}/dashboard.html
60+
```
61+
62+
Notice how this test is tightly coupled to the implementation of the page. It has to know that the input field has an id of “id_username”, and the password field has an id of “id_password”. It also has to know the URL of the page being tested.
63+
64+
Of course, you can put those hard-coded values into variables and import them from a resource file or environment variables, which makes it easier to update tests when locators change. However, there’s still the overhead of additional keywords that are often required to make a test robust, such as waiting for a page to be reloaded. The provided PageObject superclass handles some of those details for you.
65+
66+
## The same test, using page objects
67+
68+
Using page objects, the same test could be written like this:
69+
70+
```robotframework
71+
*** Test Cases ***
72+
| Login with valid credentials
73+
| | Go to page | LoginPage
74+
| | Login as a normal user
75+
| | The current page should be | DashboardPage
76+
```
77+
78+
Notice how there are no URLs or element locators in the test whatsoever, and that we’ve been able to eliminate some keywords that typically are necessary for selenium to work but which aren’t part of the test logic *per se*. What we end up with is test case that is nearly indistinguishable from typical acceptance criteria of an agile story.
79+
80+
## Writing a Page Object class
81+
82+
Page objects are simple python classes that inherit from `PageObjectLibrary.PageObject`. There are only a couple of requirements for the class:
83+
84+
- The class should define a variable named PAGE\_TITLE
85+
- The class should define a variable named PAGE\_URL which is a URI relative to the site root.
86+
87+
By inheriting from `PageObjectLibrary.PageObject`, methods have access to the folloing special object attributes:
88+
89+
- `self.se2lib` - a reference to an instance of Selenium2Library. With this you can call any of the Selenium2Library keywords via their python method names (eg: self.se2lib.input\_text)
90+
- `self.browser` - a reference to the webdriver object created when a browser was opened by Selenium2Library. With this you can bypass Selenium2Library and directly call all of the functions provided by the core selenium library.
91+
- `self.locator` - a wrapper around the `_locators` dictionary of the page. This dictionary can contain all of the locators used by the page object keywords. `self.locators` adds the ability to access the locators with dot notation rather than the slightly more verbose dictionary syntax (eg: `self.locator.username` vs `self._locators["username"]`.
92+
93+
## An example page object
94+
95+
A page object representing a login page might look like this:
96+
97+
```python
98+
from PageObjectLibrary import PageObject
99+
100+
class LoginPage(PageObject):
101+
PAGE_TITLE = "Login - PageObjectLibrary Demo"
102+
PAGE_URL = "/login.html"
103+
104+
_locators = {
105+
"username": "id=id_username",
106+
"password": "id=id_password",
107+
"submit_button": "id=id_submit",
108+
}
109+
110+
def enter_username(self, username):
111+
"""Enter the given string into the username field"""
112+
self.se2lib.input_text(self.locator.username, username)
113+
114+
def enter_password(self,password):
115+
"""Enter the given string into the password field"""
116+
self.se2lib.input_text(self.locator.password, password)
117+
118+
def click_the_submit_button(self):
119+
"""Click the submit button, and wait for the page to reload"""
120+
with self._wait_for_page_refresh():
121+
self.se2lib.click_button(self.locator.submit_button)
122+
```
123+
124+
[robot framework]: http://www.robotframework.org
125+
[Set library search order keyword]: http://robotframework.org/robotframework/latest/libraries/BuiltIn.html#Set%20Library%20Search%20Order

0 commit comments

Comments
 (0)