Having trouble retrieving specific values from my page object. The getText() method is returning the entire object instead of just the text, likely due to it being a Promise.
I can provide my code if necessary, but I'm aiming to achieve something similar to this common online example for simplicity:
var AngularHomepage = function() {
var nameInput = element(by.model('yourName'));
var greeting = element(by.binding('yourName'));
this.get = function() {
browser.get('http://www.angularjs.org');
};
this.setName = function(name) {
nameInput.sendKeys(name);
};
this.getGreetingText = function() {
return greeting.getText();
};
};
module.exports = new AngularHomepage();
Here's the modified spec file with added console logging:
var angularHomepage = require('../pages/AngularPage');
describe('angularjs homepage', function() {
it('should greet the named user', function() {
angularHomepage.get();
angularHomepage.setName('Julie');
var log = angularHomepage.getGreetingText();
expect(angularHomepage.getGreetingText()).toEqual('Hello Julie!');
console.log(log);
});
});
The test passes successfully, however when logging the result, the entire object is displayed rather than just "Hello Julie!". This raises two main concerns:
a. Is the above method suitable for an "expect" statement with getText()? Despite the conflicting outputs, why does the test pass while the console reflects the entire object? Could it be related to the promise being fulfilled by the "expect"?
b. In the following snippet, I have updated the Page Object method to resolve the promise and accurately display "Hello Julie!" on the console. Although this approach works, utilizing "expect" directly within the method seems unconventional. Is there a better way to simply obtain the element's text without incorporating "expect" in the Page Object?
this.getGreetingText = function() {
greeting.getText().then(function (text) {
console.log(text);
expect(text.toEqual('Hello Julie!'));
});
// expect is done, return not required.
//return greeting.getText();
};
Considering the perspective of a tester, comparing the actual text seems more accurate than the initial method. If using the first example proves acceptable and discourages placing "expect" in the Page Object, then that could be the way forward!