Java POM: 06-Selenium

 

Imports

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

 

Components Commonly Used

WebDriver

  • Create a public class so its accessible through the application
  • Create a single static object so it also can be referenced throughout the application
  • This class will do the work of building up & calling the correct Browser & Profile
  • Then return the driver for use with the PageObjects(which will NOT be static)
public class BrowserUtils {
    public static WebDriver driver = null;
    public static WebDriver OpenBrowser(){

            // Create web driver
            driver = new FirefoxDriver();
            return driver;
    } //end method

}

 

WebElement

  • PageObjects are used only to enumerate objects & the selectors used to access them
  • Best Practice says to either abstract the selectors to
    • the TOP of each PageObject Class OR
      • PRO: selectors & their implementation are tightly bound & easy to find
      • CON: massive duplication is likely to occur as many elements will be the same across the various pages of the web application
    • in a separate 'object library' class
      • PRO: virtually eliminates duplicate references
      • CON: can make locating elements hard if the unified file isn't well organized
    • SUGGEST to use Enumerations, one for each page or 'section' of a page ( like a menu bar )
      • Enums have inherent helper methods that can come in handy
      • Forces grouping of selectors
      • Can cause redundancy, BUT if common page elements have their own enums, this is largely avoided
  • This makes maintenance possible & selectors easy to find
  • Embedding/hard-coding the selectors begs for trouble when the project becomes large
  •  
public class Home_Page extends PageBaseClass {
    private static WebElement element = null;

    public Home_Page(WebDriver driver) {
        super(driver);
    }

    public static WebElement link_Home() throws Exception {
        try {

            element = driver.findElement(By.xpath("//a[contains(.,'Home')]"));
        } catch (Exception e) {
            throw (e);
        }
        return element;
    }

 

 

 

 

 

Tags