量角器没有找到元素

see*_*per 6 angularjs protractor

在测试我的角度应用程序时,我无法让量角器按预期运行.我的spec文件看起来像这样:

describe('app login', function() {
   it('should allow admin user to log in', function() {
       browser.get('http://localhost:3008');

    //we can find the log in link
    expect(element(by.id('login-link')).getText()).toContain('Log in');

    //open login dialog
    element(by.id('login-link')).click();
    browser.ignoreSynchronization = true;
    browser.sleep(1000);

    //enter credentials
    element(by.id('login-username')).sendKeys('User1');
    element(by.id('login-password')).sendKeys('Password1');
    browser.sleep(1000);

    //log in
    var el = element(by.id('login-btn'));
    //WORKS IF BELOW LINE IS COMMENTED OUT 
    el.click();
    browser.sleep(1000);

    //display confirms login
    expect(element(by.id('user-display')).getText()).toContain('User1');

  });
});
Run Code Online (Sandbox Code Playgroud)

请注意,我在开始时遇到同步错误,这就是我将ignoreSynchronization标志设置为true以及所有browser.sleeps的原因.

现在就是这样的事情:如果我删除el.click()语句(以及最后的期望调用),测试将会很好.但是,一旦包含该行,我得到NoSuchElementError:找不到使用locator的元素:By.id("login-username").请注意,此元素不是我实际尝试单击的元素,这是奇怪的一部分.

小智 0

您必须等待元素完全显示,方法是:

browser.driver.wait(function() {
            return element(by.css(YourElement'/login-username)).isDisplayed().then(function(IsVisible) {
                return IsVisible;
            });
        }, 10000);
Run Code Online (Sandbox Code Playgroud)

或者

browser.driver.wait(function() {
            return element(by.css(YourElement'/login-username)).isPresent().then(function(IsVisible) {
                return IsVisible;
            });
        }, 10000);
Run Code Online (Sandbox Code Playgroud)

或者您想通过使用 GoogleChrome 控制台输入以下内容来验证所使用的选择器是否正确: document.querySelector('Your CSS Selector')。