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

Protractor - button click calls the callback sooner than desired

我有一个用于登录页面的量角器测试,用于提交信用并检查是否加载了索引页面。我将回调函数传递给按钮单击的 then 函数,假设回调函数将在 click 函数返回的承诺得到解决后调用。

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

但是回调被调用,当页面加载正在进行时它使测试崩溃,因为带有 titlesubMenuItem 的元素仍然没有加载。 错误是,

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

因为,看起来所有问题都来自 手动 angular 引导 ,你会 have to use browser.driver.get():

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");

登录,让它延迟登录(睡眠不好,是的),然后手动获取索引页面。


您还可以通过设置 browser.ignoreSynchronization = true; 来禁用量角器和 angular 之间的同步,但这有很多缺点 - 您必须开始使用显式等待(browser.wait()) 很多。您可以尝试使用此标志并在加载页面之前将其临时设置为 true 并在加载页面之后设置回 false

你试过了吗?:

 element(by.name('normalLogin')).click()
    browser.wait(function() {
      return browser.driver.isElementPresent(by.xpath('//a[@title="Venues"]'))
    }).then(function(){
         var venueLink = by.xpath('//a[@title="Venues"]')
         browser.driver.isElementPresent(venueLink).then(function(){
           console.log("tenant login process successful")
           element(venueLink).click()
        })
    });

Protractor 异步工作,所以

var venueLink = by.xpath('//a[@title="Venues"]')

将在

时执行
browser.wait(function() {
      return browser.driver.isElementPresent(by.xpath('//a[@title="Venues"]'))
    })