In this part we are going to have a look design pattern for creating maintainable and reusable bits of code that we can use with our Selenium Tests. This means that if there any changes needed to our web application or changes in the way we need to find elements, we can change it once and have it fix everything every quickly.
In this part, we shall learn:
Page Objects: This is a technique where we split the test login out into separate classes. This allows us to create a java class for each of the pages that we use on the page.
In this part, we shall learn:
- Page Object
- using Page Factory with Page Object
- Using LoadableComponents
Page Objects: This is a technique where we split the test login out into separate classes. This allows us to create a java class for each of the pages that we use on the page.
- Page Factory: This allows us to decorate our webElement variables in our age objects so that we remove a lot of the look up code.
The code
that we have learnt to write earlier can be quite verbose.To clean up
our code, we can start to use page factories.This allows us to annotate
variable in our page object with how to search the page. To use the
pageFactory project in Webdriver, we will have to make sure that the we have
added it as a dependency.
For Examples:
we can look for element by name, id, xpath, css, linkText, parcialLinkText, tagName
@FindBy(name="q")
Public
webElement textSearch;
@FindBy
(how=How.Name, using="q")
WebElement
verifyButton2;
where "q" is the name the element.
We need to
initialize the factory by calling initElement() , at the same class.
pageFactory.initElements(driver,
this)
we can initialize the page factory in other class in which they were defined the webElement elements.
pageFactory.initElements(driver, nameClass.class);
We can call initElement() in constructor method or in other method.
For Example:
Using LoadableComponents: In this section we had a look at the base page for page objects that comes with the Selenium Project. It is in a base class allows us to remove quite a bit of code and move the boilerplate to LoadableComponent.
LoadableComponent is another way to approach PageObject. It is a base class that all of the page need to extend. The base class has the following methods on the interface.
- get()
- isLoaded(): This method can allow us to check id the page has been loaded correctly.
- load(): This method will load the page for us
We will have to add overrides for the load() and isLoaded() method.
I will modify at the same example the Page Factory , you can create a new class.
Instead of the usual public class PageObject, we change it:
public class PageObject extends LoadableComponent<PageObject>
For example.









The Page Object is a good design of pattern for automation testing make the code more maintainable. It's very simple to use and implement.
ResponderEliminarGreat Job!!!