我的应用程序包含angularJS和非angularJS页面。 因此我使用量角器进行黄瓜的e2e测试。 我正在尝试使用pageObjects组织测试。
文件夹结构如下:
功能
|_ step_definitions
|- login.steps.js
gmail.feature
pageObject
|- createAccount.js
|- gmailPage.js
protractor.conf.js
gmail.feature
Feature: Gmail login page
@dev
Scenario: Create new gmail account
Given I see gmail home page
When I Create an account
Then I should see account creation form
gmailPage.js
'use strict';
var gmail = function () {
this.btnCreateAccount = driver.findElement(By.css('#gmail-create-account'));
};
module.exports = gmail;
createAccount.js
'use strict';
var ca = function () {
this.inpFirstName = driver.findElement(By.css('#firstname'));
};
module.exports = ca;
login.steps.js
'use strict';
var expect = require('chai').expect;
var gmail = new (require('../../pageObject/gmailPage'))();
var ca = new (require('../../pageObject/createAccount'))();
module.exports = function(){
this.Given(/^I see gmail home page$/, function (callback) {
driver.getCurrentUrl().then(function(url){
expect(url).to.contain('https://www.gmail.com/intl/en/mail/help/about.html');
callback();
});
});
this.When(/^I Create an account$/, function (callback) {
gmail.btnCreateAccount.click();
callback();
});
this.Then(/^I should see account creation form$/, function (callback) {
callback();
});
};
问题:
当我运行此测试时,我得到"NoSuchElementError"
,因为它正在查找createAccount.js中的WebDriver.findElement(By.cssSelector("#firstname"))
。
我的问题是为什么webdriver试图在调用之前需要的文件中查找pageObjects。 为什么使用量角器驱动程序时这个用例相同?
答案 0 :(得分:0)
尝试在登录步骤模块中实例化页面对象。
'use strict';
var expect = require('chai').expect;
var gmailPage = require('../../pageObject/gmailPage');
var createAccountPage = require('../../pageObject/createAccount');
module.exports = function(){
var gmail = new gmailPage();
var ca = new createAccountPage();
this.Given(/^I see gmail home page$/, function (callback) {
driver.getCurrentUrl().then(function(url){
expect(url).to.contain('https://www.gmail.com/intl/en/mail/help/about.html');
callback();
});
});
this.When(/^I Create an account$/, function (callback) {
gmail.btnCreateAccount.click();
callback();
});
this.Then(/^I should see account creation form$/, function (callback) {
callback();
});
};