TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    Selenium

    Learning Selenium PageFactory

    Overview

    In this post, we will discuss about Selenium PageFactory. PageFactory class makes using Page Objects simpler and easier. We can call the ‘initElements’ method of the Webdriver specific PageFactory class, which declares a proxy to the page objects found in the class. The elements in the page objects class are initialized with a lazy proxy, which means that the compiler will look if the element exists on the page only when they will be used

    initElements(WebDriver driver, Class<T> pageClassToProxy)

    Selenium PageFactory Example

    pageObjectInstance = PageFactory.initElements(webDriver, SamplePageObject.class);

    We need to initialize the page object class, so that the elements declared in it can be used to interact with the page .webDriver is the browser instance; ‘SamplePageObject’ is the class where the page object elements are declared.

    PageFactory has other overloaded versions of ‘initElements’ method has shown in the below picture.

     

    PageFactory

     

    By default, the element or the list is looked up each and every time a method is called upon it. To change this behavior, simply annotate the field with the CacheLookup. We can change how the element is located, by using the FindBy annotation.

    Sample Code

    package com.testingdocs.selenium.demo;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;
    
    public class LoginPage 
    {
     WebDriver driver ;
     @FindBy(how=How.ID , using="username")
     WebElement username;
     
     @FindBy(how=How.ID , using="password")
     WebElement password;
     
     @FindBy(how=How.ID , using="loginSubmit")
     WebElement loginSubmit ;
    
     /**
     * @param driver
     */
     public LoginPage(WebDriver driver) {
     this.driver = driver;
     
     }
     
     public void login()
     {
     driver.get("sample_login_url");
     username.sendKeys("demo");
     password.sendKeys("demo123");
     loginSubmit.click();
     }
     
    }

     

    See also to learn more about FindBy annotation : Page Object FindBy Annotation

    This method will attempt to instantiate the class given to it, preferably using a constructor which takes a WebDriver instance as its only argument or falling back on a no-arg constructor.An exception will be thrown if the class cannot be instantiated.

    After the page object class initialization, you can start writing the tests. In the tests, interaction with the web elements is done by using the appropriate page object methods as shown below sample test.

    Sample Test

    package com.testingdocs.selenium.demo;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.PageFactory;
    import org.testng.annotations.Test;
    
    public class LoginPageTest 
    {
     WebDriver driver = new FirefoxDriver();
     LoginPage loginPage ;
     
     @Test()
     public void samplelogintest()
     {
     loginPage = PageFactory.initElements(driver, LoginPage.class);
     loginPage.login();
     }
    
    }

     

    PageFactory class instantiates the login page object and then populates all the web elements annotated with the FindBy annotation.

    More Information on Selenium

    https://www.selenium.dev/

    Related Posts

    Windows 10 Settings

    Selenium /

    Add Microsoft Webdriver on Windows OS

    Download Selenium Components

    Selenium /

    Getting Started with Selenium Webdriver

    LambdaTest Testing Cloud SaaS Platform

    Selenium /

    LambdaTest – Testing Cloud SaaS Platform

    Selenium /

    Selenium 3.0 and Mozilla GeckoDriver

    SauceLabs Website

    Selenium /

    Run an Example Test on SauceLabs

    ‹ Sample Selenium Framework with TestNG› Working with Different Browsers in Selenium

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com