加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 量角器找不到元素

发布时间:2020-12-17 18:04:28 所属栏目:安全 来源:网络整理
导读:在测试我的角度应用程序时,我无法让量角器按预期运行.我的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(e
在测试我的角度应用程序时,我无法让量角器按预期运行.我的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选择器’).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读