Selenium (java) with Chrome-driver, ignoring "not defined" javascript errors

1,459 views
Skip to first unread message

Martin

unread,
Oct 24, 2016, 6:05:23 AM10/24/16
to Selenium Users
Hello! 
I am trying to write a test for my online shopping web page where I want to execute javascript that enables some discounts. Executing the scripts through the browser console works perfectly, however when I try to execute them using java and selenium, I get an error telling me that the js method is undefined (which it is, I get the same message in the browser console, but the browser just ignores it and executes it anyway. With Selenium it crashes and does not execute the script).
I can unfortunately not change the javascript/html code itself, so I would need to fix it just using Java/Selenium. 

Is there a way to tell Selenium to ignore the Java Script errors and continue executing the scripts anyway?


The relevant code that gives me the error is the following: 

public WebDriver driver;
driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("addFakeRule(\"7914\", {isNew: true});");


The error I receive which makes the test case quit without finishing is:

org.openqa.selenium.WebDriverException: unknown error: addFakeDataRule is not defined

I am using the Selenium-java-3.0.0 and the latest Chrome driver, and the java code is written using latest Eclipse Neon on Windows 8.1.

Have been searching the web for hours and can't find a way to ignore the JS error and just proceed with the script execution. 

Any help is truly appreciated! 



Here is the complete error/stacktrace that I am getting:
org.openqa.selenium.WebDriverException: unknown error: addFakeRule is not defined
  (Session info: chrome=54.0.2840.71)
  (Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 40 milliseconds
Build info: version: 'unknown', revision: '350cf60', time: '2016-10-13 10:43:56 -0700'
System info: host: 'MMA-CZC3094Z8B', ip: '10.19.210.101', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_111'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf), userDataDir=c:\temp\scoped_dir3536_18951}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=54.0.2840.71, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 6ba9f4715531bdd6ae439ac07665e101
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.ErrorHandler.createThrowable(ErrorHandler.java:216)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:635)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:537)
at IKEA_Test.TestCases.testDiscounts(TestCases.java:89)
at IKEA_Test.InitiateTest.testDiscounts(InitiateTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:607)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
at org.testng.SuiteRunner.run(SuiteRunner.java:270)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)


Simon Stewart

unread,
Oct 24, 2016, 8:17:51 AM10/24/16
to seleniu...@googlegroups.com
This is expected behaviour. The error you see on the console is the result of the exception being thrown. "executeScript" is merely doing the same thing. There are many ways to get this working:

1) Don't call a function that doesn't exist.

2) Call the function, but wrap with a try/catch block in JS if you expect this method to sometimes be present

3) Check for the presence of the function in your JS and add a stub function that does nothing if it's missing.

Kind regards,

Simon

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/4a2a88e0-a778-4564-80ae-a6298e8246ef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Martin

unread,
Oct 24, 2016, 11:46:18 PM10/24/16
to Selenium Users
Thank you for you answer. I know that I can catch the error and have the rest of the code proceed with the execution, however then the JS command that generated the error will be skipped. Maybe error was the wrong word to use, it's more of a message telling me that the JS command I am trying to execute is undefined, and I do not care about that, I want to execute it anyway. As I said, if I run the command in Google Chromes console (not using selenium), then the command executes without any issues (except for telling me about the undefined method, but it still executes and does what it is supposed to do). I would like Selenium to run the JS commands I throw at it without checking if they generate any errors or not, "just do it Selenium" so to speak. 

I have found out that there is a way to tell Selenium to skip JS error messages if you use the HTMLUnitDriver, the HTML driver has a method called getOptions().setThrowExceptionOnScriptError(false); which does exactly what I want, except it is just for the HTMLUnitDriver and not for the ordinary ChromeDriver/FirefoxDriver. So I would need a similar function for the Chrome or Firefox driver. In essence, I want to execute my JS command which generates a "undefined" message without Selenium stopping it because of the message.

To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages