How to use Interface JavascriptExecutor class of org.openqa.selenium package

Best Selenium code snippet using org.openqa.selenium.Interface JavascriptExecutor

Source:BaseClass.java Github

copy

Full Screen

1package org.testng.test;2import java.awt.AWTException;3import java.awt.Robot;4import java.awt.event.KeyEvent;5import java.io.File;6import java.io.IOException;7import java.util.concurrent.TimeUnit;8import org.apache.commons.io.FileUtils;9import org.openqa.selenium.JavascriptExecutor;10import org.openqa.selenium.OutputType;11import org.openqa.selenium.TakesScreenshot;12import org.openqa.selenium.WebDriver;13import org.openqa.selenium.WebElement;14import org.openqa.selenium.chrome.ChromeDriver;15import org.openqa.selenium.interactions.Actions;16import org.openqa.selenium.support.ui.Select;17public class BaseClass {18 static WebDriver driver;19 static WebElement element;20 21/*===============================WebDriverInterface Abstract-methods====================================*/22 public static void browserLanch() {23 System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");24 driver = new ChromeDriver();25 driver.manage().window().maximize();26 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);27 }28 public static void launchUrl(String url) {29 driver.get(url);30 }31 public static void quit() {32 driver.quit();33 }34 public static void close() {35 driver.close();36 }37 public static void currentUrl() {38 driver.getCurrentUrl();39 }40 public static void title() {41 driver.getTitle();42 }43/*======================================================================================================*/44 45 46/*================================WebElementInterface Abstract-methods===================================*/47 public static void sendkey(WebElement e,String value) {48 e.sendKeys(value);49 }50 public static void clickButton(WebElement e) {51 e.click();52 } 53 public static void getText (WebElement e) {54 String attribute = e.getAttribute("value");55 System.out.println(attribute);56 }57/*=====================================================================================================*/58 59/*================================TakesScreenshotInterface Abstract-method=============================*/60 public static void screenshot(String filelocation) {61 62 TakesScreenshot ts=(TakesScreenshot)driver;63 File temp = ts.getScreenshotAs(OutputType.FILE);64 File perm = new File(filelocation);65 try {66 FileUtils.copyFile(temp, perm);67 } 68 catch (IOException e) { 69 e.printStackTrace();70 }71 }72/*====================================================================================================*/73 74/*============================================SelectClass methods======================================*/75 public static void selectValue(WebElement e, String value) {76 Select s=new Select(e);77 s.selectByValue(value);78 }79 public static void selectIndex(WebElement e, int index) {80 Select s=new Select(e);81 s.selectByIndex(index);82 }83 public static void selectVisbleText(WebElement e, String text) {84 Select s=new Select(e);85 s.selectByVisibleText(text);86 }87 public static void deselectValue(WebElement e, String value) {88 Select s=new Select(e);89 s.deselectByValue(value);90 }91 public static void deselectIndex(WebElement e, int index) {92 Select s=new Select(e);93 s.deselectByIndex(index);94 }95 public static void deselectVisbleText(WebElement e, String text) {96 Select s=new Select(e);97 s.deselectByVisibleText(text);98 }99 public static void deSelectAll(WebElement e) {100 Select s=new Select(e);101 s.deselectAll();102 }103 public static void getAllSelectedOptions(WebElement e, String text) {104 Select s=new Select(e);105 s.getAllSelectedOptions();106 }107 public static void getFirstSelectedOptions(WebElement e) {108 Select s=new Select(e);109 s.getFirstSelectedOption();110 }111 public static void getOptions(WebElement e) {112 Select s=new Select(e);113 s.getOptions();114 }115/*=====================================================================================================*/116 117 118/*=======================================ActionsClass methods==========================================*/119 public static void moveCursor(WebElement e) {120 Actions a = new Actions(driver);121 a.moveToElement(e).perform();122 }123 public static void doubleClick() {124 Actions a = new Actions(driver);125 a.doubleClick().perform();126 }127 public static void contextClick() {128 Actions a = new Actions(driver);129 a.contextClick().perform();130 }131 public static void dragAndDrop(WebElement source,WebElement target) {132 Actions a = new Actions(driver);133 a.dragAndDrop(source, target).perform();134 } 135/*=====================================================================================================*/136 137/*=============================JavascriptExecutorInterface Abstract-methods=============================*/138 public static void scrollDown(WebElement e) {139 JavascriptExecutor js=(JavascriptExecutor)driver;140 js.executeScript("arguments[0].scrollIntoView(true)", e);141 }142 public static void scrollUp(WebElement e) {143 JavascriptExecutor js=(JavascriptExecutor)driver;144 js.executeScript("arguments[0].scrollIntoView(false)", e);145 }146 public static void jsClick(WebElement e) {147 JavascriptExecutor js=(JavascriptExecutor)driver;148 js.executeScript("arguments[0].click()", e);149 }150 public static void jsSendKey(String value1,WebElement e) {151 JavascriptExecutor js=(JavascriptExecutor)driver;152 js.executeScript("arguments[0].setAttribute('value','"+value1+"');", e);153 }154 public static void jsGetKey(WebElement e) {155 JavascriptExecutor js=(JavascriptExecutor)driver;156 js.executeScript("arguments[0].setAttribute('value')", e);157 }158/*=====================================================================================================*/159 160 161/*===================================RobotClass methods================================================*/ 162 public static void robotDown(int howmanytime) {163 try {164 Robot r=new Robot();165 while(howmanytime>0) {166 r.keyPress(KeyEvent.VK_DOWN);167 r.keyRelease(KeyEvent.VK_DOWN);168 howmanytime--;169 }170 } catch (AWTException e) {171 e.printStackTrace();172 }173 }174 public static void robotUp(int howmanytime) {175 try {176 Robot r=new Robot();177 while(howmanytime>0) {178 r.keyPress(KeyEvent.VK_UP);179 r.keyRelease(KeyEvent.VK_UP);180 howmanytime--;181 }182 } catch (AWTException e) {183 e.printStackTrace();184 }185 }186 public static void robotEnter() {187 188 try {189 Robot r = new Robot();190 r.keyPress(KeyEvent.VK_ENTER);191 r.keyRelease(KeyEvent.VK_ENTER);192 } catch (AWTException e) {193 e.printStackTrace();194 }195 }196 public static void robotCopy() {197 try {198 Robot r = new Robot();199 r.keyPress(KeyEvent.VK_CONTROL);200 r.keyPress(KeyEvent.VK_C);201 r.keyRelease(KeyEvent.VK_CONTROL);202 r.keyRelease(KeyEvent.VK_C);203 } catch (AWTException e) {204 e.printStackTrace();205 }206 }207 public static void robotPaste() {208 try {209 Robot r = new Robot();210 r.keyPress(KeyEvent.VK_CONTROL);211 r.keyPress(KeyEvent.VK_V);212 r.keyRelease(KeyEvent.VK_CONTROL);213 r.keyRelease(KeyEvent.VK_V);214 } catch (AWTException e) {215 e.printStackTrace();216 }217 }218/*=====================================================================================================*/219 220 221 222 223}...

Full Screen

Full Screen

Source:JavascriptCommands.java Github

copy

Full Screen

1package javascript;2import io.github.bonigarcia.wdm.WebDriverManager;3import org.openqa.selenium.By;4import org.openqa.selenium.JavascriptExecutor;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.WebDriverWait;9import org.testng.Assert;10import org.testng.annotations.AfterClass;11import org.testng.annotations.BeforeClass;12import org.testng.annotations.Test;13import org.testng.asserts.SoftAssert;14public class JavascriptCommands {15 private WebDriver driver;16 private By LargeAndDeepDom = By.linkText("Large & Deep DOM");17 private By PageHeaderName = By.cssSelector("div[class='example'] h3");18 private By LargeAndDeepDomPageFooter = By.xpath("//div[@class='large-4 large-centered columns']//div[1]");19 private By table = By.id("large-table");20 private By JavaScriptAlert = By.linkText("JavaScript Alerts");21 @BeforeClass22 public void setup(){23 WebDriverManager.chromedriver().setup();24 driver = new ChromeDriver();25 driver.get("http://the-internet.herokuapp.com/");26 String title = driver.getTitle();27 //System.out.println(title);28 Assert.assertEquals(title,"The Internet");29 }30 @Test(priority = 1)31 public void scroll_to_bottom_of_the_page(){32 //Creating the JavascriptExecutor interface object by Type casting33 JavascriptExecutor js = (JavascriptExecutor)driver;34 driver.findElement(LargeAndDeepDom).click();35 Assert.assertEquals(driver.findElement(PageHeaderName).getText(), "Large & Deep DOM");36 js.executeScript("window.scrollTo(0, document.body.scrollHeight)");37 //System.out.println(driver.findElement(pageFooter).getText());38 Assert.assertEquals(driver.findElement(LargeAndDeepDomPageFooter).getText(),"Powered by Elemental Selenium");39 }40 @Test(priority = 2)41 public void scroll_to_top_of_the_page(){42 //Creating the JavascriptExecutor interface object by Type casting43 JavascriptExecutor js = (JavascriptExecutor)driver;44 js.executeScript("window.scrollTo(0, -document.body.scrollHeight)");45 Assert.assertEquals(driver.findElement(PageHeaderName).getText(), "Large & Deep DOM");46 }47 @Test(priority = 3)48 public void scroll_to_a_specific_element(){49 //Creating the JavascriptExecutor interface object by Type casting50 JavascriptExecutor js = (JavascriptExecutor)driver;51 WebDriverWait wait = new WebDriverWait(driver, 30);52 wait.until(ExpectedConditions.visibilityOfElementLocated(table));53 js.executeScript("arguments[0].scrollIntoView(true);", driver.findElement(table));54 System.out.println(driver.findElement(table).getText());55 //Assert.assertEquals(driver.findElement(table).getText(),"Table");56 }57 @Test(priority = 4)58 public void click_on_an_element(){59 //Creating the JavascriptExecutor interface object by Type casting60 JavascriptExecutor js = (JavascriptExecutor)driver;61 driver.navigate().back();62 js.executeScript("arguments[0].click();", driver.findElement(JavaScriptAlert));63 Assert.assertEquals(driver.findElement(PageHeaderName).getText(), "JavaScript Alerts");64 }65 @Test(priority = 5)66 public void refresh_the_browser(){67 //Creating the JavascriptExecutor interface object by Type casting68 JavascriptExecutor js = (JavascriptExecutor)driver;69 js.executeScript("history.go(0)");70 }71 @Test(priority = 6)72 public void going_back_X_no_of_pages(){73 SoftAssert softAssert = new SoftAssert();74 //Creating the JavascriptExecutor interface object by Type casting75 JavascriptExecutor js = (JavascriptExecutor)driver;76 driver.get("https://www.selenium.dev/");77 softAssert.assertEquals(driver.getTitle(), "SeleniumHQ Browser Automation");78 driver.get("https://www.facebook.com");79 softAssert.assertEquals(driver.getTitle(), "Facebook – log in or sign up");80 driver.get("https://twitter.com/");81 softAssert.assertEquals(driver.getTitle(), "Twitter. It’s what’s happening / Twitter");82 driver.get("https://www.google.com");83 softAssert.assertEquals(driver.getTitle(), "Google");84 //go back 3 pages85 js.executeScript("history.go(-3)");86 softAssert.assertEquals(driver.getTitle(), "SeleniumHQ Browser Automation");87 softAssert.assertAll();88 }89 @Test(priority = 7)90 public void testing_session_storage(){91 String itemName = "football";92 String itemValue = "10";93 //Creating the JavascriptExecutor interface object by Type casting94 JavascriptExecutor js = (JavascriptExecutor)driver;95 js.executeScript("window.sessionstorage.setitem(itemName, itemValue)");96 String result = (String) js.executeScript(String.format("return window.sessionstorage.getItem('%s');", itemValue));97 System.out.println(result);98 }99 @AfterClass100 public void tearDown(){101 driver.quit();102 }103}...

Full Screen

Full Screen

Source:JavaScriptUtil.java Github

copy

Full Screen

1package com.qtpselenium.zoho.project.util;2import java.io.File;3import java.io.IOException;4import org.apache.commons.io.FileUtils;5import org.openqa.selenium.JavascriptExecutor;6import org.openqa.selenium.OutputType;7import org.openqa.selenium.TakesScreenshot;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10public class JavaScriptUtil {11 12 13 static WebDriver driver;14 static WebElement element;15 public static void main(String[] args) {16 17 18 19 drawBorder(element, driver);20 //Drawing border and screenshot21 22 23 File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);24 File trg = new File(System.getProperty("user.dir")+"\\screenshot\\twolugs.png");25 try 26 {27 FileUtils.copyFile(src, trg);28 } 29 catch (IOException e) 30 {31 32 e.printStackTrace();33 }34 }35 36 //https://www.twolugs.com/37 public static void scrollIntoView(WebElement element, WebDriver driver)38 {39 JavascriptExecutor js = (JavascriptExecutor)driver;40 js.executeScript("arguments[0].scrollIntoView(true);", element);41 }42 //Vertical scroll down by 600 pixels 43 //js.executeScript("window.scrollBy(0,600)");44 45 46 public static void verticalScrollDown(WebDriver driver)47 { 48 //Launching the Site. 49 driver.get("http://moneyboats.com/"); 50 51 //Maximize window 52 driver.manage().window().maximize();53 JavascriptExecutor js = (JavascriptExecutor)driver;54 js.executeScript("window.scrollBy(0,600)");55 }56 public static void scrollPageDown(WebDriver driver)57 {58 JavascriptExecutor js = (JavascriptExecutor)driver;59 js.executeScript("window.scrollTo(0,document.body.scrollHeight)");60 }61/*********************************Flushing by changing Color*********************************************/ 62 public static void flash(WebElement element, WebDriver driver)63 {64 65 //The method 'getCssValue("backgroundColor");' capture the color66 String bgcolor = element.getCssValue("backgroundColor");67 68 for(int i = 0; i < 50; i++)//Changing the color 50 times69 {70 changeColor("#000000", element, driver);//1-->Black-->#00000071 changeColor(bgcolor, element, driver);//2-->Going back bgcolor(backgroundColor72 }73 }74 75 public static void changeColor(String color,WebElement element,WebDriver driver)76 77 {78 JavascriptExecutor js = (JavascriptExecutor)driver;79 js.executeScript("arguments[0].style.backgroundColor = '" + color + "'", element);80 try81 {82 Thread.sleep(2000);83 } catch (InterruptedException e) 84 {85 86 e.printStackTrace();87 }88 }89/******************************End Changing color*************************************************/ 90 91 public static void drawBorder(WebElement element, WebDriver driver)92 {93 JavascriptExecutor js = (JavascriptExecutor)driver;94 js.executeScript("arguments[0].style.border='3px solid red'", element);95 }96 97 //Capture title of the page98 //driver.getTitle();99 public static String getTitle(WebDriver driver)100 { // //Creating the JavascriptExecutor interface object by Type casting101 JavascriptExecutor js = (JavascriptExecutor)driver;102 String title = js.executeScript("return document.title;").toString();103 return title;104 }105 106 public static void clickElementByJS(WebElement element,WebDriver driver)107 { // //Creating the JavascriptExecutor interface object by Type casting108 JavascriptExecutor js = (JavascriptExecutor)driver;109 js.executeScript("arguments[0].click();", element);110 111 }112 113 114 public static void generateAlert(WebDriver driver,String message)115 { // //Creating the JavascriptExecutor interface object by Type casting116 JavascriptExecutor js = (JavascriptExecutor)driver;117 js.executeScript("alert('" + message +"')");118 119 }120 121 //refreshing page122 //driver.navigate().refresh();123 124 public static void refreshBrowserByJS(WebDriver driver)125 { //Creating the JavascriptExecutor interface object by Type casting126 JavascriptExecutor js = (JavascriptExecutor)driver;127 js.executeScript("history.go(0)");128 129 }130 131//==================================================================================132 133//JavaScriptExecutor is an Interface that helps to execute JavaScript 134//through Selenium Webdriver. JavaScriptExecutor provides two methods 135//"executescript" & "executeAsyncScript" to run javascript on the selected 136//window or current page.137 138/*139Summary:140JavaScriptExecutor is used when Selenium Webdriver fails to click on any 141element due to some issue.1421.JavaScriptExecutor provides two methods "executescript" & "executeAsyncScript" 143to handle.1442.Executed the JavaScript using Selenium Webdriver.1453.Illustrated how to click on an element through JavaScriptExecutor, 146if selenium fails to click on element due to some issue.1474.Generated the 'Alert' window using JavaScriptExecutor.1485.Navigated to the different page using JavaScriptExecutor.1496.Scrolled down the window using JavaScriptExecutor.1507.Fetched URL, title, and domain name using JavaScriptExecutor.151 152 153*/154 155 156}...

Full Screen

Full Screen

Source:DebugMainMethod.java Github

copy

Full Screen

1package com.qtpselenium.zoho.project.practice;2import java.io.File;3import java.io.IOException;4import org.apache.commons.io.FileUtils;5import org.openqa.selenium.JavascriptExecutor;6import org.openqa.selenium.OutputType;7import org.openqa.selenium.TakesScreenshot;8import org.openqa.selenium.WebDriver;9import org.openqa.selenium.WebElement;10public class DebugMainMethod {11 static WebDriver driver;12 static WebElement element;13 public static void main(String[] args) {14 15 16 17 drawBorder(element, driver);18 //Drawing border and screenshot19 20 21 File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);22 File trg = new File(System.getProperty("user.dir")+"\\screenshot\\twolugs.png");23 try 24 {25 FileUtils.copyFile(src, trg);26 } 27 catch (IOException e) 28 {29 30 e.printStackTrace();31 }32 }33 34 //https://www.twolugs.com/35 public static void scrollIntoView(WebElement element, WebDriver driver)36 {37 JavascriptExecutor js = (JavascriptExecutor)driver;38 js.executeScript("arguments[0].scrollIntoView(true);", element);39 }40 //Vertical scroll down by 600 pixels 41 //js.executeScript("window.scrollBy(0,600)");42 43 44 public static void verticalScrollDown(WebDriver driver)45 { 46 //Launching the Site. 47 driver.get("http://moneyboats.com/"); 48 49 //Maximize window 50 driver.manage().window().maximize();51 JavascriptExecutor js = (JavascriptExecutor)driver;52 js.executeScript("window.scrollBy(0,600)");53 }54 public static void scrollPageDown(WebDriver driver)55 {56 JavascriptExecutor js = (JavascriptExecutor)driver;57 js.executeScript("window.scrollTo(0,document.body.scrollHeight)");58 }59/*********************************Flushing by changing Color*********************************************/ 60 public static void flash(WebElement element, WebDriver driver)61 {62 63 //The method 'getCssValue("backgroundColor");' capture the color64 String bgcolor = element.getCssValue("backgroundColor");65 66 for(int i = 0; i < 50; i++)//Changing the color 50 times67 {68 changeColor("#000000", element, driver);//1-->Black-->#00000069 changeColor(bgcolor, element, driver);//2-->Going back bgcolor(backgroundColor70 }71 }72 73 public static void changeColor(String color,WebElement element,WebDriver driver)74 75 {76 JavascriptExecutor js = (JavascriptExecutor)driver;77 js.executeScript("arguments[0].style.backgroundColor = '" + color + "'", element);78 try79 {80 Thread.sleep(2000);81 } catch (InterruptedException e) 82 {83 84 e.printStackTrace();85 }86 }87/******************************End Changing color*************************************************/ 88 89 public static void drawBorder(WebElement element, WebDriver driver)90 {91 JavascriptExecutor js = (JavascriptExecutor)driver;92 js.executeScript("arguments[0].style.border='3px solid red'", element);93 }94 95 //Capture title of the page96 //driver.getTitle();97 public static String getTitle(WebDriver driver)98 { // //Creating the JavascriptExecutor interface object by Type casting99 JavascriptExecutor js = (JavascriptExecutor)driver;100 String title = js.executeScript("return document.title;").toString();101 return title;102 }103 104 public static void clickElementByJS(WebElement element,WebDriver driver)105 { // //Creating the JavascriptExecutor interface object by Type casting106 JavascriptExecutor js = (JavascriptExecutor)driver;107 js.executeScript("arguments[0].click();", element);108 109 }110 111 112 public static void generateAlert(WebDriver driver,String message)113 { // //Creating the JavascriptExecutor interface object by Type casting114 JavascriptExecutor js = (JavascriptExecutor)driver;115 js.executeScript("alert('" + message +"')");116 117 }118 119 //refreshing page120 //driver.navigate().refresh();121 122 public static void refreshBrowserByJS(WebDriver driver)123 { //Creating the JavascriptExecutor interface object by Type casting124 JavascriptExecutor js = (JavascriptExecutor)driver;125 js.executeScript("history.go(0)");126 127 }128 129//==================================================================================130 131//JavaScriptExecutor is an Interface that helps to execute JavaScript 132//through Selenium Webdriver. JavaScriptExecutor provides two methods 133//"executescript" & "executeAsyncScript" to run javascript on the selected 134//window or current page.135 136/*137Summary:138JavaScriptExecutor is used when Selenium Webdriver fails to click on any 139element due to some issue.1401.JavaScriptExecutor provides two methods "executescript" & "executeAsyncScript" 141to handle.1422.Executed the JavaScript using Selenium Webdriver.1433.Illustrated how to click on an element through JavaScriptExecutor, 144if selenium fails to click on element due to some issue.1454.Generated the 'Alert' window using JavaScriptExecutor.1465.Navigated to the different page using JavaScriptExecutor.1476.Scrolled down the window using JavaScriptExecutor.1487.Fetched URL, title, and domain name using JavaScriptExecutor.149 150 151*/152 153 154}...

Full Screen

Full Screen

Source:DrvManager_interface.java Github

copy

Full Screen

1package com.instance;2import com.utils.SingleForInterface;3import com.utils.StatCounter;4import io.github.bonigarcia.wdm.WebDriverManager;5import org.openqa.selenium.JavascriptExecutor;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.chrome.ChromeDriver;8import org.openqa.selenium.chrome.ChromeOptions;9import org.openqa.selenium.interactions.Actions;10import org.openqa.selenium.remote.DesiredCapabilities;11import org.openqa.selenium.remote.RemoteWebDriver;12import org.openqa.selenium.support.ui.WebDriverWait;13import java.net.MalformedURLException;14import java.net.URL;15public interface DrvManager_interface {16 WebDriver webDriver = getWebDriver();17 WebDriverWait webDriverWait = getWebDriverWait();18 RemoteWebDriver remoteWebDriver = getRemoteWebDriver();19 JavascriptExecutor javascriptExecutor = getJavaScriptExecutor();20 Actions actions = getActions();21 StatCounter statCounter = getStatCounter();22 StatCounter alwaysNewStatCounter = getAlwaysNewStatCounter();23 SingleForInterface singleForInterface = new SingleForInterface();24 private static WebDriver getWebDriver() {25 WebDriver webDrv = webDriver;26 if (webDrv == null) {27 WebDriverManager.chromedriver().setup();28 ChromeOptions options = new ChromeOptions();29 options.addArguments("--disable-notifications");30 options.addArguments("--incognito");31// options.addArguments("--headless", "--window-size=1920,1080");32// options.addArguments("--start-maximized");33// System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");34 webDrv = new ChromeDriver(options);35 System.out.println(StatCounter.increment());36 }37 System.out.println("getWebDriver");38 return webDrv;39 }40 // MalformedURLException !!!41 private static RemoteWebDriver getRemoteWebDriver() {42 RemoteWebDriver remoteDrv = remoteWebDriver;43 if (remoteDrv == null) {44 try {45 // https://coderoad.ru/17527951/%D0%9A%D0%B0%D0%BA%D0%B0%D1%8F-%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%B0-%D0%BE%D1%82-DesiredCapabilities-%D0%B2-Selenium-WebDriver46 DesiredCapabilities capabilities = new DesiredCapabilities();47 remoteDrv = new RemoteWebDriver(new URL(""), capabilities);48 } catch (MalformedURLException e) {49 e.printStackTrace();50 }51 }52 System.out.println("getRemoteWebDriver");53 return remoteDrv;54 }55 private static WebDriverWait getWebDriverWait() {56 System.out.println("getWebDriverWait");57 return webDriverWait == null ? new WebDriverWait(webDriver, 10) : webDriverWait;58 }59 private static Actions getActions() {60 System.out.println("getActions");61 return actions == null ? new Actions(webDriver) : actions;62 }63 private static JavascriptExecutor getJavaScriptExecutor() {64 System.out.println("getJavaScriptExecutor");65 return javascriptExecutor == null ? (JavascriptExecutor) webDriver : javascriptExecutor;66 }67 private static StatCounter getStatCounter() {68 System.out.println("getStatCounter");69 return statCounter == null ? new StatCounter() : statCounter;70 }71 private static StatCounter getAlwaysNewStatCounter() {72 System.out.println("getAlwaysNewStatCounter");73 return new StatCounter();74 }75}...

Full Screen

Full Screen

Source:CommonUtillty.java Github

copy

Full Screen

1package com.amazon.utilitty;2import java.io.File;3import org.openqa.selenium.Alert;4import org.openqa.selenium.JavascriptExecutor;5import org.openqa.selenium.OutputType;6import org.openqa.selenium.TakesScreenshot;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.interactions.Actions;10import org.openqa.selenium.io.FileHandler;11import org.openqa.selenium.support.ui.ExpectedConditions;12import org.openqa.selenium.support.ui.WebDriverWait;13import com.amazon.basePage.Basepage;14public class CommonUtillty extends Basepage{15 16 17 18 //Take a screen Shot method by the interface of TakeScreenshots19 public static void takeScreenshot(String fileName) throws Exception {20 //Takesscreenshot.........file.......FileHandler21 TakesScreenshot ts = (TakesScreenshot) driver;22 // method...........output as file type23 File source = ts.getScreenshotAs(OutputType.FILE);24 // // location file name adn png25 FileHandler.copy(source, new File("./Screenshot/" + fileName + ".png")); 26 27 }28 29 30 31 32 33 34// Method for the element to be clickable in explicit wait35 public static void explicitWait(WebElement element , long time) {36 //passing driver and time37 WebDriverWait wait = new WebDriverWait(driver,time);38 //untill(condition passing element39 wait.until(ExpectedConditions.elementToBeClickable(element));40 41 }42 43 44 45 46 47 //Highlight the element by the JavascriptExecutor interface48 public static void highLighterMethod(WebDriver driver, WebElement element) {49 //JavascriptExecutor is a Interface type casting50 JavascriptExecutor js = (JavascriptExecutor) driver;51 // method // passing and WebElement element52 js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;')", element);53 54 }55 56 57 //Mouseover by the Action interface and move to element58 public static void mouseOver(WebElement element) {59 60 61 // Actions is a class from the selenimum and passing the driver 62 Actions act = new Actions(driver);63 // it is just moving to the element that i need to be.64 act.moveToElement(element).build().perform();65 66 67 68 69 70 71 72 73 }74 75 76 77 78 79 80 public static Alert alertAcceptPopUp() {81 Alert alert = driver.switchTo().alert();82 return alert; 83 84 }85 86 87 88 public static void iFrame(WebElement element) {89 driver.switchTo().frame(element); 90 91 }92 93 public static void ParentFrame() {94 driver.switchTo().defaultContent();95 96 }97 98 99 100 101 102 103 104 105 106 107 108 109 110 111}...

Full Screen

Full Screen

Source:JavaSE_Test.java Github

copy

Full Screen

1package packageTest;2import java.awt.Window;3import java.util.concurrent.TimeUnit;4import org.openqa.selenium.Alert;5import org.openqa.selenium.By;6import org.openqa.selenium.JavascriptExecutor;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.WebElement;9import org.openqa.selenium.firefox.FirefoxDriver;10import org.testng.annotations.Test;11import SeleniumPack.BrowserFactory;12public class JavaSE_Test {13 14 @Test public void login() {15 16 WebDriver driver = BrowserFactory.launch("chrome"); JavascriptExecutor js =17 (JavascriptExecutor)driver; driver.get("http://demo.guru99.com/V4/");18 driver.manage().window().maximize();19 driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS); long20 start_time =System.currentTimeMillis(); js.21 executeAsyncScript("window.setTimeout(arguments[arguments.length-1], 5000);"22 ); System.out.println("Passed time: " + (System.currentTimeMillis() -23 start_time));24 25 /*26 * @Test public void Login() {27 * 28 * WebDriver driver = BrowserFactory.launch("chrome"); Alert alt =29 * driver.switchTo().alert(); //Creating the JavascriptExecutor interface object30 * by Type casting JavascriptExecutor js = (JavascriptExecutor)driver;31 * 32 * 33 * //Launching the Site. driver.get("http://demo.guru99.com/V4/");34 * 35 * WebElement button =driver.findElement(By.name("btnLogin"));36 * 37 * //Login to Guru99 driver.findElement(By.name("uid")).sendKeys("mngr34926");38 * driver.findElement(By.name("password")).sendKeys("amUpenu");39 * 40 * //Perform Click on LOGIN button using JavascriptExecutor41 * js.executeScript("arguments[0].click();", button); String getTxt =42 * alt.getText(); System.out.println(getTxt); alt.accept();43 * 44 * }45 */46}47}...

Full Screen

Full Screen

Source:A27_JavascriptExecutorTypeTest.java Github

copy

Full Screen

1package javascriptExecutor;2import org.openqa.selenium.By;3import org.openqa.selenium.JavascriptException;4import org.openqa.selenium.JavascriptExecutor;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.chrome.ChromeDriver;8public class A27_JavascriptExecutorTypeTest {9 public static void main(String[] args) {10 // TODO Auto-generated method stub11System.setProperty("webdriver.chrome.driver", "C:\\Vcentry\\Workspace\\A18_WebDriverTest\\Browser\\chromedriver.exe");12 13 WebDriver wd= new ChromeDriver();14 wd.manage().window().maximize();15 wd.get("https://www.bing.com/" );16 WebElement searchBox= wd.findElement(By.name("q"));17 // searchBox.sendKeys("chennai); 10- 1 time fail18 19 //interface== WebDriver, WebElement, JavascriptExecutor, TakesScreenshot20 21 //((TakesScreenshot)wd).getScreenshotAs(OutputType.FILE);22 //((JavascriptExecutor)wd).executeScript("", "")23 24 JavascriptExecutor js = (JavascriptExecutor)wd;25 26 //js.executeScript("javascript", "e1","e2","e3");27 // "js",0,1,228 29 js.executeScript("arguments[0].value='chennai'", searchBox);30 31 }32}...

Full Screen

Full Screen

Interface JavascriptExecutor

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2js.executeScript("window.scrollBy(0,document.body.scrollHeight)");3JavascriptExecutor js = (JavascriptExecutor) driver;4js.executeScript("window.scrollBy(0,1000)");5JavascriptExecutor js = (JavascriptExecutor) driver;6js.executeScript("window.scrollBy(0,1000)");7js.executeScript("window.scrollBy(0,1000)");8JavascriptExecutor js = (JavascriptExecutor) driver;9js.executeScript("window.scrollBy(0,1000)");10js.executeScript("window.scrollBy(0,1000)");11js.executeScript("window.scrollBy(0,1000)");12JavascriptExecutor js = (JavascriptExecutor) driver;13js.executeScript("window.scrollBy(0,1000)");14js.executeScript("window.scrollBy(0,1000)");15js.executeScript("window.scrollBy(0,1000)");16js.executeScript("window.scrollBy(0,1000)");17JavascriptExecutor js = (JavascriptExecutor) driver;18js.executeScript("window.scrollBy(0,1000)");19js.executeScript("window.scrollBy(0,1000)");20js.executeScript("window.scrollBy(0,1000)");21js.executeScript("window.scrollBy(0,1000)");22js.executeScript("window.scrollBy(0,1000)");23JavascriptExecutor js = (JavascriptExecutor) driver;24js.executeScript("window.scrollBy(0,1000)");25js.executeScript("window.scrollBy(0,1000)");26js.executeScript("window.scrollBy(0,1000)");27js.executeScript("window.scrollBy(0,1000)");28js.executeScript("window.scrollBy(0,1000)");29js.executeScript("window.scrollBy(0,1000)");30JavascriptExecutor js = (JavascriptExecutor) driver;31js.executeScript("window.scrollBy(0,1000)");32js.executeScript("window.scrollBy(0,1000)");33js.executeScript("window.scrollBy

Full Screen

Full Screen

Interface JavascriptExecutor

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2TakesScreenshot ts = (TakesScreenshot) driver;3File src = ts.getScreenshotAs(OutputType.FILE);4File dest = new File("./target/screenshots/"+fileName+".png");5FileUtils.copyFile(src, dest);6js.executeScript("arguments[0].setAttribute('style','border: solid 2px red;');", driver.findElement(By.xpath("xpath")));7TakesScreenshot ts = (TakesScreenshot) driver;8File src = ts.getScreenshotAs(OutputType.FILE);9File dest = new File("./target/screenshots/"+fileName+".png");10FileUtils.copyFile(src, dest);11JavascriptExecutor js = (JavascriptExecutor) driver;12js.executeScript("arguments[0].setAttribute('style','border: solid 2px red;');", driver.findElement(By.xpath("xpath")));13JavascriptExecutor js = (JavascriptExecutor) driver;14js.executeScript("arguments[0].setAttribute('style','border: solid 2px red;');", driver.findElement(By.xpath("xpath")));15TakesScreenshot ts = (TakesScreenshot) driver;16File src = ts.getScreenshotAs(OutputType.FILE);17File dest = new File("./target/screenshots/"+fileName+".png");18FileUtils.copyFile(src, dest);19JavascriptExecutor js = (JavascriptExecutor) driver;20js.executeScript("arguments[0].setAttribute('style','border: solid 2px red;');", driver.findElement(By.xpath("xpath")));21TakesScreenshot ts = (TakesScreenshot) driver;22File src = ts.getScreenshotAs(OutputType.FILE);23File dest = new File("./target/screenshots/"+fileName+".png");24FileUtils.copyFile(src, dest);25JavascriptExecutor js = (JavascriptExecutor) driver;26js.executeScript("arguments[0].setAttribute('style','border: solid 2px red;');", driver.findElement(By.xpath("xpath")));

Full Screen

Full Screen

Interface JavascriptExecutor

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2TakesScreenshot ts = (TakesScreenshot) driver;3RemoteWebDriver rwd = (RemoteWebDriver) driver;4WebDriver wd = (WebDriver) driver;5JavaScriptError jsError = (JavaScriptError) js;6Shutterbug sb = (Shutterbug) js;7WebDriverScreenshot wdss = (WebDriverScreenshot) js;8WebDriverScreenshot wdss = (WebDriverScreenshot) ts;9WebDriverScreenshot wdss = (WebDriverScreenshot) wd;10WebDriverScreenshot wdss = (WebDriverScreenshot) rwd;11JavascriptExecutor js = (JavascriptExecutor) driver;12TakesScreenshot ts = (TakesScreenshot) driver;13RemoteWebDriver rwd = (RemoteWebDriver) driver;14WebDriver wd = (WebDriver) driver;15JavaScriptError jsError = (JavaScriptError) js;16Shutterbug sb = (Shutterbug) js;17WebDriverScreenshot wdss = (WebDriverScreenshot) js;18WebDriverScreenshot wdss = (WebDriverScreenshot) ts;

Full Screen

Full Screen

Interface JavascriptExecutor

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2js.executeScript("window.scrollBy(0,300)");3js.executeScript("window.scrollBy(0,-300)");4js.executeScript("window.scrollTo(0, document.body.scrollHeight)");5js.executeScript("window.scrollTo(0, -document.body.scrollHeight)");6js.executeScript("arguments[0].scrollIntoView();", element);7JavascriptExecutor js = (JavascriptExecutor) driver;8js.executeScript("window.scrollBy(0,300)");9js.executeScript("window.scrollBy(0,-300)");10js.executeScript("window.scrollTo(0, document.body.scrollHeight)");11js.executeScript("window.scrollTo(0, -document.body.scrollHeight)");12js.executeScript("arguments[0].scrollIntoView();", element);

Full Screen

Full Screen

Interface JavascriptExecutor

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor) driver;2js.executeScript("window.scrollBy(0,1000)");3JavascriptExecutor js = (JavascriptExecutor) driver;4js.executeScript("window.scrollTo(0, document.body.scrollHeight)");5JavascriptExecutor js = (JavascriptExecutor) driver;6js.executeScript("window.scrollTo(0, -document.body.scrollHeight)");7JavascriptExecutor js = (JavascriptExecutor) driver;8js.executeScript("window.scrollTo(document.body.scrollWidth, 0)");9JavascriptExecutor js = (JavascriptExecutor) driver;10js.executeScript("window.scrollTo(-document.body.scrollWidth, 0)");11JavascriptExecutor js = (JavascriptExecutor) driver;12js.executeScript("arguments[0].scrollIntoView();", element);13JavascriptExecutor js = (JavascriptExecutor) driver;14js.executeScript("arguments[0].scrollIntoView();", element);15JavascriptExecutor js = (JavascriptExecutor) driver;16js.executeScript("arguments[0].scrollIntoView();", element);

Full Screen

Full Screen

Interface JavascriptExecutor

Using AI Code Generation

copy

Full Screen

1JavascriptExecutor js = (JavascriptExecutor)driver;2js.executeScript("window.scrollBy(0,1000)");3JavascriptExecutor js = (JavascriptExecutor)driver;4js.executeScript("window.scrollBy(0,1000)");5JavascriptExecutor js = (JavascriptExecutor)driver;6js.executeScript("window.scrollBy(0,1000)");7JavascriptExecutor js = (JavascriptExecutor)driver;8js.executeScript("window.scrollBy(0,1000)");9JavascriptExecutor js = (JavascriptExecutor)driver;10js.executeScript("window.scrollBy(0,1000)");11JavascriptExecutor js = (JavascriptExecutor)driver;12js.executeScript("window.scrollBy(0,1000)");13JavascriptExecutor js = (JavascriptExecutor)driver;14js.executeScript("window.scrollBy(0,1000)");15JavascriptExecutor js = (JavascriptExecutor)driver;16js.executeScript("window.scrollBy(0,1000)");17JavascriptExecutor js = (JavascriptExecutor)driver;18js.executeScript("window.scrollBy(0,1000)");

Full Screen

Full Screen
copy
1606 public native int availableProcessors();2617 public native long freeMemory();3630 public native long totalMemory();4641 public native long maxMemory();5664 public native void gc();6
Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in Interface-JavascriptExecutor

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful