Saturday, February 7, 2015

Wait in Page Factory

We know that in Selenium WebDriver already have implicit and explicit wait,it is required to wait for an element until some condition is satisfied or to check that element is loaded or not.

In Page Factory model  there is AjaxElementLocatorFactory class by which we can implement another type of waiting technique for web element, it is basically behave same as implicit wait.

We can initialize AjaxElementLocatorFactory into the constructor by which we can avail the wait facility on all WebElements in the same page object class . It is basically used where application have more Ajax component Elements.

Example code is given below
  //***************************************
  // BLOGPAGE Class
  //*************************************** 

package com.core.pageobject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;

public class BlogPage {

    @FindBy(name = "txtbox1")
    private WebElement inputBox;
    @FindBy(name = "btnsub")
    private WebElement submitBtn;
    @FindBy(linkText = "Google")
    private WebElement googleLink;
    private final WebDriver driver;

    public BlogPage(WebDriver driver) {
        this.driver = driver;

        //  Wait 20 Second To Find Element If Element Is Not Present
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, 20), this);
    }

    public BlogPage submitForm(String inputTxt) {
        inputBox.sendKeys(inputTxt);
        return new BlogPage(driver);
    }

    public GooglePage clickOnGoogleLink() {
        googleLink.click();
        return new GooglePage(driver);
    }
}
 

  //***************************************
  // GOOGLEPAGE Class
  //***************************************
package com.core.pageobject;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;

public class GooglePage {

    private final WebDriver driver;
    @FindBy(name = "q")
    WebElement searchTxtBox;

    public GooglePage(WebDriver driver) {
        this.driver = driver;

        //  Wait 20 Second To Find Element If Element Is Not Present
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, 20), this);
    }

    public GooglePage searchTxt(String searchTxt) {
        searchTxtBox.sendKeys(searchTxt);
        return new GooglePage(driver);
    }
}


  //***************************************
  // Test Class
  //**************************************
package com.core.pageobject;

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class PageObjectModelTest {

    WebDriver driver;

    @BeforeTest
    public void testSetUp() {
        FirefoxBinary bin = new FirefoxBinary(new File("G:\\Program Files\\Mozilla Firefox\\firefox.exe"));
        FirefoxProfile prof = new FirefoxProfile();
        driver = new FirefoxDriver(bin, prof);
    }

    @BeforeMethod
    public void testMethodSetUp() {
        driver.get("http://startingwithseleniumwebdriver.blogspot.in/2013/12/frmset1.html");
    }

    @Test
    public void testSubmit() throws Exception {
        BlogPage blog = new BlogPage(driver);
        blog.submitForm("Test");
    }

    @Test
    public void testGoogleLink() throws Exception {
        BlogPage blog =new BlogPage(driver);
        blog.clickOnGoogleLink().searchTxt("Test");
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }
}

No comments:

Post a Comment