Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

JavascriptExecutor dies when including a 'var' #5192

Closed
lukeis opened this issue Mar 4, 2016 · 3 comments
Closed

JavascriptExecutor dies when including a 'var' #5192

lukeis opened this issue Mar 4, 2016 · 3 comments

Comments

@lukeis
Copy link
Member

lukeis commented Mar 4, 2016

Originally reported on Google Code with ID 5192

What steps will reproduce the problem?
1. Create a test case using Webdriver.
2. Paste the following code in the test.

  JavascriptExecutor js = (JavascriptExecutor) driver;
  js.executeScript("return var x = 'something'; alert(x);");

3. Run your test.

What is the expected output? What do you see instead?
I would expect to see an alert box with the message 'something' inside it.
What I see is my test failing with the below error message:

org.openqa.selenium.WebDriverException: SyntaxError: syntax error
Command duration or timeout: 32 milliseconds
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:46:35'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-279.19.1.el6.x86_64',
java.version: '1.7.0_09-icedtea'
Session ID: abe17c62-011d-4403-8e73-bec81f56c8f9
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true,
acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true,
nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true,
applicationCacheEnabled=true, takesScreenshot=true, version=17.0.2}]
Command duration or timeout: 558 milliseconds
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:46:43'
System info: os.name: 'Linux', os.arch: 'x86', os.version: '2.6.32-279.19.1.el6.x86_64',
java.version: '1.6.0'
Session ID: 7c2153f8-d732-4117-b4c5-cc4b65ee2512
Capabilities [{handlesAlerts=true, rotatable=false, databaseEnabled=true, locationContextEnabled=true,
acceptSslCerts=true, applicationCacheEnabled=true, nativeEvents=false, cssSelectorsEnabled=true,
takesScreenshot=true, platform=LINUX, browserName=firefox, javascriptEnabled=true,
version=17.0.2, webStorageEnabled=true, webdriver.remote.sessionid=7c2153f8-d732-4117-b4c5-cc4b65ee2512,
browserConnectionEnabled=true}]
Driver info: org.openqa.selenium.remote.RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:533)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:441)
    at com.ibm.multitool.active.selenium.SendInstructionsTest.testSendInstructionsRetainExistingCase(SendInstructionsTest.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    at java.lang.reflect.Method.invoke(Method.java:611)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:48)
    at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:48)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:110)
    at org.junit.rules.TestWatchman$1.evaluate(TestWatchman.java:48)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:452)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:314)
    at java.util.concurrent.FutureTask.run(FutureTask.java:149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:897)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:919)
    at java.lang.Thread.run(Thread.java:738)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:46:43'
System info: os.name: 'Linux', os.arch: 'x86', os.version: '2.6.32-279.19.1.el6.x86_64',
java.version: '1.6.0'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:125)
    ... 27 more
Caused by: org.openqa.selenium.WebDriverException: SyntaxError: syntax error
Command duration or timeout: 32 milliseconds
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:46:35'
System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-279.19.1.el6.x86_64',
java.version: '1.7.0_09-icedtea'
Session ID: abe17c62-011d-4403-8e73-bec81f56c8f9
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=LINUX, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true,
acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true,
nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true,
applicationCacheEnabled=true, takesScreenshot=true, version=17.0.2}]
Build info: version: '2.29.0', revision: '58258c3', time: '2013-01-17 22:46:43'
System info: os.name: 'Linux', os.arch: 'x86', os.version: '2.6.32-279.19.1.el6.x86_64',
java.version: '1.6.0'
Driver info: driver.version: EventFiringWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:187)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:533)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:441)
    at sun.reflect.GeneratedMethodAccessor35.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:101)
    at $Proxy1.executeScript(Unknown Source)
    at org.openqa.selenium.support.events.EventFiringWebDriver.executeScript(EventFiringWebDriver.java:209)
    at org.openqa.selenium.remote.server.handler.ExecuteScript.call(ExecuteScript.java:55)
    at org.openqa.selenium.remote.server.handler.ExecuteScript.call(ExecuteScript.java:1)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at org.openqa.selenium.remote.server.DefaultSession$1.run(DefaultSession.java:167)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

================================

If you replace the below line:
  js.executeScript("return var x = 'something'; alert(x);");

with the following:
  js.executeScript("return alert('something');");

Everything works just fine.  My test always fails whenever I include a 'var' in the
JavaScript.

Selenium version: 2.29.0
OS:
  Linux Client for e-business (RHEL) 6.3 (Gold Master)
  Open Client RHEL 64 3.30 (Gold Master)
  Red Hat Enterprise Linux Workstation release 6.3 (Santiago)

Browser: Firefox
Browser version: 17.0.2 ESR


Please provide any additional information below. A sample reduced test
case, or a public URL that demonstrates the problem will intrigue our merry
band of Open Source developers far more than nothing at all: they'll be far
more likely to look at your problem if you make it easy for them!

Reported by karl.portner on 2013-02-19 21:47:07

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

If I try to enter the following in the Firebug console, I receive a syntax error:

function zz() { return var x; alert('hello'); }; zz();

This is functionally equivalent to what WebDriver's executeScript() method is doing,
creating a function and immediately executing the function created. If your JavaScript
is syntactically invalid, the JavascriptExecutor will not be able to work at all.

Executing the following, however, works fine for me:

((JavascriptExecutor)driver).executeScript("var x = 2; alert('hello'); return x;");

It's perfectly acceptable to use the "var" keyword in your JavaScript, and it's perfectly
valid to return a value from your JavaScript, but "return var" is invalid JavaScript
syntax.

Reported by james.h.evans.jr on 2013-02-19 22:31:36

  • Status changed: WorkingAsIntended
  • Labels removed: Status-Untriaged

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Thanks for setting me straight, I misunderstood how this statement works.

Regards

Reported by karl.portner on 2013-02-20 13:39:11

@lukeis
Copy link
Member Author

lukeis commented Mar 4, 2016

Reported by luke.semerau on 2015-09-17 18:16:47

  • Labels added: Restrict-AddIssueComment-Commit

@lukeis lukeis closed this as completed Mar 4, 2016
@SeleniumHQ SeleniumHQ locked and limited conversation to collaborators Mar 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant