Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  66] [ 6]  / answers: 1 / hits: 15253  / 9 Years ago, mon, august 10, 2015, 12:00:00

I’m trying to get text from a page and then use that text further down in the spec to assert on another element.



I’ve pasted a very simple spec you can run that shows you can’t return a value from a function if the function’s return statement is inside a protractor promise return txt; (line 24)…



describe('My Test', function () {
var tempVariable;

it('should go get some text from the page', function () {
browser.get('https://angularjs.org/');
tempVariable = getTextFromElement(); //it appears javascript immediately sets this variable before waiting for protractor to return the value
});

it('should do some random other stuff', function () {
element.all(by.cssContainingText('a', 'Learn')).get(0).click();
element.all(by.cssContainingText('a', 'Case Studies')).get(0).click();
element.all(by.cssContainingText('a', ' Home')).get(0).click();
});

it('should be able to use the text from the first page in this test', function () {
console.log('ntempVariable: ' + tempVariable); //this is undefined!
expect(typeof tempVariable).not.toBe('undefined', 'failed: tempVariable was undefined!');
});
});

function getTextFromElement() {
$('a.learn-link').getText().then(function (txt) {
console.log('nInitial text: ' + txt);
return txt; //how do we return this so it's available to other 'it' blocks?
});
}


Updated snippet of code following @alecxe answer and my comment.



I am attempting to contruct an object from various text on a page and return it to assert on in a later page...



function getRandomProductFromList() {
var Product = function (line, name, subname, units) {
this.line = line;
this.name = name;
this.subname = subname;
this.units = units;
};

var myProduct = new Product();

myProduct.line = 'Ford';
myProduct.units = 235;

//select a random product on the page and add it to 'myProduct'
var allProducts = element.all('div.product');
allProducts.count().then(function (count) {
var randomIndex = Math.floor(Math.random() * count);
var productName = allProducts.get(randomIndex);

productName.getText().then(function (prodName) {
myProduct.name = prodName;
productName.click();
});
});

//If a sub-product can be chosen, select it and add it to 'myProduct'
var subproduct = $('div.subproduct');
subproduct.isDisplayed().then(function (subProductExists) {
if (subProductExists) {
subproduct.getText().then(function (subProductName) {
myProduct.subname = subProductName;
});
subproduct.click();
}
}, function (err) {});

return myProduct;
}

More From » node.js

 Answers
14

Thanks to @alecxe for starting me off on the right foot.



I found a solution I am now using quite a bit after reading this.



By passing an object by reference, you can add properties on the fly and use them later in your spec.



Example:



describe('My Test', function () {
var tempObject = {};

it('should go get some text from the page', function () {
browser.get('https://angularjs.org/');
getTextFromElement(tempObject); //pass an object by reference to populate with page text
});

it('should do some random other stuff', function () {
$('div.someDiv').click();
});

it('should be able to use the text from the first page in this test', function () {
console.log(tempObject.textFromFirstPage); //works!
});
});

function getTextFromElement(tempObject) {
$('a.some-link').getText().then(function (txt) {
tempObject.textFromFirstPage = txt;
});
}

[#65462] Friday, August 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
everett

Total Points: 317
Total Questions: 112
Total Answers: 109

Location: Philippines
Member since Sat, Jul 11, 2020
4 Years ago
;