Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
1.2k views
by

My code is as follows-: 

package firstPackage;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class JavaScriptExecutorConcept {

    public static void main(String[] args) {
        
        
System.setProperty("webdriver.chrome.driver","C:\\Users\\piush//chromedriver.exe");
        
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        
        driver.manage().timeouts().pageLoadTimeout(40,TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
        driver.get("https://www.facebook.com/");
        
        driver.findElement(By.id("email")).sendKeys("[email protected]");
        driver.findElement(By.id("pass")).sendKeys("123456789");
        
        
 
         
        
        WebElement loginBtn = driver.findElement(By.xpath("//input[@id='u_0_b']")); 
        flash(loginBtn, driver); 
        
        drawBorder(loginBtn, driver); 

    }
    public static void flash(WebElement element, WebDriver driver) {
        
        JavascriptExecutor js = ((JavascriptExecutor)driver);
        String bgcolor = element.getCssValue("backgroundColor");
        for (int i = 0; i < 100; i++) {
            changeColor("rgb(0,200,0)", element, driver); 
            changeColor(bgcolor, element, driver); 
            
        }
    }
        public static void changeColor(String color, WebElement element, WebDriver driver) {
            JavascriptExecutor js = ((JavascriptExecutor)driver);
            js.executeScript("aguments[0].style.backgroundColor='"+color+"'", element);
            try {
                Thread.sleep(20);
          
            }catch (InterruptedException e) {
              
        }
    
}


public static void drawBorder(WebElement element, WebDriver driver) {
    JavascriptExecutor js = ((JavascriptExecutor)driver);
    js.executeScript("aguments[0].style.border='3px solid red'", element);
}

}

The error that I'm getting is-: 

Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 26787
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1579159191.958][WARNING]: This version of ChromeDriver has not been tested with Chrome version 79.
Jan 16, 2020 12:49:53 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: aguments is not defined
  (Session info: chrome=79.0.3945.117)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:48'
System info: host: 'LAPTOP-367DO8AD', ip: '192.168.43.110', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_231'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 79.0.3945.117, chrome: {chromedriverVersion: 78.0.3904.70 (edb9c9f3de024..., userDataDir: C:\Users\piush\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:64798}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 202276515147957313c5b93a9d5f9e74
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:489)
    at firstPackage.JavaScriptExecutorConcept.changeColor(JavaScriptExecutorConcept.java:51)
    at firstPackage.JavaScriptExecutorConcept.flash(JavaScriptExecutorConcept.java:44)
    at firstPackage.JavaScriptExecutorConcept.main(JavaScriptExecutorConcept.java:34)
 

 

1 Answer

0 like 0 dislike
by

In drawBorder method, the argument is misspelled.
 js.executeScript("aguments[0].style.border='3px solid red'", element);

correct this to

 js.executeScript("arguments[0].style.border='3px solid red'", element);


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...