首页 > 解决方案 > 量角器:无法在登录页面中找到元素

问题描述

我是量角器的新手用户,并试图将其用于 angularjs 应用程序,配置文件片段:

 exports.config = {
    directConnect: true,
    capabilities: {
        'browserName': 'chrome'
    },
    framework: 'jasmine',
    specs: ['plugins/./test_spec.js'],

    allScriptsTimeout: 60000,
    getPageTimeout: 30000,
    jasmineNodeOpts: {
        defaultTimeoutInterval: 1240000
    }

};

工作测试用例(规范文件):

describe('Login', function () {
    it('Login Page', function () {
        browser.get('http://localhost:9000/apps/admin/');
        element(by.model('ctrl.user.name'))
        element(by.model('ctrl.user.password'))
        expect(true).toBe(true)
    });
});

失败的测试用例(规范文件):

describe('Login', function () {
    it('Login Page', function () {
        browser.get('http://localhost:9000/apps/admin/');
        element(by.model('ctrl.user.name')).sendKeys("test1");
        element(by.model('ctrl.user.password')).sendKeys("test1");
        element(by.css('[type="submit"]')).click();
        expect(true).toBe(true)
    });
});

尝试使用 sendKeys 登录页面失败但没有通过 sendkeys 测试用例,我收到以下错误:

Failed: script timeout: result was not received in 60 seconds
    (Session info: chrome = 72.0.3626.109)
    (Driver info: chromedriver = 2.46.628402(536cd7adbad73a3783fdc2cab92ab2ba7ec361e1), platform = Windows NT 10.0.17134 x86_64)

我怀疑没有找到元素。请指导我完成这个。

提前致谢

标签: protractor

解决方案


Protractor 是 selenium 的包装器,因此当您打算使用 await/async 方法时,只需通过将 SELENIUM_PROMISE_MANAGER 设置为 false 来禁用它,以便 protractor 承诺与 async/await 方法很好地配合使用。

我还建议使用 pagemodel 设计模式,这将使代码更具可读性。

enter code here


export class loginPageObject{
public emailTextBox: ElementFinder;
public passwordTextBox: ElementFinder;
public signInButton: ElementFinder;
public errorMessage: ElementFinder;

constructor(){      //this.emailTextBox = $("input[type='email']");
    //this.emailTextBox = element(by.css("body > app-root > div > app-login > div > div > form > div > input"));
    this.emailTextBox = $("input[type='email']");
    this.passwordTextBox = $("input[type='password']");
    this.signInButton = $("button[type='submit']");
    this.errorMessage =  $("span");
}

}

上面就是这样一个示例..稍后您可以像以下方式一样使用它


推荐阅读