Module 4.1.1 - Selenium Page Object Design Pattern - Helper Class

From Training Material
Jump to navigation Jump to search
package com.shk.webdriver.sanity;

import java.io.File;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * @author vaseemzia
 *
 */

public class LoginPage {  
    
    private final WebDriver driver;  
    private final static Logger logger = Logger.getLogger("com.shk.webdriver.sanity.LoginPage");
  
    public LoginPage(WebDriver driver,int seconds) {  
        super();  
        this.driver = driver;  
  
        WebDriverWait wait = new WebDriverWait(driver, seconds);
        wait.until(ExpectedConditions.titleIs("Sign In"));  
    }  
  
   /*
   *  The HomePage object is returned, once we call the loginAs method. 
   *  From our testscript perspective we can access all the 
   *  public methods in the HomePage class
   * 
   * 
   */
    public HomePage loginAs(String username, String password) {  
  
        executeLogin(username, password);  
        return new HomePage(driver,10);  
    }  
  /*
   * Set the return-type to void, if the focus of an certain action is ambiguous. 
   * Like in highly dynamic websites, 
   * where we have no control on the input/output data. 
   * In this case we can use the code below:
   * 
   */
    public void failLoginAs(String username, String password) {  
        executeLogin(username, password);  
    }  
  
    private void executeLogin(String username, String password)
    {  
  	
		Commons.waitforPageLoad(driver);
//		WebElement txtlogin = Commons.findElement(driver,By.xpath(".//*[@id='i0116']"));
		WebElement txtlogin = (new WebDriverWait(driver, 10))
		  .until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='i0116']")));
		txtlogin.clear();
		txtlogin.sendKeys(username);
		WebElement txtpassword = (new WebDriverWait(driver, 10))
		  .until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='i0118']")));
		txtpassword.clear();
		txtpassword.sendKeys(password);
		WebElement checkbox = (new WebDriverWait(driver, 10))
		  .until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='idChkBx_PWD_KMSI0Pwd']")));
		checkbox.click();
		WebElement submit = (new WebDriverWait(driver, 10))
		  .until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='idSIButton9']")));
		submit.click();
		Commons.takeScreenShot(driver);
       
    }  
    @SuppressWarnings("unused")
	private void executeLoginDsl(String username, String password)
    {  
  	
		Commons.waitforPageLoad(driver);
//		WebElement txtlogin = Commons.findElement(driver,By.xpath(".//*[@id='i0116']"));
		WebElement txtlogin = (new WebDriverWait(driver, 10))
		  .until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='i0116']")));
		txtlogin.clear();
		txtlogin.sendKeys(username);
		Commons.findElementAndType(driver, By.xpath(".//*[@id='i0118']"), password);
		
		WebElement checkbox = (new WebDriverWait(driver, 10))
		  .until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='idChkBx_PWD_KMSI0Pwd']")));
		checkbox.click();
		Commons.findElementAndClick(driver, By.xpath(".//*[@id='idSIButton9']"));
		Commons.takeScreenShot(driver);
       
    } 
    
    
    
    public String getErrorMessage() {  
    	logger.info("Got Error Message  :" + driver.findElement(By.xpath(".//*[@id='idTd_PWD_ErrorMsg_Username']")).getText());
        return driver.findElement(By.xpath(".//*[@id='idTd_PWD_ErrorMsg_Username']")).getText();  
    }  
  
}