Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 可以在量角器中单击具有phantomJS的元素吗?_Javascript_Selenium Webdriver_Phantomjs_Protractor - Fatal编程技术网

Javascript 可以在量角器中单击具有phantomJS的元素吗?

Javascript 可以在量角器中单击具有phantomJS的元素吗?,javascript,selenium-webdriver,phantomjs,protractor,Javascript,Selenium Webdriver,Phantomjs,Protractor,我试图在使用PhantomJS作为我的浏览器选择时单击一个按钮,我得到了很多错误 第一次尝试,直接单击按钮: var button = $('#protractorTest'); button.click(); 返回错误: Element is not currently visible and may not be manipulated 尝试调整phantomJS视口的大小似乎没有效果。该按钮位于屏幕的左上角,但不知何故超出了(如果我没记错的话)默认的400x300视口 browser

我试图在使用PhantomJS作为我的浏览器选择时单击一个按钮,我得到了很多错误

第一次尝试,直接单击按钮:

var button  = $('#protractorTest');
button.click();
返回错误:

Element is not currently visible and may not be manipulated
尝试调整phantomJS视口的大小似乎没有效果。该按钮位于屏幕的左上角,但不知何故超出了(如果我没记错的话)默认的400x300视口

browser.manage().window().setSize(1920, 1080);
我似乎无法在任何时间点记录窗口大小,我能得到的最接近的结果是记录未完成的承诺。因此,我不确定屏幕大小是否有任何变化

通过搜索大量其他问题,我尝试了各种执行脚本和不同的方法来选择元素,但迄今为止没有成功

尝试运行执行脚本以单击它时,我发现undefined不是一个函数:

var hiddenElement = browser.findElement(by.id('protractorTest'));
browser.executeScript("arguments[0].click()", hiddenElement).then(function() {
    expect(true).toMatch(true);
});
返回此错误:

Failed: {"errorMessage":"'undefined' is not a function (evaluating 'arguments[0].click()')","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"75","Content-Type":"application/json; charset=utf-8","Host":"localhost:42837","User-Agent":"Apache-HttpClient/4.3.6 (java 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"script\":\"arguments[0].click()\",\"args\":[{\"ELEMENT\":\":wdc:1441214073816\"}]}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/0fa194a0-5196-11e5-a5f1-99fee78af55e/execute"}}
    Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
    System info: host: 'DS5539', ip: '10.4.4.65', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_40'
    Driver info: driver.version: unknown
尝试使用webElement选择器运行测试时返回与上面相同的错误:

var hiddenElement = element(by.id('protractorTest')).getWebElement();
browser.executeScript("arguments[0].click()", hiddenElement).then(function() {
    expect(successPage.isDisplayed).toBeTruthy();
});
尝试使用量角器选择运行测试时会出现不同的错误:

var hiddenElement = $('#protractorTest');
browser.executeScript("arguments[0].click()", hiddenElement).then(function() {
    expect(successPage.isDisplayed).toBeTruthy();
});

Maximum call stack size exceeded

有没有办法使用phantomJS和量角器单击按钮?

首先,如果您选择phantomJS作为目标浏览器,我打赌您在不久的将来会遇到新问题。即使是
量角器
开发人员:

我们建议不要在带有量角器的测试中使用PhantomJS。那里 有很多关于PhantomJS崩溃和行为的报道吗 与真正的浏览器不同

另外,如果您这样做是为了测试,那么您应该让您的测试环境尽可能接近最终用户的环境-您是否看到应用程序的用户使用了
PhantomJS
?-可能不会

如果您选择PhantomJS是因为它的无头特性和没有真正的显示,那么您也可以在无头模式下运行firefox或chrome,请参阅:

还有,读一读这篇关于无头与无头的伟大文章

或者,您可以在使用远程selenium服务器


您采取的最新方法有一个问题-
isDisplayed
应被称为:

var hiddenElement = $('#protractorTest');
browser.executeScript("arguments[0].click()", hiddenElement).then(function() {
    expect(successPage.isDisplayed()).toBeTruthy();
    //                        HERE^
});
或/和,您可以滚动到具有以下内容的图元视图:

添加一个

var EC = protractor.ExpectedConditions;
browser.wait(EC.visibilityOf(button), 5000);

谢谢你的评论!我意识到不推荐使用PhantomJS,我的任务是证明它是否能够满足我们的需求——在TeamCity中运行的自动化测试。这让我无法确信它是可行的!我在显示通话中犯的错误,这只是一个简单的例子。对于这些测试,我一直在使用presenceOf并检查innerHtml值(getText也需要元素可见)。将元素滚动到视图中会遇到与单击相同的问题-有趣的是,即使存在不可见/不可操作的错误。感谢您提供研究链接。我们将很快建立一个BrowserStack环境,这只是希望与TeamCity很好地合作。我也会看看其他的无头浏览器。
var EC = protractor.ExpectedConditions;
browser.wait(EC.visibilityOf(button), 5000);