Last active
August 17, 2017 11:15
-
-
Save erajanraja24/36596754a27f6c7e83d90c07f85f473e to your computer and use it in GitHub Desktop.
Wrapper for WebElement controls Automation script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.AWTException; | |
import java.awt.Robot; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.StringSelection; | |
import java.awt.event.KeyEvent; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.commons.lang.StringUtils; | |
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.chrome.ChromeOptions; | |
import org.openqa.selenium.support.ui.Select; | |
public class Wrapper { | |
public static WebDriver driver; | |
//To launch the browser | |
public static void launchBrowser(String browsename) | |
{ | |
if (browsename.equals("Chrome")) | |
{ | |
System.setProperty("webdriver.chrome.driver", | |
"chromedriver.exe"); | |
driver = new ChromeDriver(); | |
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); | |
} | |
} | |
public static void launchBrowserWithExtenstion(String browsername,String pathToExtension) | |
{ | |
if (browsername.equals("Chrome")) | |
{ | |
System.setProperty("webdriver.chrome.driver", | |
"chromedriver.exe"); | |
ChromeOptions options = new ChromeOptions(); | |
options.addArguments("load-extension=" +pathToExtension); | |
driver = new ChromeDriver(options); | |
sleep(4500); | |
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); | |
} | |
} | |
//Navigate to Website | |
public static void navigateTo(String url) | |
{ | |
driver.get(url); | |
} | |
public static boolean elementPresent(String attribute,String value) | |
{ | |
boolean elementPresent=false; | |
switch(attribute) | |
{ | |
case "id": | |
driver.findElement(By.id(value)).isDisplayed(); | |
elementPresent=true; | |
break; | |
case "xpath": | |
driver.findElement(By.xpath(value)).isDisplayed(); | |
elementPresent=true; | |
break; | |
case "name": | |
driver.findElement(By.name(value)).isDisplayed(); | |
elementPresent=true; | |
break; | |
case "className": | |
driver.findElement(By.name(value)).isDisplayed(); | |
elementPresent=true; | |
break; | |
case "linkText": | |
driver.findElement(By.linkText(value)).isDisplayed(); | |
elementPresent=true; | |
break; | |
} | |
return elementPresent; | |
} | |
//To enter a text in Textbox | |
public static void enterTextInTextbox(String attribute,String value,String textEnter) | |
{ | |
switch(attribute) | |
{ | |
case "id": | |
//driver.findElement(By.id(value)).clear(); | |
driver.findElement(By.id(value)).sendKeys(textEnter);; | |
break; | |
case "className": | |
//driver.findElement(By.className(value)).clear(); | |
driver.findElement(By.className(value)).sendKeys(textEnter);; | |
break; | |
case "xpath": | |
driver.findElement(By.xpath(value)).clear(); | |
driver.findElement(By.xpath(value)).sendKeys(textEnter);; | |
break; | |
case "name": | |
driver.findElement(By.name(value)).clear(); | |
driver.findElement(By.name(value)).sendKeys(textEnter);; | |
break; | |
} | |
} | |
//To submit a form | |
public static void submit(String attribute,String value) | |
{ | |
switch(attribute) | |
{ | |
case "id": | |
driver.findElement(By.id(value)).submit(); | |
break; | |
case "className": | |
driver.findElement(By.className(value)).submit(); | |
break; | |
case "xpath": | |
driver.findElement(By.xpath(value)).submit(); | |
break; | |
case "name": | |
driver.findElement(By.name(value)).submit(); | |
break; | |
} | |
} | |
//To click on a element | |
public static void click(String attribute,String value) | |
{ | |
//WebElement clickElement; | |
switch(attribute) | |
{ | |
case "id": | |
driver.findElement(By.id(value)).click(); | |
break; | |
case "className": | |
driver.findElement(By.className(value)).click(); | |
break; | |
case "xpath": | |
driver.findElement(By.xpath(value)).click(); | |
break; | |
case "linkText": | |
driver.findElement(By.linkText(value)).click(); | |
break; | |
case "name": | |
driver.findElement(By.name(value)).click(); | |
break; | |
} | |
} | |
//To click on link Text | |
public static void clickByLinkText(String linkText) | |
{ | |
driver.findElement(By.linkText(linkText)).click(); | |
} | |
//To click on parital Link Text | |
public static void clickByPartialLinkText(String linkText) | |
{ | |
driver.findElement(By.partialLinkText(linkText)).click(); | |
} | |
//To select values from ul Li Dropdown control | |
public static void selectTextFromLiDropDown(String attribute,String value,String textNeedToSelect) | |
{ | |
switch(attribute) | |
{ | |
case "className": | |
for (WebElement opt : driver.findElements(By.className(value))) | |
{ | |
System.out.println(opt.getText()+" "+opt); | |
if (opt.getText().equalsIgnoreCase(textNeedToSelect)) | |
{ | |
//opt.getText() | |
System.out.println(opt.getText()+textNeedToSelect); | |
opt.click(); | |
break; | |
} | |
} | |
break; | |
case "xpath": | |
for (WebElement opt : driver.findElements(By.xpath(value))) | |
{ | |
//System.out.println(opt.getText()+" "+opt); | |
if (containsIgnoreCase(opt.getText(),textNeedToSelect)) | |
{ | |
//opt.getText().equalsIgnoreCase(textNeedToSelect) | |
opt.click(); | |
break; | |
} | |
} | |
break; | |
case "id": | |
for (WebElement opt : driver.findElements(By.id(value))) | |
{ | |
//System.out.println(opt.getText()+" "+opt); | |
if (opt.getText().contains(textNeedToSelect)) | |
{ | |
opt.click(); | |
break; | |
} | |
} | |
break; | |
case "name": | |
for (WebElement opt : driver.findElements(By.name(value))) | |
{ | |
System.out.println(opt.getText()+" "+opt); | |
if (opt.getText().contains(textNeedToSelect)) | |
{ | |
opt.click(); | |
break; | |
} | |
} | |
break; | |
} | |
} | |
//To select Radio Button control | |
public static void selectRadioButton(String attribute,String value) | |
{ | |
switch(attribute) | |
{ | |
case "id": | |
driver.findElement(By.id(value)).click(); | |
break; | |
case "className": | |
driver.findElement(By.className(value)).click(); | |
break; | |
case "xpath": | |
driver.findElement(By.xpath(value)).click(); | |
break; | |
} | |
} | |
//To select visible text from Normal Dropdown | |
public static void selectTextFromNormalDropDown(String attribute,String value,String textNeedToSelect) | |
{ | |
switch(attribute) | |
{ | |
case "id": | |
new Select(driver.findElement(By.id(value))).selectByVisibleText(textNeedToSelect); | |
break; | |
case "className": | |
new Select(driver.findElement(By.className(value))).selectByVisibleText(textNeedToSelect); | |
break; | |
case "xpath": | |
new Select(driver.findElement(By.xpath(value))).selectByVisibleText(textNeedToSelect); | |
break; | |
case "name": | |
new Select(driver.findElement(By.name(value))).selectByVisibleText(textNeedToSelect); | |
break; | |
} | |
} | |
//To select value from Normal Dropdown if the Text is String | |
public static void selectvalueFromNormalDropDown(String attribute,String value,String textNeedToSelect) | |
{ | |
switch(attribute) | |
{ | |
case "id": | |
new Select(driver.findElement(By.id(value))).selectByValue(textNeedToSelect);; | |
break; | |
case "className": | |
new Select(driver.findElement(By.className(value))).selectByValue(textNeedToSelect); | |
break; | |
case "xpath": | |
new Select(driver.findElement(By.xpath(value))).selectByValue(textNeedToSelect); | |
break; | |
case "name": | |
new Select(driver.findElement(By.name(value))).selectByValue(textNeedToSelect); | |
break; | |
} | |
} | |
@SuppressWarnings("unused") | |
public static boolean verifyTextFromNormalDropDown(String attribute,String value,String textNeedToVerify) | |
{ | |
switch(attribute) | |
{ | |
case "id": | |
Select oSelect1 = new Select(driver.findElement(By.id(value))); | |
List <WebElement> elementCount1 = oSelect1.getOptions(); | |
int iSize1 = elementCount1.size(); | |
System.out.println("Size is "+iSize1); | |
for(int i =0; i<iSize1 ; i++) | |
{ | |
String sValue = elementCount1.get(i).getText(); | |
System.out.println(sValue); | |
if(containsIgnoreCase(sValue, textNeedToVerify)) | |
{ | |
return true; | |
} | |
} | |
break; | |
case "className": | |
Select oSelect2 = new Select(driver.findElement(By.className(value))); | |
List <WebElement> elementCount2 = oSelect2.getOptions(); | |
int iSize2 = elementCount2.size(); | |
for(int i =0; i<iSize2 ; i++){ | |
String sValue = elementCount2.get(i).getText(); | |
if(containsIgnoreCase(sValue, textNeedToVerify)) | |
{ | |
return true; | |
} | |
} | |
break; | |
case "xpath": | |
Select oSelect3 = new Select(driver.findElement(By.xpath(value))); | |
List <WebElement> elementCount3 = oSelect3.getOptions(); | |
int iSize3 = elementCount3.size(); | |
for(int i =0; i<iSize3 ; i++){ | |
String sValue = elementCount3.get(i).getText(); | |
if(containsIgnoreCase(sValue, textNeedToVerify)) | |
{ | |
return true; | |
} | |
} | |
break; | |
case "name": | |
Select oSelect4 = new Select(driver.findElement(By.name(value))); | |
List <WebElement> elementCount4 = oSelect4.getOptions(); | |
int iSize4 = elementCount4.size(); | |
for(int i =0; i<iSize4 ; i++){ | |
String sValue = elementCount4.get(i).getText(); | |
if(containsIgnoreCase(sValue, textNeedToVerify)) | |
{ | |
return true; | |
} | |
} | |
break; | |
default: | |
return false; | |
} | |
return false; | |
} | |
//To maximize the Window | |
public static void maximizeTheBrowserWindow() | |
{ | |
driver.manage().window().maximize(); | |
} | |
//To sleep for Milliseconds | |
public static void sleep(long time) | |
{ | |
try { | |
Thread.sleep(time); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
//To Upload a file from Desktop | |
public static void uploadFile(String attribute,String value,String fileLocation) | |
{ | |
switch(attribute) | |
{ | |
case "id": | |
driver.findElement(By.id(value)).click(); | |
sleep(1500); | |
uploadFileTasker(fileLocation); | |
sleep(1500); | |
break; | |
case "className": | |
driver.findElement(By.className(value)).click(); | |
sleep(1500); | |
uploadFileTasker(fileLocation); | |
sleep(1500); | |
break; | |
case "xpath": | |
driver.findElement(By.xpath(value)).click(); | |
sleep(1500); | |
uploadFileTasker(fileLocation); | |
sleep(1500); | |
break; | |
case "linkText": | |
driver.findElement(By.linkText(value)).click(); | |
sleep(1500); | |
uploadFileTasker(fileLocation); | |
sleep(1500); | |
break; | |
} | |
} | |
public static void uploadFileTasker(String fileLocation) | |
{ | |
try { | |
setClipboardData(fileLocation); | |
sleep(500); | |
Robot robot = new Robot(); | |
robot.keyPress(KeyEvent.VK_CONTROL); | |
robot.keyPress(KeyEvent.VK_V); | |
robot.keyRelease(KeyEvent.VK_V); | |
robot.keyRelease(KeyEvent.VK_CONTROL); | |
robot.keyPress(KeyEvent.VK_ENTER); | |
robot.keyRelease(KeyEvent.VK_ENTER); | |
sleep(1500); | |
} catch (Exception exp) | |
{ | |
exp.printStackTrace(); | |
} | |
} | |
public static void setClipboardData(String string) | |
{ | |
StringSelection stringSelection = new StringSelection(string); | |
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null); | |
} | |
//To move the WebPage down | |
public static void pageDown() | |
{ | |
Robot robot; | |
try { | |
robot = new Robot(); | |
robot.keyPress(KeyEvent.VK_PAGE_DOWN); | |
robot.keyRelease(KeyEvent.VK_PAGE_DOWN); | |
} catch (AWTException e) { | |
e.printStackTrace(); | |
} | |
} | |
//To close the browser | |
public static void close() | |
{ | |
driver.close(); | |
} | |
//To press down button | |
public static void pressDown() | |
{ | |
Robot robot; | |
try { | |
robot = new Robot(); | |
robot.keyPress(KeyEvent.VK_DOWN); | |
robot.keyRelease(KeyEvent.VK_DOWN); | |
} catch (AWTException e) { | |
e.printStackTrace(); | |
} | |
} | |
//TO press Enter | |
public static void pressEnter() | |
{ | |
Robot robot; | |
try { | |
robot = new Robot(); | |
robot.keyPress(KeyEvent.VK_ENTER); | |
robot.keyRelease(KeyEvent.VK_ENTER); | |
} catch (AWTException e) { | |
e.printStackTrace(); | |
} | |
} | |
//To check the contains with Ignore Case | |
public static boolean containsIgnoreCase(String str1,String str2) | |
{ | |
String str3,str4; | |
str3=str1.toLowerCase(); | |
str4=str2.toLowerCase(); | |
if(str3.contains(str4)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
//To send a text using Robot | |
public static void sendText(String textNeedToSend) throws AWTException | |
{ | |
Robot robot = new Robot(); | |
setClipboardData(textNeedToSend); | |
robot.keyPress(KeyEvent.VK_CONTROL); | |
robot.keyPress(KeyEvent.VK_V); | |
robot.keyRelease(KeyEvent.VK_V); | |
robot.keyRelease(KeyEvent.VK_CONTROL); | |
robot.keyPress(KeyEvent.VK_ENTER); | |
robot.keyRelease(KeyEvent.VK_ENTER); | |
} | |
//Switch to child Window upon providing Parent Window handle as parameter | |
public static void switchToChildWindow(String parentWindowHandle) | |
{ | |
for (String childWindowHandle : driver.getWindowHandles()) | |
{ | |
if(!childWindowHandle.equals(parentWindowHandle)) | |
{ | |
driver.switchTo().window(childWindowHandle); | |
} | |
} | |
} | |
//Switch to Parent Window upon providing parent window handle as parameter | |
public static void switchToParentWindow(String parentWindowHandle) | |
{ | |
driver.switchTo().window(parentWindowHandle); | |
} | |
//Close the child Window Upon providing parent window handle as parameter | |
public static void closeChildWindow(String parentWindowHandle) | |
{ | |
for (String childWindowHandle : driver.getWindowHandles()) | |
{ | |
if(!childWindowHandle.equals(parentWindowHandle)) | |
{ | |
driver.switchTo().window(childWindowHandle); | |
driver.close(); | |
} | |
} | |
} | |
//Switch to the given frame | |
public static void switchToFrame(String frameID) | |
{ | |
driver.switchTo().frame(frameID); | |
} | |
//To get the table value right below the header | |
@SuppressWarnings("unused") | |
public static String getTableText(String xpath,String tableHeader) | |
{ | |
WebElement mytable=driver.findElement(By.xpath(xpath)); | |
List<WebElement> rows_table = mytable.findElements(By.tagName("tr")); | |
//To calculate no of rows In table. | |
int rows_count = rows_table.size(); | |
//Loop will execute till the last row of table. | |
for (int row=0; row<rows_count; row++) | |
{ | |
List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td")); | |
List<WebElement> header = rows_table.get(row).findElements(By.tagName("th")); | |
//To calculate no of columns (cells). In that specific row. | |
int columns_count = Columns_row.size(); | |
int header_count=header.size(); | |
for (int column=0; column<header_count; column++) | |
{ | |
// To retrieve text from that specific cell. | |
if(header.get(column).getText().contains(tableHeader)) | |
{ | |
String tableValue=rows_table.get(row).findElements(By.tagName("td")).get(column).getText(); | |
return tableValue; | |
} | |
} | |
} | |
return "Not Found"; | |
} | |
//To capitalizse the first letter in the set of given words | |
@SuppressWarnings("deprecation") | |
public static String convertToFirstUpperCase(String str) | |
{ | |
return StringUtils.capitaliseAllWords(str); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment