量角器 - 按钮单击会比预期更快地调用回调

3 javascript jasmine angularjs selenium-webdriver protractor

我对登录页面进行了量角器测试,提交了信用证并检查是否加载了索引页面.我将回调函数传递给按钮单击的then函数,假设在click函数返回的promise 被解析后将调用回调函数.

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');
  })
});
Run Code Online (Sandbox Code Playgroud)

但回调得到调用,当页面加载过程中,它崩溃的考验,因为与元素title,subMenuItem仍然没有加载.错误是,

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'.
Run Code Online (Sandbox Code Playgroud)

ale*_*cxe 9

在点击之前,您可以等待子菜单可见:

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);
});
Run Code Online (Sandbox Code Playgroud)

因为,看起来所有的问题都来自手动角度引导,你必须使用browser.driver.get():

如果您的页面执行手动引导,则Protractor将无法使用browser.get加载您的页面.而是使用基本webdriver实例 - browser.driver.get.这意味着Protractor不知道您的页面何时完全加载,并且您可能需要添加等待语句以确保您的测试避免竞争条件.

这可能会导致类似于:

element(by.name('normalLogin')).click(); 
browser.sleep(3000); 
browser.driver.get("index.html");
Run Code Online (Sandbox Code Playgroud)

登录,让它通过延迟登录你(睡眠不好,是的)并手动获取索引页面.


你也可以通过设置browser.ignoreSynchronization = true;,使用量角器和角度之间的禁用同步,但这有很多缺点 - 你必须开始大量使用Explicit Waits(browser.wait()).你可以尝试使用这个标志,并true在加载页面之前暂时设置它并重新设置false.