MENU

Fun & Interesting

Selenium Cucumber Java BDD Framework 7 - Page Factory | Step by Step

Automation Step by Step 111,437 5 years ago
Video Not Working? Fix It Now

QUIZ - https://forms.gle/khk3hsLVUtY5qAcDA FREE TUTORIAL - https://automationstepbystep.com/ 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) { this.driver = driver; PageFactory.initElements(driver, LoginPage_PF.class); } 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); PageFactory.initElements(factory, Login_PF.class); 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 https://github.com/SeleniumHQ/selenium/wiki/PageFactory https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/PageFactory.html https://example.testproject.io/web/ Join this channel to get access to perks: https://www.youtube.com/automationstepbystep/join 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 If my work has helped you, consider helping https://animalaidunlimited.org/ or any animal welfare group near you, in any way you can. ________ ONLINE COURSES TO LEARN ________ https://automationstepbystep.com/online-courses/

Comment