********************************************************
Important JavaScript Methods to cater selenium need
********************************************************
//HIGHLIGHT ELEMENT
public static void highlightElement(WebElement element, WebDriver driver){
JavascriptExecutor jex = (JavascriptExecutor) driver;
String bgcolor = element.getCssValue("backgroundColor");
for (int i =0;i<10;i++){
changeColor("rgb(0,200,0)",element,driver);
changeColor(bgcolor,element,driver);
}
//DRAWBORDER FOR AN ELEMENT
public static void drawBorder(WebElement element, WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].style.bordere3px solid red"',element);
}
//CLICK ON AN ELEMENT
public static void clickEleByJS(WebElement element, WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScriptrarguments(0).click();", element);
}
//REFRESH BROWSER
public static void refreshBrowser(WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("history.go(0)");
}
//FLASHING ELEMENT USING COLOR CHANGE LOGIC
public static void changeColor(String color, WebElement element, WebDriver driver){
JavascriptExecutor jex = (JavascriptExecutor) driver;
jex.executeScript("arguments[01.style.backgroundColor ="4-color+—, element);
try{
Thread.sleep(20);
} catch(InterruptedException e){
}
}
//GENERATE ALERT
public static void generateAlert(WebElement element, WebDriver driver,String message){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("alert('"+message+1");
}
//GET PAGE TITLE
public static String getTrtleByJs(WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
String Title = js.executeScript("return document.titleatoString(); return Title;)
//GET PAGE TEXT
public static String getPagelnnerTextByJs(WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
String PageText = js.executeScript("return document.documentElementinnerText;").toString();
return PageText;
}
//SCROLL PAGES
//SCROLL BY PIXEL
public static void scrollPageDown(WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScriptrwindow.scrollBy(0,600");
}
//SCROLL TO BOTTOM
public static void scrollPageDown(WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScriptcwindow.scrollTo(0,document.body.scrollheight");
}
//SCROLL INTO VIEW
public static void scrolllntoView(WebElement element,WebDriver driver){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScriptc[arguments[0].scrollIntoView(true);",element);
}
//SEND KEYS
public static void scrolllntoView(WebElement element,WebDriver driver){JavascriptExecuter executor = (JavascriptExecutor)driver;
executor.excuteScript(“document.getElementById(element).value=’new value’);
}
No comments:
Post a Comment