filmov
tv
Selenium Cucumber Java BDD Framework 7 - Page Factory | Step by Step
Показать описание
Today we will learn:
1 - What is Page Factory
2 - How to implement Page Factory
3 - Useful Tips
What is Page Factory
A simple and easier implementation of Page Object Model in Selenium
Selenium’s inbuilt and optimized Page Object Model concept
As POM, has separation of objects and tests
Uses annotation @FindBy to find WebElements
@FindBy can use id, name, css, xpath, tagName, linkText, partialLinkText etc
Uses method initElements to initialize web elements
on calling initElements method all objects on that page gets initialized
Demo How to implement Page Factory Model
Step 1 - Create a class for each page
Step 2 - Create locators of all objects to be used in that page using @FindBy
Step 3 - Create methods or actions to be performed on the objects
Step 4 - Create constructor to get driver instance and
initialize Elements using method initElements
public LoginPage_PF(WebDriver driver) {
}
Step 5 - Update Test Scripts to refer methods from PageFactory class
Step 6 - Run and validate
Useful Tips 1
@CacheLookup
CacheLookup can be used to instruct the InitElements() method to cache the element once its located and so that it will not be searched over and over again whenever calling it from any method
@FindBy(id = "name")
@CacheLookup
WebElement txt_username;
This works well with a basic web application, but not recommended if you have Ajax applications where DOM changes on user actions.
In case you get StaleElementExceptions, avoid using this.
Useful Tips 2
In Ajax applications to handle loading time for element and to avoid
‘No Element Exception’, we can use
AjaxElementLocatorFactory Class
timeout for a WebElement can be assigned to the Object page class with the help of AjaxElementLocatorFactory
AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver, 30);
The above code will wait for maximum of 30 seconds until the elements specified by annotations is loaded.
If the element is not found in the given time interval, it will throw ‘NoSuchElementException' exception.
Useful Tips 3
With PageFactory, you can also locate a list of Elements
@FindBy(partialLinkText = "raghav")
List<WebElement> myLinks;
References
Hit Like and. Subscribe button, if you like this video. It gives me great motivation.
You can support my mission for education by sharing this knowledge and helping as many people as you can
________ ONLINE COURSES TO LEARN ________
1 - What is Page Factory
2 - How to implement Page Factory
3 - Useful Tips
What is Page Factory
A simple and easier implementation of Page Object Model in Selenium
Selenium’s inbuilt and optimized Page Object Model concept
As POM, has separation of objects and tests
Uses annotation @FindBy to find WebElements
@FindBy can use id, name, css, xpath, tagName, linkText, partialLinkText etc
Uses method initElements to initialize web elements
on calling initElements method all objects on that page gets initialized
Demo How to implement Page Factory Model
Step 1 - Create a class for each page
Step 2 - Create locators of all objects to be used in that page using @FindBy
Step 3 - Create methods or actions to be performed on the objects
Step 4 - Create constructor to get driver instance and
initialize Elements using method initElements
public LoginPage_PF(WebDriver driver) {
}
Step 5 - Update Test Scripts to refer methods from PageFactory class
Step 6 - Run and validate
Useful Tips 1
@CacheLookup
CacheLookup can be used to instruct the InitElements() method to cache the element once its located and so that it will not be searched over and over again whenever calling it from any method
@FindBy(id = "name")
@CacheLookup
WebElement txt_username;
This works well with a basic web application, but not recommended if you have Ajax applications where DOM changes on user actions.
In case you get StaleElementExceptions, avoid using this.
Useful Tips 2
In Ajax applications to handle loading time for element and to avoid
‘No Element Exception’, we can use
AjaxElementLocatorFactory Class
timeout for a WebElement can be assigned to the Object page class with the help of AjaxElementLocatorFactory
AjaxElementLocatorFactory factory = new AjaxElementLocatorFactory(driver, 30);
The above code will wait for maximum of 30 seconds until the elements specified by annotations is loaded.
If the element is not found in the given time interval, it will throw ‘NoSuchElementException' exception.
Useful Tips 3
With PageFactory, you can also locate a list of Elements
@FindBy(partialLinkText = "raghav")
List<WebElement> myLinks;
References
Hit Like and. Subscribe button, if you like this video. It gives me great motivation.
You can support my mission for education by sharing this knowledge and helping as many people as you can
________ ONLINE COURSES TO LEARN ________
Комментарии