Module 4.2 - Selenium DSL Design Pattern - Example

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

import java.io.File;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.commons.io.FileUtils;
import org.junit.internal.runners.TestMethod;
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.browserlaunchers.locators.Firefox2Locator;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.internal.selenesedriver.FindElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Commons 
{

	public static void waitforPageLoad(WebDriver driver)
	{
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}
	/*
	 * DSL
	 */
	public static void findElementAndClick(WebDriver driver, By by)
	{
		driver.findElement(by).click();
	}
	
	/*
	 * 
	 * 
	 */
	
	public static void findElementAndType(WebDriver driver,By by,String testData)
	{
			driver.findElement(by).clear();
			driver.findElement(by).sendKeys(testData);
	}

	public static ExpectedCondition<WebElement> visibilityOfElementLocated(final By by) {
        return new ExpectedCondition<WebElement>() {
          public WebElement apply(WebDriver driver) {
            WebElement element = driver.findElement(by);
            return element.isDisplayed() ? element : null;
          }
        };
      }
	
	
	public static void verifyTitle(WebDriver driver,int secsToWait,String title) throws TimeoutException 
	{
	    new WebDriverWait(driver, secsToWait)
		        .until(ExpectedConditions.titleIs(title));
	}
	
	
	public static void takeScreenShot(WebDriver driver) {
		File screenShot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
		try {
		FileUtils.copyFile(screenShot, new File("C:\\screenShots\\" +".png"));
		} catch (IOException ioe) {
		throw new RuntimeException(ioe.getMessage(), ioe);
		}
		}
	
	
}