它是可能的点击一个元素与phantomJS在量角器

Is it possible to click an element with phantomJS in protractor?

本文关键字:元素 一个 phantomJS 量角器      更新时间:2023-09-26

我试图点击一个按钮,而使用PhantomJS作为我选择的浏览器,我得到很多错误。

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

var button  = $('#protractorTest');
button.click();

返回错误:

Element is not currently visible and may not be manipulated

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

browser.manage().window().setSize(1920, 1080);

我似乎无法在任何时间点记录窗口大小,我能得到的最接近的是记录未完成的承诺。所以,我不确定屏幕大小是否会改变。

通过搜索许多其他问题,我尝试了各种执行脚本,不同的方法来选择元素,到目前为止还没有成功。

试图运行一个执行脚本点击它,我得到未定义的问题不是一个函数:

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作为您的目标浏览器,我打赌您将在最近的将来带着一个新问题回到SO。即使是protractor开发者也不建议这么做:

我们建议不要使用PhantomJS与Protractor进行测试。在那里有许多报告的问题与PhantomJS崩溃和行为与真实的浏览器不同。

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

如果你选择PhantomJS是因为它的无头性质和缺乏真正的显示,你可能会在无头模式下运行firefox或chrome,参见:

  • 量角器与任何无头浏览器?
  • 什么是一个好的无头浏览器运行量角器?

还有,看看这个@Louis的回答——关于headless和headful的好文章。

或者,您可以在BrowserStackSauce Labs或类似的服务上使用远程硒服务器。


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

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

或者/And,你可以使用scrollIntoView():

滚动到一个元素的视图
var button  = $('#protractorTest');
browser.executeScript("arguments[0].scrollIntoView();", button.getWebElement()).then(function () {
    button.click();
});

添加显式等待也可能有所帮助:

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