Friday, August 5, 2016

AbstractContainer

package com.mridul.framework.selenium.interfaces;  
 import org.openqa.selenium.JavascriptExecutor;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.interactions.Actions;  
 import org.openqa.selenium.support.PageFactory;  
 import org.openqa.selenium.support.ui.ExpectedCondition;  
 import org.openqa.selenium.support.ui.ExpectedConditions;  
 import org.openqa.selenium.support.ui.Select;  
 import org.openqa.selenium.support.ui.WebDriverWait;  
 import org.springframework.context.ApplicationContext;  
 import java.util.Collections;  
 import java.util.List;  
 import java.util.NoSuchElementException;  
 import java.util.concurrent.ThreadLocalRandom;  
 import java.util.concurrent.TimeUnit;  
 /**  
  * Created by Mridul on 3/11/2016.  
  */  
 public abstract class AbstractContainer {  
   private ApplicationContext context;  
   protected WebDriver driver;  
   protected WebDriverWait wait;  
   private Actions actions;  
   private String containerId;  
   public AbstractContainer(WebDriver driver) {  
     ////TODO-Fix spring injection  
     //driver=(WebDriver)context.getBean("chrome");  
     this.driver = driver;  
     PageFactory.initElements(driver, this);  
     wait = new WebDriverWait(driver, 10);  
     actions=new Actions(driver);  
     this.containerId = driver.getWindowHandle();  
     driver.switchTo().window(containerId);  
     driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
   }  
   protected Actions getActionsDriver(){  
     return actions;  
   }  
   protected String getWindowHandle() {  
     return containerId;  
   }  
   private WebElement waitUntilElementIsVisible(WebElement element) {  
     element = wait.until(ExpectedConditions.elementToBeClickable(element));  
     //highLightElement(element);  
     if (element == null) {  
       throw new RuntimeException("Element: " + element + " is not visible!");  
     }  
     return element;  
   }  
   private void highLightElement(WebElement element) {  
       JavascriptExecutor js = (JavascriptExecutor) driver;  
       js.executeScript(  
           //"arguments[0].setAttribute('style', 'border: 3px solid red;');",  
           "arguments[0].setAttribute('style', 'background: red;');",  
           element);  
   }  
   protected void hoverOverElementsAndClick(WebElement... elements){  
     for(WebElement element: elements) {  
       wait.until(ExpectedConditions.visibilityOf(element));  
        actions=actions.moveToElement(element);  
     }  
     actions.click().build().perform();  
   }  
   protected void switchToIFrame(WebElement element) {  
     element = waitUntilElementIsVisible(element);  
     driver.switchTo().frame(element);  
   }  
   protected void populateTextField(WebElement textField, String text) {  
     textField = waitUntilElementIsVisible(textField);  
     textField.click();  
     textField.clear();  
     textField.sendKeys(text);  
   }  
   protected void checkOnCheckBox(WebElement chkbox) {  
     chkbox = waitUntilElementIsVisible(chkbox);  
     if (!chkbox.isSelected())  
       chkbox.click();  
   }  
   protected void unCheckOnCheckBox(WebElement chkbox) {  
     chkbox = waitUntilElementIsVisible(chkbox);  
     if (chkbox.isSelected())  
       chkbox.click();  
   }  
   protected void clickOnButton(WebElement button) {  
     clickOnElement(button);  
   }  
   protected void clickOnWebElement(WebElement element) {  
     clickOnElement(element);  
   }  
   private void clickOnElement(WebElement element) {  
     element = waitUntilElementIsVisible(element);  
     element.click();  
   }  
   protected void selectOptionByText(WebElement selectField, String text) {  
     Select dropdown = wrapAsSelect(selectField);  
 /*    dropdown.getOptions().forEach(option -> {if(option.getText().equalsIgnoreCase(text)){  
       option.click();  
     }});*/  
     dropdown.selectByVisibleText(text.trim());  
   }  
   protected void selectOptionByIndex(WebElement selectField, int index) {  
     Select dropdown = wrapAsSelect(selectField);  
     dropdown.selectByIndex(index);  
   }  
   protected void selectOptionByRandomIndex(WebElement selectField, int maxOptions) {  
     selectOptionByRandomIndex(selectField, 1, maxOptions);  
   }  
   protected int selectOptionByRandomIndex(WebElement selectField, int startIndex, int maxOptions) {  
     Select dropdown = wrapAsSelect(selectField);  
     //int outerBound = maxOptions + 1;  
     int index;  
     do {  
       index = ThreadLocalRandom.current().nextInt(startIndex, maxOptions);  
       if(dropdown.getFirstSelectedOption().getText().contains("None") ||  
           dropdown.getFirstSelectedOption().getText().contains("Other")){  
         continue;  
       }  
     } while (index == 0 || index == maxOptions);  
     dropdown.selectByIndex(index);  
     return index;  
   }  
   ////TODO-Not fully implemented  
   protected void selectOptionByRandomValue(WebElement selectField) {  
     Select dropdown = wrapAsSelect(selectField);  
     WebElement firstOption=dropdown.getFirstSelectedOption();  
     List<WebElement> dropdownOptions=dropdown.getOptions();  
     dropdownOptions.remove(firstOption);//remove the first element  
     Collections.shuffle(dropdownOptions); //shuffles the data for randomness  
     for(WebElement option: dropdownOptions){  
       String text=option.getAttribute("value").trim();  
       if (option.equals(firstOption) || text.equalsIgnoreCase("Other")){  
         continue;  
       }else {  
         dropdown.selectByVisibleText(option.getText());  
         break;  
       }  
     }  
   }  
   private Select wrapAsSelect(WebElement element) {  
     element = waitUntilElementIsVisible(element);  
     Select dropdown = new Select(element);  
     if (dropdown.getOptions().size() == 0) {  
       throw new RuntimeException("Dropdown: " + dropdown + " has no options to select!");  
     }  
     return dropdown;  
   }  
   // template method implemented in child classes  
   protected abstract List<WebElement> registerAnchors();  
   protected final boolean validateAnchors() {  
     List<WebElement> anchors = registerAnchors();  
     Boolean anchorFlag = Boolean.TRUE;  
     if (anchors == null) {  
       throw new NoSuchElementException("Page Anchors are not set or could not be identified! ");  
     } else {  
       for (WebElement elem : anchors) {  
         anchorFlag = anchorFlag && wait.until(new ExpectedCondition<Boolean>() {  
           @Override  
           public Boolean apply(WebDriver d) {  
             return elem.isDisplayed();  
           }  
         });  
       }  
     }  
     return anchorFlag;  
   }  
   //Fluent wrappers  
   public abstract <T extends AbstractContainer> T and();  
   public abstract <T extends AbstractContainer> T then();  
   public void sleep(double seconds)  
   {  
     try {  
       Thread.sleep((long)seconds * 1000);  
     } catch (InterruptedException e) {  
     }  
   }  
 }  

No comments:

Post a Comment