gpt4 book ai didi

javascript - 是否可以在 Protractor 中单击带有 phantomJS 的元素?

转载 作者:行者123 更新时间:2023-11-29 10:11:37 25 4
gpt4 key购买 nike

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

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

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

返回错误:

Element is not currently visible and may not be manipulated

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

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

我似乎无法在任何时间点记录窗口大小,我能得到的最接近的记录是一个未完成的 promise 。所以,我不确定屏幕尺寸是否发生了变化。

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

尝试运行一个执行脚本来点击它,我遇到了 undefined is not a function 的问题:

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();
});

尝试使用 Protractor 选择运行测试会出现不同的错误:

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

Maximum call stack size exceeded

有什么方法可以使用 phantomJS 和 Protractor 点击按钮吗?

最佳答案

首先,如果您选择 PhantomJS 作为您的目标浏览器,我敢打赌您在不久的将来会带着新问题回到 SO。甚至 protractor 开发人员 recommend against it :

We recommend against using PhantomJS for tests with Protractor. There are many reported issues with PhantomJS crashing and behaving differently from real browsers.

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

如果您选择使用 PhantomJS 因为它是 headless 的并且没有真正的显示,您也可以在 headless 模式下运行 firefox 或 chrome,请参阅:

此外,请阅读此 @Louis's answer - 关于 headless 与有头的好文章。

或者,您可以在 BrowserStack or Sauce Labs or similar services 使用远程 selenium 服务器.


您采用的最新方法有问题 - isDisplayed 应该被调用:

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

或者/并且,您可以使用 scrollIntoView() 滚动到某个元素的 View 中:

var button  = $('#protractorTest');
browser.executeScript("arguments[0].scrollIntoView();", button.getWebElement()).then(function () {
button.click();
});

添加 explicit wait也可能有帮助:

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

关于javascript - 是否可以在 Protractor 中单击带有 phantomJS 的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32359702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com