WebdriverJS / IO& PhantomJS - 具有属性选择器的单击处理程序不适用于PhantomJS

时间:2014-11-10 13:03:47

标签: node.js selenium-webdriver phantomjs ghostdriver webdriver-io

我目前正致力于使用WebdriverJS和PhantomJS编写应用程序测试套件。 为确保我的测试有效,我先通过Chrome运行它们,它们都能正常工作。然而,当我换掉Chrome for PhantomJS时,测试就会中断。

这个问题 - WebDriver PhantomJS Unable to find element, but works fine with Firefox - 似乎概述了一个非常类似的问题,但所附的解决方案似乎没有帮助。

以下是适用于Chrome的内容类型的粗略示例,但不适用于PhantomJS:

var client = webdriverjs.remote({ 
    desiredCapabilities: {
        browserName: 'chrome'       
    }, 
    logLevel: 'silent' 
});

client.waitForExist("[data-id='1568911']", function(e){
    client.click("[data-id='1568911']", function(e){
        assert(!e, "Should click on a specific element:" + element);
    });
});

在PhantomJS上运行时,我显然首先更改了WebdriverJS选项:

var client = webdriverjs.remote({ 
    desiredCapabilities: {
        browserName: 'phantomjs',
        'phantomjs.binary.path': "path/to/phantomjs"
    }, 
    logLevel: 'silent' 
});

但是当我运行测试并将logLevel设置为' verbose'时,我会收到如下所示的错误消息:

[12:43:34]:  COMMAND    POST     "/wd/hub/session/eb2b0a4b-e659-4607-bec0-82209bd6539a/element"
[12:43:34]:  DATA        {"using":"css selector","value":"[data-id='1568911']"}
[12:43:35]:  ERROR  UnknownError    An unknown server-side error occurred while processing the command.
        {"errorMessage":"Unable to find element with css selector '[data-id='1568911']'","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"54","Content-Type":"application/json; charset=utf-8","Host":"localhost:12784","User-Agent":"Apache-HttpClient/4.3.2 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"css selector\",\"value\":\"[data-id='1568911']\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/2e1ff0a0-68d7-11e4-ad4c-3105ad572a89/element"}}

为什么常见的CSS2 +选择器喜欢" [data-id =' 1568911']",甚至"#foo",不能通过WebdriverJS在PhantomJS上工作?它是一个PhantomJS错误,一个WebdriverJS错误,还是我在实施过程中犯的错误?

1 个答案:

答案 0 :(得分:1)

  

所以至少对于谷歌页面来说,它应该意味着WebdriverJS对userAgent字符串做错了。但这并不意味着它与原始页面的问题相同。

像这样......

默认的WebdriverJS用户代理值很好。问题(对于我正在测试的页面,至少)来自一段代码大致如下:

if(navigator.onLine){ 
    renderPage()
} else doSomethingElse();

看来PhantomJS总是将navigator.onLine设置为false ...

https://github.com/ariya/phantomjs/issues/10647

卫生署。

那就是说,我无法理解为什么有人会想要在一个总是在线的产品中使用navigator.onLine值。其中一个只能在别人的代码中找到的经典......

痛苦的错误。

感谢您的帮助。