量角器 - 按钮单击会比预期更快地调用回调 [英] Protractor - button click calls the callback sooner than desired
问题描述
我对登录页面进行了量角器测试,提交了信用卡并检查是否加载了索引页面。我将回调函数传递给按钮单击的然后
函数,假设在返回的承诺后点击
功能已解决。
I have a protractor test for login page, that submits creds and checks if index page is loaded. I am passing a callback function to the button click's then
function, assuming the callback will be invoked after the promise returned by click
function is resolved.
var protractor = require('protractor')
describe('E2E: main page', function() {
beforeEach(function() {
browser.get('http://localhost:8001/login.html/');
});
it("login in the portal", function(){
var d = protractor.promise.defer();
element(by.model('loginParams.email')).sendKeys('saravana0209@r.com');
element(by.model('loginParams.password')).sendKeys('password');
element(by.name('normalLogin')).click().then(function(){
//it crashes here before selecting the sub menu item
element(by.xpath('//a[@title="subMenuItem"]')).click()
console.log("sub-menu item has been chosen in the ribbon")
setTimeout(function() { d.fulfill('ok')}, 50000)
})
expect(d).toBe('ok');
})
});
但是当页面加载正在进行并且崩溃测试时,回调被调用,因为 title
, subMenuItem
的元素仍未加载。
错误是,
But the callback is getting invoked, when the page loading is in progress and it crashes the test, since the element with title
, subMenuItem
is still not loaded.
Error is,
Error: Failed: invalid selector: Unable to locate an element with the xpath expression //a[@title="Venues"] because of the following error:
TypeError: Failed to execute 'createNSResolver' on 'Document': parameter 1 is not of type 'Node'.
推荐答案
您可以等待子菜单可见点击前:
var EC = protractor.ExpectedConditions;
element(by.name('normalLogin')).click().then(function() {
var subMenu = element(by.xpath('//a[@title="subMenuItem"]'));
browser.wait(EC.visibilityOf(subMenu), 5000);
subMenu.click();
console.log("sub-menu item has been chosen in the ribbon");
setTimeout(function() { d.fulfill('ok')}, 50000);
});
因为看起来所有的问题都是来自手动角度引导,你会必须使用 browser.driver.get()
:
如果您的页面使用手动引导程序,Protractor将无法使用browser.get加载您的页面
。而是使用基本webdriver实例
- browser.driver.get。这意味着Protractor不知道您的页面何时完全加载,并且您可能需要将等待语句添加到
,确保您的测试避免竞争条件。
If your page does manual bootstrap Protractor will not be able to load your page using browser.get. Instead, use the base webdriver instance - browser.driver.get. This means that Protractor does not know when your page is fully loaded, and you may need to add a wait statement to make sure your tests avoid race conditions.
这可能导致类似:
element(by.name('normalLogin')).click();
browser.sleep(3000);
browser.driver.get("index.html");
登录,让它通过延迟登录(睡眠不好,是的)并手动获取之后的索引页。
Login, let it log you in by having a delay (sleep is bad, yeah) and manually get the index page after.
您还可以通过设置<$ c $来处理量角器和角度之间禁用的同步c> browser.ignoreSynchronization = true; ,但这有很多缺点 - 你必须开始使用Explicit Waits( browser.wait()
)很多。你可以尝试使用这个标志并暂时将其设置为 true
,然后再加载页面并设置回 false
之后。
You can also work with a disabled synchronization between protractor and angular by setting the browser.ignoreSynchronization = true;
, but this have a lot of drawbacks - you would have to start using Explicit Waits (browser.wait()
) a lot. You can though try to play around with this flag and set it temporarily to true
before loading a page and set back to false
after.
这篇关于量角器 - 按钮单击会比预期更快地调用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!