RobotFramework library that implements the Page Object pattern
Project description
PageObjectLibrary
Overview
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.
Installing
pip install --upgrade robotframework-pageobjectlibrary
Source Code
The source code is hosted on GitHub at the following url:
Running the Demo
In the GitHub repository is a small demonstration suite that includes a self-contained webserver and web site.
For the demo to run, you must have robotframework 2.9+ and robotframework-seleniumlibrary installed. You must also have cloned the GitHub repository to have access to the demo files.
To run the demo, clone the GitHub repository, cd to the folder that contains this file, and then run the following command: :
robot -d demo/results demo
A Simple Tutorial
For a simple tutorial, see https://github.com/boakley/robotframework-pageobjectlibrary/wiki/Tutorial
How it Works
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.
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.
Why Page Objects Makes Writing Tests Easier
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.
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 resistant to change as the website matures.
A Typical Test Without Page Objects
With traditional testing using Selenium, a simple login test might look something like the following: (using the pipe-separated format for clarity):
*** Test Cases ***
| Login with valid credentials
| | Go to | ${ROOT}/Login.html
| | Wait for page to contain | id=id_username
| | Input text | id=id_username | ${USERNAME}
| | Input text | id=id_password | ${PASSWORD}
| | Click button | id=id_form_submit
| | Wait for page to contain | Your Dashboard
| | Location should be | ${ROOT}/dashboard.html
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.
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.
The Same Test, Using Page Objects
Using Page Objects, the same test could be written like this:
*** Test Cases ***
| Login with valid credentials
| | Go to page | LoginPage
| | Login as a normal user
| | The current page should be | DashboardPage
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.
Writing a Page Object class
Page Objects are simple python classes that inherit from PageObjectLibrary.PageObject
. There are only a couple of requirements for the class:
- The class should define a variable named
PAGE_TITLE
- The class should define a variable named
PAGE_URL
which is a URI relative to the site root.
By inheriting from PageObjectLibrary.PageObject
, methods have access to the following special object attributes:
self.selib
- a reference to an instance of SeleniumLibrary. With this you can call any of the SeleniumLibrary keywords via their python method names (eg: self.selib.input_text)self.browser
- a reference to the webdriver object created when a browser was opened by SeleniumLibrary. With this you can bypass SeleniumLibrary and directly call all of the functions provided by the core selenium library.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
vsself._locators["username"]
.
An example Page Object
A Page Object representing a login page might look like this:
from PageObjectLibrary import PageObject
class LoginPage(PageObject):
PAGE_TITLE = "Login - PageObjectLibrary Demo"
PAGE_URL = "/login.html"
_locators = {
"username": "id=id_username",
"password": "id=id_password",
"submit_button": "id=id_submit",
}
def enter_username(self, username):
"""Enter the given string into the username field"""
self.selib.input_text(self.locator.username, username)
def enter_password(self,password):
"""Enter the given string into the password field"""
self.selib.input_text(self.locator.password, password)
def click_the_submit_button(self):
"""Click the submit button, and wait for the page to reload"""
with self._wait_for_page_refresh():
self.selib.click_button(self.locator.submit_button)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file robotframework-pageobjectlibrary-1.1.tar.gz
.
File metadata
- Download URL: robotframework-pageobjectlibrary-1.1.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63945db77afa2811e07c96311fe3fa5e7effe7ade97d6246856515739b80bbc0 |
|
MD5 | cb0c955b85d4e3b07f931035ce7bac40 |
|
BLAKE2b-256 | 95f3d3576764b4fc9fd75cdd472aa90815aa549fac55400a7974fc739ae01a66 |
File details
Details for the file robotframework_pageobjectlibrary-1.1-py3-none-any.whl
.
File metadata
- Download URL: robotframework_pageobjectlibrary-1.1-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 110510b9324d4358f1d5be4a2a00ea9e73c341468941f3346ba614ff9f7c0a12 |
|
MD5 | 4935511303aa3371919112cd006fb658 |
|
BLAKE2b-256 | 7fcaf1f88882bbc5f20dba4bb6a89d62a59f5b25c1107f964fe91e2db59d5f0c |