package automation.selenium;
import java.awt.AWTException;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.Select;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class actions implements actionsInterface {
private WebDriver driver;
actions() {
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\driver\\chromedriver.exe");
this.driver = new ChromeDriver();
}
void excute(String[] data) throws NumberFormatException, InterruptedException, AWTException, IOException {
String identifierType = data[0];
String identifie = data[1];
String action = data[2];
String actionData = data[3];
this.driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement element = null;
By ele = this.getElement(identifierType, identifie);
if (ele != null) {
try {
element = this.driver.findElement(ele);
} catch (Exception e) {
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("C:/selenium/error.png"));
this.driver.quit();
System.out.print("no such element: Unable to locate element: {method:"+identifierType+",selector :"+identifie+"}/n");
}
}
notifications notify = new notifications();
switch (action) {
case goTo:
this.driver.get(actionData);
this.driver.manage().window().maximize();
break;
case "quit":
this.driver.quit();
break;
case set:
element.sendKeys(actionData);
break;
case click:
element.click();
break;
case wait:
Thread.sleep(Integer.parseInt(actionData));
break;
case waitUntill:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(ele));
break;
case selectByValue:
Select listbox = new Select(element);
listbox.selectByValue(actionData);
break;
case selectByIndex:
Select select_list = new Select(element);
select_list.selectByIndex(Integer.parseInt(actionData));
break;
case assertEquals:
String eleText = element.getText();
if (!eleText.equals(actionData)) {
notify.displayTray("Text not matched");
}
break;
case assertContains:
String eleTextCon = element.getText();
if (!eleTextCon.contains(actionData)) {
notify.displayTray("Text not matched");
}
break;
}
}
By getElement(String identifierType, String identifier) {
By element = null;
switch (identifierType) {
case "id":
element = By.id(identifier);
break;
case "classname":
element = By.className(identifier);
break;
case "cssselector":
element = By.cssSelector(identifier);
break;
case "xpath":
element = By.xpath(identifier);
break;
case "linkText":
element = By.linkText(identifier);
break;
case "name":
element = By.name(identifier);
break;
}
return element;
}
}
import java.awt.AWTException;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.Select;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
public class actions implements actionsInterface {
private WebDriver driver;
actions() {
System.setProperty("webdriver.chrome.driver", "E:\\selenium\\driver\\chromedriver.exe");
this.driver = new ChromeDriver();
}
void excute(String[] data) throws NumberFormatException, InterruptedException, AWTException, IOException {
String identifierType = data[0];
String identifie = data[1];
String action = data[2];
String actionData = data[3];
this.driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement element = null;
By ele = this.getElement(identifierType, identifie);
if (ele != null) {
try {
element = this.driver.findElement(ele);
} catch (Exception e) {
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src, new File("C:/selenium/error.png"));
this.driver.quit();
System.out.print("no such element: Unable to locate element: {method:"+identifierType+",selector :"+identifie+"}/n");
}
}
notifications notify = new notifications();
switch (action) {
case goTo:
this.driver.get(actionData);
this.driver.manage().window().maximize();
break;
case "quit":
this.driver.quit();
break;
case set:
element.sendKeys(actionData);
break;
case click:
element.click();
break;
case wait:
Thread.sleep(Integer.parseInt(actionData));
break;
case waitUntill:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(ele));
break;
case selectByValue:
Select listbox = new Select(element);
listbox.selectByValue(actionData);
break;
case selectByIndex:
Select select_list = new Select(element);
select_list.selectByIndex(Integer.parseInt(actionData));
break;
case assertEquals:
String eleText = element.getText();
if (!eleText.equals(actionData)) {
notify.displayTray("Text not matched");
}
break;
case assertContains:
String eleTextCon = element.getText();
if (!eleTextCon.contains(actionData)) {
notify.displayTray("Text not matched");
}
break;
}
}
By getElement(String identifierType, String identifier) {
By element = null;
switch (identifierType) {
case "id":
element = By.id(identifier);
break;
case "classname":
element = By.className(identifier);
break;
case "cssselector":
element = By.cssSelector(identifier);
break;
case "xpath":
element = By.xpath(identifier);
break;
case "linkText":
element = By.linkText(identifier);
break;
case "name":
element = By.name(identifier);
break;
}
return element;
}
}
/*
* actions actions = new actions(); actions.excute("id", "identifierId", "goto",
* "http://www.gmail.com"); actions.excute("id", "identifierId", "set",
* "zafar.hayat151@gmail.com");
*/
}
}
Comments
Post a Comment