angularjs – 量角器找不到元素

前端之家收集整理的这篇文章主要介绍了angularjs – 量角器找不到元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在测试我的角度应用程序时,我无法让量角器按预期运行.我的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');

  });
});

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

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

解决方法

您必须等到元素完全出现后才能使用:

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

要么

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

或者您想通过键入以下内容来验证使用的选择器是否正确,方法是输入:document.querySelector(‘您的CSS选择器’).

猜你在找的Angularjs相关文章