vmi / selenese-runner-java

Selenium IDE native format (selenese and side) interpreter.
Other
140 stars 83 forks source link

Unable to store Javascript functions in storedVars #182

Closed alan-morey closed 8 years ago

alan-morey commented 8 years ago

For example, the following script works with Selenium IDE

Command Target Value
storeEval (function(){ return { hello: function(s) { return 'hello' + s; } } })(); util
storeEval storedVars.util.hello('world'); var0
echo ${var0}

Log:

    [info] Executing: |storeEval | (function(){ return { hello: function(s) { return 'hello' + s; } } })(); | util |
    [info] script is: (function(){ return { hello: function(s) { return 'hello' + s; } } })();
    [info] Executing: |storeEval | storedVars.util.hello('world'); | var0 |
    [info] script is: storedVars.util.hello('world');
    [info] Executing: |echo | ${var0} | |
    [info] echo: helloworld
    [info] Test case passed 

Adding similar test to VaribleTest

@Test
public void evalStoredVarWithJavscriptFunction() {
    CommandFactory cf = runner.getCommandFactory();
    String baseURL = wsr.getBaseURL();
    VarsMap varsMap = runner.getVarsMap();
    String script = "(function() { return { hello: function(s) { return 'hello' + s; }}})();";
    TestCase testCase = Binder.newTestCase("dummy", "dummy", baseURL);
    testCase.addCommand(cf, "open", "/");
    testCase.addCommand(cf, "storeEval", script, "util");
    testCase.addCommand(cf, "storeEval", "storedVars.util.hello('world');", "var01");
    testCase.addCommand(cf, "storeEval", "storedVars.util.hello('foo');", "var02");
    Result result = runner.execute(testCase);
    assertThat(result, is(instanceOf(Success.class)));
    assertThat((String) varsMap.get("var01"), is("helloworld"));
    assertThat((String) varsMap.get("var02"), is("hellofoo"));
}

Fails with

java.lang.AssertionError: 
Expected: is an instance of jp.vmi.selenium.selenese.result.Success
     but: <[Error: WebDriverException - com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot call property hello in object [object Object]. It is not a function, it is "string". (injected script#2(eval)#1)
Build info: version: '2.50.0', revision: '1070ace4650453d518aeb03e7a9a36c9d264a8e7', time: '2016-01-27 10:46:18'
System info: host: 'dldev3-amorey', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-76-generic', java.version: '1.8.0_72'
Driver info: driver.version: HtmlUnitDriver (HtmlUnitDriver.executeScript(HtmlUnitDriver.java:693) / Eval.eval(Eval.java:74) / Eval.eval(Eval.java:40) / GetEval.execute(GetEval.java:23) / Store.executeImpl(Store.java:36) / AbstractCommand.execute(AbstractCommand.java:145) / CommandList.doCommand(CommandList.java:98) / ScreenshotInterceptor.invoke(ScreenshotInterceptor.java:18) / AbstractDoCommandInterceptor.invoke(AbstractDoCommandInterceptor.java:29) / HighlightInterceptor.invoke(HighlightInterceptor.java:28) / AbstractDoCommandInterceptor.invoke(AbstractDoCommandInterceptor.java:29) / CommandLogInterceptor.invoke(CommandLogInterceptor.java:72) / AbstractDoCommandInterceptor.invoke(AbstractDoCommandInterceptor.java:29) / CommandList.execute(CommandList.java:152) / TestCase.execute(TestCase.java:299) / ExecuteTestCaseInterceptor.invoke(ExecuteTestCaseInterceptor.java:49) / AbstractExecuteTestCaseInterceptor.invoke(AbstractExecuteTestCaseInterceptor.java:29) / Runner.execute(Runner.java:560) / VariableTest.evalStoredVarWithJavscriptFunction(VariableTest.java:145) / JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) / JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) / JUnitStarter.main(JUnitStarter.java:74))]> is a jp.vmi.selenium.selenese.result.Error
vmi commented 8 years ago

Hi,

This is the restriction of Selenese Runner (and Selenium WebDriver). storedVars is saved in Selenese Runner, and it is exchanged with a browser. However, Selenium WebDriver API can use only "JSONizable" object at executing Javascript. ("JSONizable" object = String, Array, and Object as map)

The workaround is to use Function.toString().

Command Target Value
storeEval (function(){ return { hello: (function(s) { return 'hello' + s; }).toString() } })(); util
storeEval storedVars.util.hello func
storeEval (${func})("world") var0
echo ${var0}
vmi commented 8 years ago

Hi, Can I close this issue?

alan-morey commented 8 years ago

Yes.

On Wed, Apr 27, 2016, 15:22 Motonori IWAMURO notifications@github.com wrote:

Hi, Can I close this issue?

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub https://github.com/vmi/selenese-runner-java/issues/182#issuecomment-215249599