问题
So a coworker and I were discussing making a data object for our e2e tests. From my understanding about data objects they are used for decoupling your test suites. For example, my first test suite is to create an account and to test if the fields are valid and the second test suite logins into the account and does its own tests. I am told it is good to use data objects (not a page object) just incase the first test suite fails when making an account. That way we can use the data object in the second test suite to create a new user just for testing login. My problem is that if my first test suite fails at making an account, why would creating an account in my second test suite pass? Whatever error I get in the First test suite, I should also get in the second test suite right? I have many more questions about data objects and how to use them. I was wondering if someone could explain data objects and how to use/write one.
/***
Test Data Object
***/
var Member = function() {
var unixTime = String(Math.round(new Date()/1000));
this.username = "TestAccount" + unixTime;
this.email = this.username + "@gmail.com";
this.password = "password";
};
Member.prototype.create = function () {
var signup = new signupPage.Signup();
signup.getPage();
signup.memberAs(this.username, this.email, this.password);
};
Member.prototype.login = function () {
var login = new loginPage.Login();
login.getPage();
login.memberAs(this.username, this.password);
};
Member.prototype.logout = function () {
// k.logoutMember();
};
exports.Member = Member;
This is the data object my coworker wrote. We haven't finished writing the tests because we stopped to think about it more, but here are the tests we have so far.
var chai = require('chai');
var chaiAsPromised = require("chai-as-promised");
var expect = chai.expect;
var member = require('./lib/test-data');
chai.use(chaiAsPromised);
describe.only('Member Account Settings and Information', function() {
before(function () {
member.create();
});
before.each(function() {
member.login();
});
describe('My Account', function () {
it('Logging in should enable the "My Account" link.', function() {
member.login();
});
it('Clicking on "My Account" should expand the account options', function() {
});
});
回答1:
I use hashes for my data objects. Here's an example from my protractor_example code on GitHub.
Given a data file:
var UserData = function() {
this.testUser = {'username': 'test', 'password': 'test'};
};
module.exports = new UserData();
Then the spec...
describe ('non-angular login test', function() {
var loginPage = require('../pages/nonAngularLoginPage.js');
var userData = require('../data/userData.js');
it('should goto friend pages on successful login', function() {
loginPage.loginAs(userData.testUser);
expect(browser.getTitle()).toContain('Angular JS Demo');
});
});
来源:https://stackoverflow.com/questions/28727854/using-data-objects-while-e2e-testing-with-protractor