Exponent Page Objects - TypeError: Object # <Object> does not have a method 'methodName'

I am trying to write a simple test using the page object template - based on docs / page-objects '.

I created a file that describes the page object, and another, using this page object to test the page.

//page object var LoginPage = function() { this.userInput = browser.driver.findElement(by.id('username')); this.pwdInput = browser.driver.findElement(by.id('password')); this.btnEnter = browser.driver.findElement(by.id('btnLogin')); this.get = function(){ browser.get('http://example.com'); }; this.setUser = function (user){ this.userInput.sendKeys(user); }; this.setPasswd = function (password) { this.pwdInput.sendKeys(password); }; this.clickBtnEnter = function (){ btnEnter.click(); };}; 

Specification File:

 var loginPage = require('./LoginPage.js'); describe('myApp', function() { it('should save contract config', function (){ loginPage.get(); loginPage.setUser('userid'); loginPage.setPasswd('passwd'); loginPage.clickBtnEnter(); }); }); 

When starting this test, the following error is displayed: TypeError: Object # does not have a 'get' method - in this line: loginPage.get () ;.

When I looked for this problem, I found various approaches to using page objects in Protractor, for example Astrolable . Now I'm not sure about the proper use of page objects.

Do you have any ideas on how I can fix this test?

Thanks guys.

+1
source share
2 answers

After flashing the syntax (without success), I rewrote the page object using Astrolable . Now it works! My test is as follows:

 //pageobject 'use strict'; var env = require('./environment.js') var LoginPage = function () { browser.driver.get('http://example.com'); }; LoginPage.prototype = Object.create({}, { userInput: { get: function() { return browser.driver.findElement(by.id('username'));}}, pwdInput: { get: function() { return browser.driver.findElement(by.id('password'));}}, btnEnter: { get: function() { return browser.driver.findElement(by.id('btnLogin'));}}, setUser: { value: function (loginName) { this.userInput.sendKeys(loginName); }}, setPasswd: { value: function (loginPass) { this.pwdInput.sendKeys(loginPass); }}, clickBtnEnter: { get: function() { return this.btnEnter.click();}} }); module.exports = LoginPage; 

Specification File:

 'use strict'; var loginPage = require('./LoginPage.js'); describe('myApp', function() { var poLogin = new loginPage(); it('should save contract config', function (){ poLogin.setUser('userid'); poLogin.setPasswd('passwd'); poLogin.clickBtnEnter; }); }); 

Now it works fine. Thanks for answering.

0
source

Try the following:

Make sure your LoginPage.js has the following:

 module.exports = LoginPage; 

Add missing keyword new

 var LoginPage = require('./LoginPage.js'); var loginPage = new LoginPage(); 
+1
source

Source: https://habr.com/ru/post/1210055/