Framework - W8D2-1
~30 mins
Framework
DriverInstance.java
package com.framework.selenium.api.base;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DriverInstance {
private static final ThreadLocal<RemoteWebDriver> remoteWebdriver = new ThreadLocal<RemoteWebDriver>();
private static final ThreadLocal<WebDriverWait> wait = new ThreadLocal<WebDriverWait>();
public void setWait() {
wait.set(new WebDriverWait(getDriver(), 30));
}
public WebDriverWait getWait() {
return wait.get();
}
public void setDriver(String browser, boolean headless) {
switch (browser) {
case "chrome":
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
options.addArguments("--incognito");
remoteWebdriver.set(new ChromeDriver(options));
break;
case "firefox":
remoteWebdriver.set(new FirefoxDriver());
break;
case "ie":
remoteWebdriver.set(new InternetExplorerDriver());
default:
break;
}
}
public RemoteWebDriver getDriver() {
return remoteWebdriver.get();
}
}
SeleniumBase.java
package com.framework.selenium.api.base;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.framework.selenium.api.design.Browser;
import com.framework.selenium.api.design.Element;
import com.framework.selenium.api.design.Locators;
import com.framework.utils.Reporter;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SeleniumBase extends Reporter implements Browser, Element {
protected Actions act;
public static String projectId;
public static String auctionRef;
protected String getAttribute(WebElement ele, String attributeValue) {
String val = "";
try {
val = ele.getAttribute(attributeValue);
} catch (WebDriverException e) {
reportStep("Attribue value not able to fetch :" + e.getMessage(), "info");
}
return val;
}
protected void moveToElement(WebElement ele) {
act = new Actions(getDriver());
act.moveToElement(ele).perform();
}
protected void dragAndDrop(WebElement eleSoutce, WebElement eleTarget) {
act = new Actions(getDriver());
act.dragAndDrop(eleSoutce, eleTarget).perform();
}
protected void contextClick(WebElement ele) {
act = new Actions(getDriver());
act.contextClick(getWait().until(ExpectedConditions.elementToBeClickable(ele))).perform();
}
protected void hoverAndClick(WebElement ele) {
act = new Actions(getDriver());
act.moveToElement(getWait().until(ExpectedConditions.elementToBeClickable(ele))).pause(5000).click().perform();
}
protected void doubleTap(WebElement ele) {
act = new Actions(getDriver());
act.click(getWait().until(ExpectedConditions.elementToBeClickable(ele))).click().perform();
reportStep("Element moved", "info");
}
protected void doubleClick(WebElement ele) {
act = new Actions(getDriver());
act.doubleClick(getWait().until(ExpectedConditions.elementToBeClickable(ele))).perform();
reportStep("Element double clicked", "info");
}
public void waitForApperance(WebElement element) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
WebDriverWait wait = new WebDriverWait(getDriver(), 20);
wait.until(ExpectedConditions.visibilityOf(element));
} catch (Exception e) {
reportStep("Element did not appear after 20 seconds", "fail", false);
}
}
@Override
public void click(WebElement ele) {
try {
ele.isDisplayed(); // @FindBy return the proxy even if it does not exist !!
}catch (NoSuchElementException e) {
reportStep("The Element " + ele + " is not found", "fail");
}
String text = "";
try {
try {
Thread.sleep(500);
getWait().until(ExpectedConditions.elementToBeClickable(ele));
text = ele.getText();
if (ele.isEnabled()) {
ele.click();
} else {
getDriver().executeScript("arguments[0].click()", ele);
}
} catch (Exception e) {
boolean bFound = false;
int totalTime = 0;
while(!bFound && totalTime < 10000) {
try {
Thread.sleep(500);
ele.click();
bFound = true;
} catch (Exception e1) {
bFound = false;
}
totalTime = totalTime+500;
}
if(!bFound)
ele.click();
}
} catch (StaleElementReferenceException e) {
System.err.println(e);
reportStep("The Element " + text + " could not be clicked due to:" + e.getMessage(), "fail");
} catch (WebDriverException e) {
System.err.println(e);
reportStep("The Element " + ele + " could not be clicked due to: " + e.getMessage(), "fail");
} catch (Exception e) {
System.err.println(e);
reportStep("The Element " + ele + " could not be clicked due to: " + e.getMessage(), "fail");
}
}
public void clickUsingJs(WebElement ele) {
try {
ele.isDisplayed(); // @FindBy return the proxy even if it does not exist !!
}catch (NoSuchElementException e) {
reportStep("The Element " + ele + " is not found", "fail");
}
String text = "";
try {
try {
getDriver().executeScript("arguments[0].click()", ele);
} catch (Exception e) {
boolean bFound = false;
int totalTime = 0;
while(!bFound && totalTime < 10000) {
try {
Thread.sleep(500);
getDriver().executeScript("arguments[0].click()", ele);
bFound = true;
} catch (Exception e1) {
bFound = false;
}
totalTime = totalTime+500;
}
if(!bFound)
getDriver().executeScript("arguments[0].click()", ele);
}
} catch (StaleElementReferenceException e) {
reportStep("The Element " + text + " could not be clicked due to:" + e.getMessage(), "fail");
} catch (WebDriverException e) {
reportStep("The Element " + ele + " could not be clicked due to: " + e.getMessage(), "fail");
} catch (Exception e) {
reportStep("The Element " + ele + " could not be clicked due to: " + e.getMessage(), "fail");
}
}
public void click(Locators locatorType, String value) {
String text = "";
WebElement ele = locateElement(locatorType,value);
try {
try {
Thread.sleep(500);
getWait().until(ExpectedConditions.elementToBeClickable(ele));
text = ele.getText();
if (ele.isEnabled()) {
ele.click();
} else {
getDriver().executeScript("arguments[0].click()", ele);
}
} catch (Exception e) {
boolean bFound = false;
int totalTime = 0;
while(!bFound && totalTime < 10000) {
try {
Thread.sleep(500);
ele = locateElement(locatorType,value);
ele.click();
bFound = true;
} catch (Exception e1) {
bFound = false;
}
totalTime = totalTime+500;
}
if(!bFound)
ele.click();
}
} catch (StaleElementReferenceException e) {
reportStep("The Element " + text + " could not be clicked " + e.getMessage(), "fail");
} catch (WebDriverException e) {
reportStep("The Element " + ele + " could not be clicked \n" + e.getMessage(), "fail");
} catch (Exception e) {
reportStep("The Element " + ele + " could not be clicked \n" + e.getMessage(), "fail");
}
}
public void clickWithNoSnap(WebElement ele) {
String text = ele.getText();
try {
getWait().until(ExpectedConditions.elementToBeClickable(ele));
ele.click();
} catch (StaleElementReferenceException e) {
reportStep("The Element " + ele + " could not be clicked \n" + e.getMessage(), "fail", false);
} catch (WebDriverException e) {
reportStep("The Element " + ele + " could not be clicked \n" + e.getMessage(), "fail", false);
} catch (Exception e) {
reportStep("The Element " + ele + " could not be clicked \n" + e.getMessage(), "fail", false);
}
}
@Override
public void append(WebElement ele, String data) {
try {
String attribute = ele.getAttribute("value");
if (attribute.length() > 1) {
ele.sendKeys(data);
} else {
ele.sendKeys(data);
}
} catch (WebDriverException e) {
reportStep("The Element " + ele + " could not be appended \n" + e.getMessage(), "fail");
}
}
@Override
public void clear(WebElement ele) {
try {
ele.clear();
} catch (ElementNotInteractableException e) {
reportStep("The field is not Interactable \n" + e.getMessage(), "fail");
}
}
/**
* Overloaded method used to clear the existing value and type the data with
* keys for tab or enter kind of
*
* @param ele - WebElement from the DOM
* @param data - Use to type and pass Keys as many needed
*/
public void clearAndType(WebElement ele, CharSequence... data) {
try {
getWait().until(ExpectedConditions.visibilityOf(ele));
ele.clear();
ele.sendKeys(data);
} catch (ElementNotInteractableException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
} catch (WebDriverException e) { // retry - 1
pause(500);
try {
ele.sendKeys(data);
} catch (Exception e1) {
reportStep("The Element " + ele + " did not allow to clear / type \n" + e.getMessage(), "fail");
}
}
}
@Override
public void clearAndType(WebElement ele, String data) {
try {
getWait().until(ExpectedConditions.visibilityOf(ele));
ele.clear();
ele.sendKeys("", "", data);
} catch (ElementNotInteractableException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
} catch (WebDriverException e) { // retry - 1
pause(500);
try {
ele.sendKeys(data);
} catch (Exception e1) {
reportStep("The Element " + ele + " did not allow to clear / type \n" + e.getMessage(), "fail");
}
}
}
public void typeAndTab(WebElement ele, String data) {
try {
getWait().until(ExpectedConditions.visibilityOf(ele));
ele.clear();
ele.sendKeys("", "", data, Keys.TAB);
} catch (ElementNotInteractableException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
} catch (WebDriverException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
}
}
public void type(WebElement ele, String data) {
try {
getWait().until(ExpectedConditions.visibilityOf(ele));
ele.clear();
ele.sendKeys("", "", data);
} catch (ElementNotInteractableException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
} catch (WebDriverException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
}
}
public void typeAndEnter(WebElement ele, String data) {
try {
getWait().until(ExpectedConditions.visibilityOf(ele));
ele.clear();
ele.sendKeys("", "", data, Keys.ENTER);
} catch (ElementNotInteractableException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
} catch (WebDriverException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
}
}
@Override
public String getElementText(WebElement ele) {
try {
String text = ele.getText();
reportStep("Text has been retrieved " + text, "info");
return text;
} catch (WebDriverException e) {
reportStep("Sorry! text is not available \n" + e.getMessage(), "fail");
} catch (Exception e) {
reportStep("Sorry! text is not available \n" + e.getMessage(), "fail");
}
return null;
}
@Override
public String getBackgroundColor(WebElement ele) {
String cssValue = null;
try {
cssValue = ele.getCssValue("color");
reportStep("The background color is " + cssValue, "info");
} catch (WebDriverException e) {
reportStep("Not able to get the background color \n" + e.getMessage(), "fail");
} catch (Exception e) {
reportStep("Not able to get the background color \n" + e.getMessage(), "fail");
}
return cssValue;
}
@Override
public String getTypedText(WebElement ele) {
String attributeValue = null;
try {
attributeValue = ele.getAttribute("value");
reportStep("The attribute value is " + attributeValue, "info");
} catch (WebDriverException e) {
reportStep("Not able to find attribute value \n" + e.getMessage(), "fail");
}
return attributeValue;
}
@Override
public void selectDropDownUsingText(WebElement ele, String value) {
try {
Select sel = new Select(ele);
sel.selectByVisibleText(value);
} catch (WebDriverException e) {
reportStep("Not able to select the drop down with text \n" + value, "fail");
}
}
@Override
public void selectDropDownUsingIndex(WebElement ele, int index) {
try {
Select sel = new Select(ele);
sel.selectByIndex(index);
} catch (WebDriverException e) {
reportStep("Not able to select the drop down with index " + index + " \n" + e.getMessage(), "fail");
}
}
@Override
public void selectDropDownUsingValue(WebElement ele, String value) {
try {
Select sel = new Select(ele);
sel.selectByValue(value);
} catch (WebDriverException e) {
reportStep("Not able to select the drop down with value " + value + " \n" + e.getMessage(), "fail");
}
}
@Override
public boolean verifyExactText(WebElement ele, String expectedText) {
try {
String text = ele.getText();
if (text.contains(expectedText)) {
return true;
} else {
reportStep("The expected text " + text + "doesn't equals to the " + expectedText, "warning");
}
} catch (WebDriverException e) {
reportStep("Unknown exception occured while verifying the Text \n" + e.getMessage(), "fail");
}
return false;
}
@Override
public boolean verifyPartialText(WebElement ele, String expectedText) {
try {
if (ele.getText().contains(expectedText)) {
return true;
} else {
reportStep("The expected text doesn't contain the actual " + expectedText, "warning");
}
} catch (WebDriverException e) {
reportStep("Unknown exception occured while verifying the Text \n" + e.getMessage(), "fail");
}
return false;
}
@Override
public boolean verifyExactAttribute(WebElement ele, String attribute, String value) {
try {
if (ele.getAttribute(attribute).equals(value)) {
return true;
} else {
reportStep("The expected attribute :" + attribute + " value does not contains the actual " + value,
"warning");
}
} catch (WebDriverException e) {
reportStep("Unknown exception occured while verifying the Attribute Text \n" + e.getMessage(), "fail");
}
return false;
}
@Override
public void verifyPartialAttribute(WebElement ele, String attribute, String value) {
try {
if (ele.getAttribute(attribute).contains(value)) {
reportStep("The expected attribute :" + attribute + " value contains the actual " + value, "pass");
} else {
reportStep("The expected attribute :" + attribute + " value does not contains the actual " + value,
"warning");
}
} catch (WebDriverException e) {
reportStep("Unknown exception occured while verifying the Attribute Text \n" + e.getMessage(), "fail");
}
}
@Override
public boolean verifyDisplayed(WebElement ele) {
try {
if (ele.isDisplayed()) {
return true;
} else {
reportStep("The element " + ele + " is not visible", "warnings");
}
} catch (WebDriverException e) {
reportStep("WebDriverException : \n" + e.getMessage(), "fail");
}
return false;
}
@Override
public boolean verifyDisappeared(WebElement ele) {
try {
Boolean until = getWait().until(ExpectedConditions.invisibilityOf(ele));
reportStep("Waited for an element to disappear", "info");
return until;
} catch (org.openqa.selenium.TimeoutException e) {
reportStep("Element not disappeared \n" + e.getMessage(), "fail");
} catch (Exception e) {
reportStep("Element not disappeared \n" + e.getMessage(), "fail");
}
return false;
}
@Override
public boolean verifyEnabled(WebElement ele) {
try {
if (ele.isEnabled()) {
return true;
} else {
reportStep("The element " + ele + " is not Enabled", "warning");
}
} catch (WebDriverException e) {
reportStep("WebDriverException : \n" + e.getMessage(), "fail");
}
return false;
}
@Override
public boolean verifySelected(WebElement ele) {
try {
if (ele.isSelected()) {
return true;
} else {
reportStep("The element " + ele + " is not selected", "warning");
}
} catch (WebDriverException e) {
reportStep("WebDriverException : \n" + e.getMessage(), "fail");
}
return false;
}
@Override
public void startApp(String url, boolean headless) {
try {
setDriver("chrome", headless);
setWait();
act = new Actions(getDriver());
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
getDriver().get(url);
reportStep("The Browser Launched in chrome browser with URL " + url, "pass");
} catch (Exception e) {
reportStep("Something went wrong \n" + e.getMessage(), "fail");
}
}
@Override
public void startApp(String browser, boolean headless, String url) {
try {
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.silentOutput", "true");
// System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriverManager.chromedriver().setup();
setDriver("chrome", headless);
} else if (browser.equalsIgnoreCase("firefox")) {
//System.setProperty("webdriver.gecko.driver", "./drivers/geckodriver.exe");
WebDriverManager.firefoxdriver().setup();
setDriver("firefox", headless);
} else if (browser.equalsIgnoreCase("ie")) {
//System.setProperty("webdriver.ie.driver", "./drivers/IEDriverServer.exe");
WebDriverManager.iedriver().setup();
setDriver("ie",false);
}
setWait();
getDriver().manage().window().maximize();
getDriver().manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
getDriver().manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
getDriver().get(url);
} catch (WebDriverException e) {
e.printStackTrace();
reportStep("The Browser Could not be Launched. Hence Failed \n" + e.getMessage(), "fail");
} catch (Exception e) {
e.printStackTrace();
reportStep("The Browser Could not be Launched. Hence Failed \n" + e.getMessage(), "fail");
}
}
@Override
public WebElement locateElement(Locators locatorType, String value) {
try {
switch (locatorType) {
case CLASS_NAME:
return getDriver().findElement(By.className(value));
case CSS:
return getDriver().findElement(By.cssSelector(value));
case ID:
return getDriver().findElement(By.id(value));
case LINK_TEXT:
return getDriver().findElement(By.linkText(value));
case NAME:
return getDriver().findElement(By.name(value));
case PARTIAL_LINKTEXT:
return getDriver().findElement(By.partialLinkText(value));
case TAGNAME:
return getDriver().findElement(By.tagName(value));
case XPATH:
return getDriver().findElement(By.xpath(value));
default:
System.err.println("Locator is not Valid");
break;
}
} catch (NoSuchElementException e) {
reportStep("The Element with locator:" + locatorType + " Not Found with value: " + value + "\n"
+ e.getMessage(), "fail");
} catch (Exception e) {
reportStep("The Element with locator:" + locatorType + " Not Found with value: " + value + "\n"
+ e.getMessage(), "fail");
}
return null;
}
@Override
public WebElement locateElement(String value) {
try {
WebElement findElementById = getDriver().findElementById(value);
return findElementById;
} catch (NoSuchElementException e) {
reportStep("The Element with locator id Not Found with value: " + value + "\n" + e.getMessage(), "fail");
} catch (Exception e) {
reportStep("The Element with locator id Not Found with value: " + value + "\n" + e.getMessage(), "fail");
}
return null;
}
@Override
public List<WebElement> locateElements(Locators type, String value) {
try {
switch (type) {
case CLASS_NAME:
return getDriver().findElements(By.className(value));
case CSS:
return getDriver().findElements(By.cssSelector(value));
case ID:
return getDriver().findElements(By.id(value));
case LINK_TEXT:
return getDriver().findElements(By.linkText(value));
case NAME:
return getDriver().findElements(By.name(value));
case PARTIAL_LINKTEXT:
return getDriver().findElements(By.partialLinkText(value));
case TAGNAME:
return getDriver().findElements(By.tagName(value));
case XPATH:
return getDriver().findElements(By.xpath(value));
default:
System.err.println("Locator is not Valid");
break;
}
} catch (NoSuchElementException e) {
reportStep("The Element with locator:" + type + " Not Found with value: " + value + "\n" + e.getMessage(),
"fail");
}
return null;
}
@Override
public void switchToAlert() {
try {
getDriver().switchTo().alert();
reportStep("Focus has been switched to Alert", "info", false);
} catch (NoAlertPresentException e) {
reportStep("There is no alert present.", "fail", false);
} catch (WebDriverException e) {
reportStep("WebDriverException : " + e.getMessage(), "fail", false);
}
}
@Override
public void acceptAlert() {
String text = "";
try {
getWait().until(ExpectedConditions.alertIsPresent());
Alert alert = getDriver().switchTo().alert();
text = alert.getText();
alert.accept();
reportStep("The alert " + text + " is accepted.", "pass", false);
} catch (NoAlertPresentException e) {
reportStep("There is no alert present.", "fail", false);
} catch (WebDriverException e) {
reportStep("WebDriverException : " + e.getMessage(), "fail", false);
}
}
@Override
public void dismissAlert() {
String text = "";
try {
Alert alert = getDriver().switchTo().alert();
text = alert.getText();
alert.dismiss();
reportStep("The alert " + text + " is accepted.", "pass", false);
} catch (NoAlertPresentException e) {
reportStep("There is no alert present.", "pass", false);
} catch (WebDriverException e) {
reportStep("WebDriverException : " + e.getMessage(), "fail", false);
}
}
@Override
public String getAlertText() {
String text = "";
try {
Alert alert = getDriver().switchTo().alert();
text = alert.getText();
reportStep("The alert text is " + text, "pass", false);
} catch (NoAlertPresentException e) {
reportStep("There is no alert present.", "fail", false);
} catch (WebDriverException e) {
reportStep("WebDriverException : \n" + e.getMessage(), "fail", false);
}
return text;
}
@Override
public void typeAlert(String data) {
try {
getDriver().switchTo().alert().sendKeys(data);
} catch (NoAlertPresentException e) {
reportStep("There is no alert present.", "fail", false);
} catch (WebDriverException e) {
reportStep("WebDriverException : \n" + e.getMessage(), "fail", false);
}
}
@Override
public void switchToWindow(int index) {
try {
Set<String> allWindows = getDriver().getWindowHandles();
List<String> allhandles = new ArrayList<String>(allWindows);
getDriver().switchTo().window(allhandles.get(index));
reportStep("The Window With index: " + index + " switched successfully", "info", false);
reportStep(getDriver().getTitle(), "info");
} catch (NoSuchWindowException e) {
reportStep("The Window With index: " + index + " not found\n" + e.getMessage(), "fail", false);
} catch (Exception e) {
reportStep("The Window With index: " + index + " not found\n" + e.getMessage(), "fail", false);
}
}
@Override
public boolean switchToWindow(String title) {
try {
Set<String> allWindows = getDriver().getWindowHandles();
for (String eachWindow : allWindows) {
getDriver().switchTo().window(eachWindow);
if (getDriver().getTitle().equals(title)) {
break;
}
}
reportStep("The Window With Title: " + title + "is switched ", "info");
return true;
} catch (NoSuchWindowException e) {
reportStep("The Window With Title: " + title + " not found", "fail", false);
}
return false;
}
@Override
public void switchToFrame(int index) {
try {
Thread.sleep(100);
getDriver().switchTo().frame(index);
} catch (NoSuchFrameException e) {
reportStep("No such frame " + e.getMessage(), "warning", false);
} catch (Exception e) {
reportStep("No such frame " + e.getMessage(), "fail", false);
}
}
@Override
public void switchToFrame(WebElement ele) {
try {
getDriver().switchTo().frame(ele);
} catch (NoSuchFrameException e) {
reportStep("No such frame " + e.getMessage(), "fail", false);
} catch (Exception e) {
reportStep("No such frame " + e.getMessage(), "fail", false);
}
}
public void switchToFrameUsingXPath(String xpath) {
try {
getDriver().switchTo().frame(locateElement(Locators.XPATH,xpath));
} catch (NoSuchFrameException e) {
//reportStep("No such frame " + e.getMessage(), "warning", false);
} catch (Exception e) {
//reportStep("No such frame " + e.getMessage(), "fail", false);
}
}
@Override
public void switchToFrame(String idOrName) {
try {
getDriver().switchTo().frame(idOrName);
} catch (NoSuchFrameException e) {
reportStep("No such frame " + e.getMessage(), "fail", false);
} catch (Exception e) {
reportStep("No such frame " + e.getMessage(), "fail", false);
}
}
@Override
public void defaultContent() {
try {
getDriver().switchTo().defaultContent();
} catch (Exception e) {
reportStep("No such window " + e.getMessage(), "fail", false);
}
}
@Override
public boolean verifyUrl(String url) {
if (getDriver().getCurrentUrl().equals(url)) {
reportStep("The url: " + url + " matched successfully", "info");
return true;
} else {
reportStep("The url: " + url + " not matched", "fail");
}
return false;
}
@Override
public boolean verifyTitle(String title) {
if (getDriver().getTitle().equals(title)) {
reportStep("Page title: " + title + " matched successfully", "info");
return true;
} else {
reportStep("Page url: " + title + " not matched", "fail");
}
return false;
}
@Override
public long takeSnap() {
long number = (long) Math.floor(Math.random() * 900000000L) + 10000000L;
try {
FileUtils.copyFile(getDriver().getScreenshotAs(OutputType.FILE),
new File("./"+Reporter.folderName+"/images/" + number + ".jpg"));
} catch (WebDriverException e) {
reportStep("The browser has been closed." + e.getMessage(), "fail");
} catch (IOException e) {
reportStep("The snapshot could not be taken " + e.getMessage(), "warning");
}
return number;
}
@Override
public void close() {
try {
getDriver().close();
reportStep("Browser is closed", "info", false);
} catch (Exception e) {
reportStep("Browser cannot be closed " + e.getMessage(), "fail", false);
}
}
@Override
public void quit() {
try {
getDriver().quit();
reportStep("Browser is closed", "info", false);
} catch (Exception e) {
reportStep("Browser cannot be closed " + e.getMessage(), "fail", false);
}
}
public void waitForDisapperance(WebElement element) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
WebDriverWait wait = new WebDriverWait(getDriver(), 10);
wait.until(ExpectedConditions.invisibilityOf(element));
} catch (Exception e) {
reportStep("Element did not appear after 10 seconds", "fail", false);
}
}
public void pause(int timeout) {
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void chooseDate(WebElement ele, String data) {
try {
getDriver().executeScript("arguments[0].setAttribute('value', '" + data +"')", ele);
reportStep("The Data :" + data + " entered Successfully", "pass");
} catch (ElementNotInteractableException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
} catch (WebDriverException e) {
reportStep("The Element " + ele + " is not Interactable \n" + e.getMessage(), "fail");
}
}
public void fileUpload(WebElement ele, String data) {
try {
hoverAndClick(ele);
pause(2000);
// Store the copied text in the clipboard
StringSelection stringSelection = new StringSelection(data);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
// Paste it using Robot class
Robot robot = new Robot();
// Enter to confirm it is uploaded
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
reportStep("The file is selected Successfully", "pass");
} catch (Exception e) {
reportStep("The file is not selected Successfully", "fail");
}
}
public void fileUploadWithJs(WebElement ele, String data) {
try {
clickUsingJs(ele);;
pause(2000);
// Store the copied text in the clipboard
StringSelection stringSelection = new StringSelection(data);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
// Paste it using Robot class
Robot robot = new Robot();
// Enter to confirm it is uploaded
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
reportStep("The file is selected Successfully", "pass");
} catch (Exception e) {
reportStep("The file is not selected Successfully", "fail");
}
}
@Override
public void executeTheScript(String js, WebElement ele) {
getDriver().executeScript(js, ele);
}
public static void generateProjectAndAuctionIds() {
int ranNum1 = (int) (Math.random() * 10000);
int ranNum2 = ranNum1+9999;
projectId = Integer.toString(ranNum1);
auctionRef = Integer.toString(ranNum2);
}
}
Browser
package com.framework.selenium.api.design;
import java.net.MalformedURLException;
import java.util.List;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.NoSuchWindowException;
import org.openqa.selenium.WebElement;
public interface Browser {
/**
* This method will launch the Chrome browser and
* maximise the browser and set the wait for 30 seconds
* and load the url
* @param url - This will load the specified url `
* @author Sarath - TestLeaf
* @throws MalformedURLException
*/
public void startApp(String url, boolean headless);
/**
* This method will launch the Any browser and
* maximise the browser and set the wait for 30 seconds
* and load the url
* @param browser - This will load the specified browser
* @param url - This will load the specified url
* @author Sarath - TestLeaf
* @throws MalformedURLException
*/
public void startApp(String browser, boolean headless, String url);
/**
* This method will locate the element using any given locator
* @param locatorType - The locator by which the element to be found
* @param locValue - The locator value by which the element to be found
* @author Sarath - TestLeaf
* @throws NoSuchElementException
* @return The first matching element on the current context.
*/
public WebElement locateElement(Locators locatorType, String value);
/**
* This method will locate the element using id
* @param locValue - The locator value by which the element to be found
* @author Sarath - TestLeaf
* @throws NoSuchElementException
* @return The first matching element on the current context.
*/
public WebElement locateElement(String value);
/**
* This method will locates all matching element using any given locator
* @param locatorType - The locator by which the element to be found
* @param locValue - The locator value by which the element to be found
* @author Sarath - TestLeaf
* @return A list of all WebElements, or an empty list if nothing matches.
*/
public List<WebElement> locateElements(Locators locatorType, String value);
/**
* This method will switch to the Alert
* @author Sarath - TestLeaf
* @return NoAlertPresentException
*/
public void switchToAlert();
/**
* This method will accept the alert opened
* @author Sarath - TestLeaf
* @throws NoAlertPresentException
*/
public void acceptAlert();
/**
* This method will dismiss the alert opened
* @author Sarath - TestLeaf
* @throws NoAlertPresentException
*/
public void dismissAlert();
/**
* This method will return the text of the alert
* @author Sarath - TestLeaf
* @throws NoAlertPresentException
*/
public String getAlertText();
/**
* This method will enter the value in the alert
* @author Sarath - TestLeaf
* @param data- the data to be entered in alert
* @throws NoAlertPresentException
*/
public void typeAlert(String data);
/**
* This method will switch to the Window of interest
* @param index The window index to be switched to. 0 -> first window
* @author Sarath - TestLeaf
* @throws NoSuchWindowException
*/
public void switchToWindow(int index);
/**
* This method will switch to the Window of interest using its title
* @param title The window title to be switched to first window
* @author Sarath - TestLeaf
* @return
* @throws NoSuchWindowException
*/
public boolean switchToWindow(String title);
/**
* This method will switch to the specific frame using index
* @param index - The int (frame) to be switched
* @author Sarath - TestLeaf
* @throws NoSuchFrameException
*/
public void switchToFrame(int index);
/**
* This method will switch to the specific frame
* @param ele - The Webelement (frame) to be switched
* @author Sarath - TestLeaf
* @throws NoSuchFrameException, StaleElementReferenceException
*/
public void switchToFrame(WebElement ele);
/**
* This method will switch to the specific frame using Id (or) Name
* @param idOrName - The String (frame) to be switched
* @author Sarath - TestLeaf
* @throws NoSuchFrameException
*/
public void switchToFrame(String idOrName);
/**
* This method will switch to the first frame on the page
* @author Sarath - TestLeaf
* @return This driver focused on the top window/first frame.
*/
public void defaultContent();
/**
* This method will verify browser actual url with expected
* @param url - The url to be checked
* @author Sarath - TestLeaf
* @return true if the given object represents a String equivalent to this url, false otherwise
*/
public boolean verifyUrl(String url);
/**
* This method will verify browser actual title with expected
* @param title - The expected title of the browser
* @author Sarath - TestLeaf
* @return true if the given object represents a String equivalent to this title, false otherwise
*/
public boolean verifyTitle(String title);
/**
* This method will take snapshot of the browser
* @author Sarath - TestLeaf
* @return Object in which is stored information about the screenshot.
* @throws WebDriverException
*/
/**
* This method will close the active browser
* @author Sarath - TestLeaf
*/
public void close();
/**
* This method will close all the browsers
* @author Sarath - TestLeaf
*/
public void quit();
}
Element.java
package com.framework.selenium.api.design;
import org.openqa.selenium.InvalidElementStateException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
public interface Element {
void executeTheScript(String js, WebElement ele);
/**
* This method will click the element and take snap
* @param ele - The Webelement (button/link/element) to be clicked
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @throws StaleElementReferenceException
*/
void click(WebElement ele);
/**
* This method will enter the value in the given text field
* @param ele - The Webelement (text field) in which the data to be entered
* @param data - The data to be sent to the webelement
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @throws ElementNotInteractable,IllegalArgumentException(throws if keysToSend is null)
*/
void append(WebElement ele, String data);
/**
* This method will clear the value in the given text field
* @param ele - The Webelement (text field) in which the data to be entered
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @throws InvalidElementStateException (throws if not user-editable element)
*/
void clear(WebElement ele);
/**
* This method will clear and type the value in the given text field
* @param ele - The Webelement (text field) in which the data to be entered
* @param data - The data to be sent to the webelement
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @throws ElementNotInteractable,IllegalArgumentException(throws if keysToSend is null)
*/
void clearAndType(WebElement ele,String data);
/**
* This method will get the visible text of the element
* @param ele - The Webelement (button/link/element) in which text to be retrieved
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
*/
String getElementText(WebElement ele);
/**
* This method will get the Color values of the element
* @param ele - The Webelement (button/link/element) in which text to be retrieved
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @return The visible text of this element.
*/
String getBackgroundColor(WebElement ele);
/**
* This method will get the text of the element textbox
* @param ele - The Webelement (button/link/element) in which text to be retrieved
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @return The attribute/property's current value (or) null if the value is not set.
*/
String getTypedText(WebElement ele);
/**
* This method will select the drop down visible text
* @param ele - The Webelement (dropdown) to be selected
* @param value The value to be selected (visibletext) from the dropdown
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @throws NoSuchElementException
*/
void selectDropDownUsingText(WebElement ele, String value) ;
/**
* This method will select the drop down using index
* @param ele - The Webelement (dropdown) to be selected
* @param index The index to be selected from the dropdown
* @see locateElement method in Browser Class
* @author Babu - TestLeaf
* @throws NoSuchElementException
*/
void selectDropDownUsingIndex(WebElement ele, int index) ;
/**
* This method will select the drop down using index
* @param ele - The Webelement (dropdown) to be selected
* @param value - The value to be selected (value) from the dropdown
* @see locateElement method in Browser Class
* @author Sarath - TestLeaf
* @throws NoSuchElementException
*/
void selectDropDownUsingValue(WebElement ele, String value) ;
/**
* This method will verify exact given text with actual text on the given element
* @param ele - The Webelement in which the text to be need to be verified
* @param expectedText - The expected text to be verified
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
* @return true if the given object represents a String equivalent to this string, false otherwise
*/
boolean verifyExactText(WebElement ele, String expectedText);
/**
* This method will verify given text contains actual text on the given element
* @param ele - The Webelement in which the text to be need to be verified
* @param expectedText - The expected text to be verified
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
* @return true if this String represents the same sequence of characters as the specified string, false otherwise
*/
boolean verifyPartialText(WebElement ele, String expectedText);
/**
* This method will verify exact given attribute's value with actual value on the given element
* @param ele - The Webelement in which the attribute value to be need to be verified
* @param attribute - The attribute to be checked (like value, href etc)
* @param value - The value of the attribute
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
* @return true if this String represents the same sequence of characters as the specified value, false otherwise
*/
boolean verifyExactAttribute(WebElement ele, String attribute, String value);
/**
* This method will verify partial given attribute's value with actual value on the given element
* @param ele - The Webelement in which the attribute value to be need to be verified
* @param attribute - The attribute to be checked (like value, href etc)
* @param value - The value of the attribute
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
* @return true if this String represents the same sequence of characters as the specified value, false otherwise
*
*/
void verifyPartialAttribute(WebElement ele, String attribute, String value);
/**
* This method will verify if the element is visible in the DOM
* @param ele - The Webelement to be checked
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
* @return true if the element is displayed or false otherwise
*/
boolean verifyDisplayed(WebElement ele);
/**
* This method will checking the element to be invisible
* @param ele - The Webelement to be checked
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
*/
boolean verifyDisappeared(WebElement ele);
/**
* This method will verify if the input element is Enabled
* @param ele - The Webelement (Radio button, Checkbox) to be verified
* @return true - if the element is enabled else false
* @author Sarath - TestLeaf
*
* @see locateElement method in Browser Class
* @return True if the element is enabled, false otherwise.
*/
boolean verifyEnabled(WebElement ele);
/**
* This method will verify if the element (Radio button, Checkbox) is selected
* @param ele - The Webelement (Radio button, Checkbox) to be verified
* @author Sarath - TestLeaf
* @see locateElement method in Browser Class
* @return True if the element is currently selected or checked, false otherwise.
*/
boolean verifySelected(WebElement ele);
}
package com.framework.selenium.api.design;
public enum Locators {
ID, XPATH, CLASS_NAME, NAME, CSS, LINK_TEXT, PARTIAL_LINKTEXT, TAGNAME
}