Here is a snippet of code that extracts text from a web page, selects an input box, clears it, and then enters the extracted text into the input box:
var pText = browser.element(by.css('some_tag')).getText();
var inputbox = browser.element(by.css('[data-aid="login-app-input-username"]'));
inputbox.clear();
// if pText is defined here, everything is ok.
inputbox.then(
function(e){
pText.then(
function(text) {
e.sendKeys(text);
});
});
The issue here is that the sendKeys()
function is being executed before the clear()
function, even though the code is written in order. Moving the declaration of pText
below the clear()
function solves the problem.
My question is how can we ensure that the above code runs in the correct order without relying on the position of the pText
declaration. It's worth noting that using a promise for sendKeys()
is not the solution I am seeking.