クラススコープの理解に問題があります。また、システム内でオブジェクト、プロトタイプなどを利用する方法があります。私は正しいことをしているようですが、それは機能していないと思います。誰かが私にそれを説明したり、正しい方向に指摘してくれることを願っています。javascript、protractor、classesのトラブル
ここに状況はあります: 関連するテキストラベルを持つ入力ボックスを処理する一般的なページオブジェクトと検証オブジェクトを作成しようとしています。状況は、ページオブジェクト構造と同じクラスに検証がある場合には動作するように見えますが、私は最近、それが悪い設計であることを知っています。
これはAngular 2 rc1サイトです。私は分度器3.3.0を使用しており、webdriver-managerを使ってセレンとchromedriverのプロジェクトバージョンを最新にしています。
だからここに(ファイル名は、各セクションの開始時にコメントしている)私がやったものだ:
'use strict';
/*
* InputLabelPageObject.js
*
* This object will provide basic methods for an Input box with an attached label.
* It is expected that the label will have an element called "label" and an element called "input"
*/
module.exports = InputLabelPageObject;
/**
* Create an object that will provide methods for an input/label combination of elements.
*
* @param container The selector for the __container of the input/label combination of elements.
*/
function InputLabelPageObject(container) {
this.Container = container;
}
InputLabelPageObject.prototype = {
constructor: InputLabelPageObject,
/**
* Return the element for the label of the input/label combination of elements.
*
* @returns {protractor.element}
*/
getLabel: function() {
return this.Container.$('label');
},
/**
* Return the element for the input of the input/label combination of elements.
*
* @returns {ElementFinder}
*/
getInput: function() {
return this.Container.$('input');
},
/**
* Return the text shown in the input of the input/label combination of elements.
*
* @returns {Promise}
*/
getValue: function() {
return this.getInput().getAttribute('value');
},
/**
* Get the placeholder text shown in the input of the input/label combination of elements.
*
* @returns {Promise}
*/
getPlaceholder: function() {
return this.getInput().getAttribute('placeholder');
},
/**
* Clears the input element then puts the text from data into the input element.
*
* @param data The text to be entered into the input element.
*/
sendKeys: function (data) {
var el = this.getInput();
el.clear().then(function() {
return el.sendKeys(data);
});
}
};
-
'use strict';
/*
* InputLabelVerification.js
*
* Provide verification methods associated with an Input and Label
* combination of elements.
*/
module.exports = InputLabelVerifications;
var inputLabelPageObject;
function InputLabelVerifications(inputLabelPageObject) {
this.__setPageObject(inputLabelPageObject);
}
InputLabelVerifications.prototype = {
constructor: InputLabelVerifications,
__setPageObject: function (ilpo) {
inputLabelPageObject = ilpo;
},
/**
* Verify the text on the label of the input/label combination of elements.
*
* @param expected The expected text on the label.
*/
verifyText: function (expected) {
//console.log('Asserting text [' + expected + ']');
expect(inputLabelPageObject.getLabel()).toEqual(expected);
},
/**
* Verify the text shown in the input of the input/label combination of elements.
*
* @param expected The expected text in the input element.
*/
verifyValue: function (expected) {
//console.log('Asserting input value [' + expected + ']');
expect(inputLabelPageObject.getValue()).toEqual(expected);
},
/**
* Verify the placeholder text shown in the input of the input/label combination of elements.
*
* @param expected The expected text of the placeholder.
*/
verifyPlaceholder: function (expected) {
//console.log('Verifying placeholder text [' + expected + ']');
expect(inputLabelPageObject.getPlaceholder()).toEqual(expected);
}
};
-
'use strict';
/*
* LoginPageObject.js
*
*/
var InputLabelPageObject = require('./generics/InputLabelPageObject.js');
module.exports = LoginPageObject;
var __container = $('login-component');
var username = new InputLabelPageObject(__container.$('form:nth-child(2) > div:nth-child(1)'));
var password = new InputLabelPageObject(__container.$('form:nth-child(2) > div:nth-child(2)'));
/**
* Create an object that contains the methods necessary to perform actions against the LoginPageObject page.
*
* @param url The base URL string. If not undefined, it will load the url+'/login' page.
* @constructor new LoginPageObject('http://localhost:9000');
*/
function LoginPageObject(url) {
if (url) {
this.loadPage(url)
}
}
LoginPageObject.prototype = {
constructor: LoginPageObject,
loadPage: function (url) {
url = url + '/login';
console.log('Loading page: '+ url);
browser.get(url);
},
welcome: {
/**
* Return the element for the Welcome text
*
* @returns {ElementFinder}
*/
get: function() {
return __container.$('section:first-child h1:first-child');
},
},
/**
* Return an InputLabelPageObject object specific for the username input and label elements.
*/
username: username,
/**
* Return an InputLabelPageObject object specific for the password input and label elements.
*/
password: password,
loginButton: {
/**
* Return the element for the login button.
*
* @returns {ElementFinder}
*/
get: function() {
return __container.$('form > button');
},
/**
* Click the LoginPageObject button.
* @returns {*|void|webdriver.promise.Promise<void>|ActionSequence|!webdriver.promise.Promise.<void>}
*/
click: function() {
return this.get().click();
}
}
};
-
'use strict';
/*
* LoginPageVerifications.js
*/
var LoginPageObject = require('../pageObjects/LoginPageObject');
var verifyText = require('./generics/VerifyText');
var inputLabelVerifications = require('./generics/InputLabelVerifications');
module.exports = LoginPageVerifications;
var __loginPageObject = new LoginPageObject();
function LoginPageVerifications(url) {
if (url) {
__loginPageObject = new LoginPageObject(url);
}
}
LoginPageVerifications.prototype = {
constructor: LoginPageVerifications,
loginPageObject: new LoginPageObject(),
welcome: {
verifyText: function (expected) {
verifyText(__loginPageObject.welcome.get(), expected);
}
},
username: new inputLabelVerifications(__loginPageObject.username),
password: new inputLabelVerifications(__loginPageObject.password),
loginButton: {
verifyText: function (expected) {
verifyText(__loginPageObject.loginButton.get(), expected);
}
},
/**
* Performs the actions of logging in. That is, enter the username and password values,
* then click the LoginPageObject button. This does *not* verify page load.
*
* @param username The username to login with.
* @param password The password to login with.
*/
doLogin: function (username, password) {
var uPromise = __loginPageObject.username.sendKeys(username);
var pPromise = __loginPageObject.password.sendKeys(password);
protractor.promise.asap(this.username.verifyValue(username));
protractor.promise.asap(this.password.verifyValue(password));
protractor.promise.all([uPromise, pPromise]).then(this.loginButton.click());
},
/**
* Verifies all page elements' text or other default attributes.
*
* @param welcomeText The expected Welcome text
* @param userText The expected username label text.
* @param userPlaceholder The expected username's input element's placeholder text.
* @param passText The expected password label text.
* @param passPlaceholder The expected password's input element's placeholder text.
* @param loginText The expected login button text.
*/
verifyPage: function (welcomeText, userText, userPlaceholder, passText, passPlaceholder, loginText) {
this.welcome.verifyText(welcomeText);
this.username.verifyText(userText);
this.username.verifyPlaceholder(userPlaceholder);
this.password.verifyText(passText);
this.password.verifyPlaceholder(passPlaceholder);
this.loginButton.verifyText(loginText);
}
};
-
'use strict';
/*
* login-spec.js
*/
var LoginPageVerifications = require('../components/actions/LoginPageVerifications');
var myUrl = 'http://localhost:3000';
describe('My Login Page test', function() {
var loginPage;
beforeAll(function() {
loginPage = new LoginPageVerifications(myUrl);
});
it('should verify username input and label values', function() {
var welcomeText = 'Thank you for visiting my login page';
var userText = 'Username';
var userPlaceholder = 'Enter your username';
var passText = 'Password';
var passPlaceholder = 'Enter your password';
var loginText = 'Login';
loginPage.username.verifyText(userText);
// loginPage.verifyPage(welcomeText, userText, userPlaceholder, passText, passPlaceholder, loginText);
});
});
私は通常見ている結果: 私はvar inputLabelPageObject
を残したり、唯一のコンストラクタ関数に値を設定しようInputLabelVerification.jsに、私はFailed: Cannot read property 'getLabel' of undefined
を取得する場合。それで、私はそれを上記のように設定しなければならないと考えました。 LoginPageVerification.jsで
A Jasmine spec timed out. Resetting the WebDriver Control Flow.
F
Failures:
1) My Login Page test should verify username input and label values
Expected ({ ptor_: ({ controlFlow: Function, schedule: Function,
setFileDetector: Function, getSession: Function, getCapabilities: Function,
quit: Function, actions: Function, touchActions: Function,
executeScript: Function, executeAsyncScript: Function, call: Function,
wait: Function, sleep: Function, getWindowHandle... }) }) to equal 'Username'.
、私がテストし、他の検証(歓迎とloginButton)が正常に動作することを保証しました:
私が取得するように見える最も近い、私は次の応答を得るときです。また
、ログイン-spec.jsから、私はこの行を追加した場合:
expect(loginPage.loginPageObject.username.getLabel().getText()).toEqual(userText);
この期待がテストに合格します。
私は答えを持っています。そして今、私は愚かに感じます。私が2日以内に答えると、私は投稿します。 (これは正しいことをするのに数日しかかかりませんでした。)基本的には、InputLabelPageObjectをInputLabelPageObjectから呼び出すと、ElementFinderを返します。それはそのまま文字列を返しません必要です。 InputLabelPageObjectでは、次の行を追加しました: 'getText:function(){this.getLabel()。getText(); } 'を呼び出し、検証オブジェクトで' getText() '関数を呼び出し、すべて期待どおりに動作しています。 – Machtyn