当我隔离测试时,它会通过,但是当与其他测试顺序运行时,它会因NoSuchElementError而失败

时间:2015-11-13 03:59:05

标签: protractor

我正在对我们用于运行我们的Protractor测试以在Sauce Labs上运行的页面对象进行一些更改,即调用实用程序方法来获取浏览器和平台,以便我们可以使用相应的测试用户,并在进行更改后在运行测试套件时不断收到NoSuchElementError。

当我隔离注销测试时,它会通过,但是当与任何其他文件一起运行时,它会失败。目前,我只在Chrome上运行登录测试和注销测试,以限制可能的原因。

我们使用页面对象导航到可测试状态,在这种情况下是登录页面对象和仪表板页面对象(登录将您带到仪表板)。

登录页面对象:

'use strict';

var TestUtils = require('../../util/test-utils.js');
var HeaderPageElement = require('../page_elements/header-page-element.js');

var LoginPage = function () {
    var self = this;

    this.get = function () {
        browser.get('http://localhost:9000/index.html');
        this.header = new HeaderPageElement();
        this.loginForm = element(by.name('loginForm'));
        this.usernameInput = element(by.model('credentials.username'));
        this.passwordInput = element(by.model('credentials.password'));
        this.loginButton = element(by.name('loginButton'));
        this.signupLink = element(by.xpath('//a[@ui-sref="signup"]'));
    };

    this.setCredentials = function (username, password) {
        var deferred = protractor.promise.defer();
        var testUtils = new TestUtils();

        testUtils.getCapabilities().then(function (capabilities) {
            return testUtils.getTestUser(capabilities.browserName, capabilities.platform);
        }).then(function (testUser) {
            username = username || testUser.username;
            password = password || testUser.password;
            self.usernameInput.sendKeys(username);
            self.passwordInput.sendKeys(password);
            deferred.fulfill();
        });

        return deferred.promise;
    };

    this.login = function (username, password) {
        return this.setCredentials(username, password).then(function () {
            return self.loginButton.click();
        });
    };

    this.signup = function () {
        return this.signupLink.click();
    };

    this.get();
};

module.exports = LoginPage;

仪表板页面对象:

'use strict';

var LoginPage = require('./login-page.js');
var HeaderPageElement = require('../page_elements/header-page-element.js');
var ProjectCreateModalPageElement = require('../page_elements/project-create-modal-page-element.js');

var DashboardPage = function () {
    var self = this;

    this.get = function () {
        var loginPage = new LoginPage();
        loginPage.login();
        this.header = new HeaderPageElement();
        this.newProjectButton = element(by.name('newProjectButton'));
        this.projectFilterInput = element(by.name('projectFilterInput'));
    };

    this.createNewProject = function (projectTitle, projectTypes) {
        var deferred = protractor.promise.defer();
        this.newProjectButton.click().then(function () {
            var modalPage = new ProjectCreateModalPageElement();
            modalPage.createNewProject(projectTitle, projectTypes);
            deferred.fulfill();
        });
        return deferred.promise;
    };

    this.get();
};

module.exports = DashboardPage;

这些是正在运行的测试。

登录测试:

'use strict';

var LoginPage = require('./pages/login-page.js');

describe('login test', function () {
    var page;

    beforeEach(function () {
        page = new LoginPage();
    });

    it('should be directed to login', function () {
        expect(page.loginForm.isPresent()).toBe(true);
    });

    it('Login button should be disabled', function () {
        expect(page.loginButton.getAttribute('disabled')).toEqual('true');
        page.setCredentials('wrong', 'user').then(function () {
            expect(page.loginButton.getAttribute('disabled')).toEqual(null);
        });
    });

    it('login should fail and remain at login screen', function () {
        page.login('wrong', 'user').then(function () {
            expect(page.loginForm.isPresent()).toBe(true);
        });
    });

    it('login success should redirect to dashboard', function () {
        page.login().then(function () {
            browser.wait(function () {
                return $('#dashboard').isPresent();
            });
            expect($('#dashboard').isDisplayed()).toBe(true);
        });
    });
});

退出测试:

'use strict';

var DashboardPage = require('./pages/dashboard-page.js');

describe('logout test', function () {
    var page;

    beforeEach(function () {
        page = new DashboardPage();
    });

    it('logout success should redirect to login page', function () {
        page.header.logout().then(function() {
            browser.wait(function () {
                return $('#login').isPresent();
            });
            expect($('#login').isDisplayed()).toBe(true);
        });
    });
});

按顺序运行这些测试时得到的错误如下:

NoSuchElementError: No element found using locator: by.model("credentials.username")

它指定的行是DashboardPage对象中的get方法,它实例化一个LoginPage对象并调用login方法以便导航到仪表板:

this.get = function () {
    var loginPage = new LoginPage();
    loginPage.login();
    this.header = new HeaderPageElement();
    this.newProjectButton = element(by.name('newProjectButton'));
    this.projectFilterInput = element(by.name('projectFilterInput'));
};

无论出于何种原因,在调用usernameInput方法时尚未设置登录页面的login

我很确定这与没有正确编码承诺有关,但是我几天都没有成功地反对它。任何帮助将不胜感激。

0 个答案:

没有答案