JavaScript Executor在谷歌浏览器中工作但不在Firefox中工作

时间:2018-05-09 12:49:24

标签: javascript java selenium

所以我使用Selenium测试在多个浏览器上测试我的网站。

我对谷歌浏览器的测试工作完全正常,但它不适用于Firefox。

public class BrowserAutomationTest {
    private final static WebDriver driver = new ChromeDriver();
    private final static Actions builder = new Actions(driver);
    private final static JavascriptExecutor js = (JavascriptExecutor) driver;


    @BeforeClass
    public static void init() {
        System.setProperty("webdriver.chrome.driver", "chromedriver");
        System.setProperty("webdriver.gecko.driver", "geckodriver");

        final ChromeOptions options = new ChromeOptions();
        options.addArguments("disable-infobars");

        driver.get("file:/graph.html");
    }

@Test
private void leftClickAddNodeTest() {
    //add two nodes in different positions by left clicking
    final WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.className("tour-close-button")));
driver.findElement(By.className("tour-close-button")).click();

builder.moveByOffset(0, 0)
        .click()
        .moveByOffset(100, 100)
        .click()
        .build()
        .perform();

final ArrayList<Object> jsReturnObject = (ArrayList<Object>) js.executeScript("return sigmaInstance"); // Error is here
assertTrue(jsReturnObject.size() == 2);
}
}

因此,上述代码适用于Google Chrome。

但是,将Webdriver行替换为:

private final static WebDriver driver = new FirefoxDriver();

Firefox确实加载但是当调用js.executeScript()时它会返回异常:

org.openqa.selenium.JavascriptException: ReferenceError: sigmaInstance is not defined

奇怪的问题是在Selenium Google Chrome测试中不会发生此错误。此外,当手动将JS插入Firefox浏览器时,它将起作用。

可能是什么问题?感谢

编辑:完整堆栈跟踪

objc[3179]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/bin/java (0x1088bb4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10893f4e0). One of the two will be used. Which one is undefined.
1525869916907   geckodriver INFO    geckodriver 0.20.1
1525869916913   geckodriver INFO    Listening on 127.0.0.1:22235
1525869917264   mozrunner::runner   INFO    Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-profile" "/var/folders/sn/b7mx42_530v_kx2xqn27d84h0000gn/T/rust_mozprofile.VRXA2ZSTKOIA"
1525869917876   Marionette  INFO    Enabled via --marionette
2018-05-09 13:45:18.710 plugin-container[3182:399742] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x7c3f, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
2018-05-09 13:45:18.859 plugin-container[3182:399742] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x7cbb, name = 'com.apple.coredrag'
See /usr/include/servers/bootstrap_defs.h for the error codes.
1525869919046   Marionette  INFO    Listening on port 60700
1525869919134   Marionette  WARN    TLS certificate errors will be ignored for this session
May 09, 2018 1:45:19 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
2018-05-09 13:45:19.607 plugin-container[3183:399823] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x7c43, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
2018-05-09 13:45:19.771 plugin-container[3183:399823] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x190f, name = 'com.apple.coredrag'
See /usr/include/servers/bootstrap_defs.h for the error codes.

org.openqa.selenium.JavascriptException: ReferenceError: sigmaInstance is not defined
Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:26:55.152Z'
System info: host: 'Emans-MBP-2', ip: '2a00:23c4:4a09:b00:6cfa:11:c2f:f577', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.3', java.version: '1.8.0_151'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 59.0.3, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 3181, moz:profile: /var/folders/sn/b7mx42_530v..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 17.4.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: d2b4c7f5-2951-1047-8f97-4a7640091b09

    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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:545)
    at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:482)
    at BrowserAutomationTest.leftClickAddNodeTest(BrowserAutomationTest.java:58)
    at BrowserAutomationTest.testSequence(BrowserAutomationTest.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

*** UTM:SVC TimerManager:registerTimer called after profile-before-change notification. Ignoring timer registration for id: telemetry_modules_ping

1 个答案:

答案 0 :(得分:0)

完整的错误堆栈跟踪肯定会帮助我们以更好的方式调试您的错误/问题但是此错误消息...

org.openqa.selenium.JavascriptException: ReferenceError: sigmaInstance is not defined

...可能因为 GeckoDriver 和/或 sigmaInstance 未正确声明/定义而引起。

首先,您需要将 GeckoDriver 二进制文件的绝对路径作为String传递,如下所示:

System.setProperty("webdriver.gecko.driver", "geckodriver");

注意:根据您当前的代码, geckodriver 不在""内被识别为String

其次,您尝试返回sigmaInstance 而不执行任何脚本。您对 executeScript 的致电应采用以下格式:

java.lang.Object executeScript(java.lang.String script, java.lang.Object... args)

根据 Java Docs executeScript()在当前选定的框架或窗口的上下文中执行JavaScript。提供的脚本片段将作为匿名函数的主体执行。在脚本中,使用document来引用当前文档。请注意,一旦脚本执行完毕,局部变量将不可用,但全局变量将保持不变。