Advertisement
najjah

Selenium Browser

Feb 10th, 2014
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. package br.com.infoglobo.testes.desktop.cucumber;
  2.  
  3. import java.util.Set;
  4.  
  5. import org.openqa.selenium.By;
  6. import org.openqa.selenium.JavascriptExecutor;
  7. import org.openqa.selenium.NoSuchWindowException;
  8. import org.openqa.selenium.WebDriver;
  9. import org.openqa.selenium.WebElement;
  10. import org.openqa.selenium.firefox.FirefoxDriver;
  11. import org.openqa.selenium.ie.InternetExplorerDriver;
  12. import org.openqa.selenium.support.ui.ExpectedCondition;
  13. import org.openqa.selenium.support.ui.ExpectedConditions;
  14. import org.openqa.selenium.support.ui.Select;
  15. import org.openqa.selenium.support.ui.WebDriverWait;
  16.  
  17. public class Browser {
  18.  
  19.     private static Browser browser;
  20.     private WebDriver driver;
  21.     private long browserTimeout;
  22.     private static final long DEFAULT_TIMEOUT = 60;
  23.     private static final BrowserType DEFAULT_BROWSER = BrowserType.FIREFOX;
  24.  
  25.     public enum BrowserType { IE, FIREFOX }
  26.  
  27.     /**
  28.      * Avoid constructing without calling getInstance
  29.      */
  30.     private Browser(){
  31.     }
  32.  
  33.     /**
  34.      * Private constructor used by public getInstance methods
  35.      * @param type
  36.      * @param timeout
  37.      */
  38.     private Browser(BrowserType type, long timeout){
  39.         switch(type){
  40.             case IE:
  41.                 driver = new InternetExplorerDriver();
  42.                 break;
  43.             case FIREFOX:
  44.                 driver = new FirefoxDriver();
  45.                 break;
  46.         }
  47.         browserTimeout = timeout;
  48.         driver.manage().window().maximize();
  49.     }
  50.  
  51.     /**
  52.      * Default singleton assigning BrowserType Firefox and default timeout 60 seconds
  53.      * @return
  54.      */
  55.     public static Browser getInstance(){
  56.         return getInstance(DEFAULT_BROWSER, DEFAULT_TIMEOUT);
  57.     }
  58.  
  59.     /**
  60.      * Singleton passing default timeout as argument and assuming BrowserType Firefox as default
  61.      * @param defaultTimeout
  62.      * @return
  63.      */
  64.     public static Browser getInstance(long defaultTimeout){
  65.         return getInstance(DEFAULT_BROWSER, defaultTimeout);
  66.     }
  67.  
  68.     /**
  69.      * Singleton passing BrowserType and assuming 60 seconds as default timeout
  70.      * @param browserType
  71.      * @return
  72.      */
  73.     public static Browser getInstance(BrowserType browserType){
  74.         return getInstance(browserType, DEFAULT_TIMEOUT);
  75.     }
  76.  
  77.     /**
  78.      * Singleton fully customized.
  79.      * @param type
  80.      * @param defaultTimeout
  81.      * @return
  82.      */
  83.     public static Browser getInstance(BrowserType type, long defaultTimeout){
  84.         if(browser == null) {
  85.             browser = new Browser(type, defaultTimeout);
  86.         }
  87.         return browser;
  88.     }
  89.  
  90.     public void close(){
  91.         this.driver.close();
  92.         browser = null;
  93.     }
  94.  
  95.     public void get(String url){
  96.         this.driver.get(url);
  97.     }
  98.  
  99.     public WebElement findElement(By locator){
  100.         waitUntilElementIsPresent(locator);
  101.         return this.driver.findElement(locator);
  102.     }
  103.  
  104.     public Select findSelect(By locator){
  105.         waitUntilElementIsPresent(locator);
  106.         return new Select(this.driver.findElement(locator));
  107.     }
  108.  
  109.     public boolean isElementPresent (By locator) {
  110.         return this.driver.findElements(locator).size() > 0;
  111.     }
  112.  
  113.     public boolean isElementClickable(By locator){
  114.         return ExpectedConditions.elementToBeClickable(locator) != null;
  115.     }
  116.  
  117.     public void waitUntilElementIsPresent(By locator){
  118.         final By elementLocator = locator;
  119.         new WebDriverWait(this.driver,this.browserTimeout).until(
  120.                 new ExpectedCondition<Boolean>() {
  121.                     public Boolean apply(WebDriver d) {
  122.                         return d.findElements(elementLocator).size() > 0;
  123.                     }
  124.                 }
  125.             );
  126.     }
  127.  
  128.     public void waitUntilElementIsClickable(By locator){
  129.         final By elementLocator = locator;
  130.         new WebDriverWait(this.driver, this.browserTimeout).until(
  131.                 ExpectedConditions.elementToBeClickable(elementLocator)
  132.         );
  133.     }
  134.  
  135.     public void waitUntilPageIsLoaded(){
  136.         new WebDriverWait(this.driver,this.browserTimeout).until(
  137.             new ExpectedCondition<Boolean>() {
  138.                 public Boolean apply(WebDriver d) {
  139.                     return ((JavascriptExecutor) d).executeScript("return document.readyState").equals("complete");
  140.                 }
  141.             }
  142.         );
  143.     }
  144.  
  145.     public void switchToWindow(Set<String> previousHandles){
  146.         final Set<String> handles = previousHandles;
  147.         String newHandle = new WebDriverWait(this.driver, this.browserTimeout).until(
  148.             new ExpectedCondition<String>() {
  149.                 public String apply(WebDriver d){
  150.                     Set<String> currentHandles = d.getWindowHandles();
  151.                     currentHandles.removeAll(handles);
  152.                     if(currentHandles.size()>0)
  153.                         return currentHandles.iterator().next();
  154.                     return null;
  155.                 }
  156.             }
  157.         );
  158.         this.driver.switchTo().window(newHandle);
  159.     }
  160.  
  161.     public void switchToWindow(String windowHandleOrName){
  162.         final String title = windowHandleOrName;
  163.         new WebDriverWait(this.driver, this.browserTimeout).until(
  164.             new ExpectedCondition<Boolean>() {
  165.                 public Boolean apply(WebDriver d){
  166.                     try{
  167.                         d.switchTo().window(title);
  168.                         return true;
  169.                     } catch(NoSuchWindowException e){
  170.                         return false;
  171.                     }
  172.                 }
  173.             }
  174.         );
  175.         this.driver.switchTo().window(title);
  176.     }
  177.  
  178.     public void waitInSeconds(long seconds){
  179.         new WebDriverWait(this.driver,seconds);
  180.     }
  181.  
  182.     public WebDriver getDriver() {
  183.         return driver;
  184.     }
  185.  
  186.     public void setDriver(WebDriver driver) {
  187.         this.driver = driver;
  188.     }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement