V
V
Voll.2018-10-23 16:15:53
JavaScript
Voll., 2018-10-23 16:15:53

How to return an unresolved promise?

I work with Selenium, and I want to highlight a separate function. Structure of the existing code:

driver.then(_=> driver.wait(until.elementLocated(By.css()))
.then(_=>driver.findElement(By.css()).click());

I want to replace with: The function looks like
driver.then(_=> driver.getElementByCss().click());
function getElementByCss(path){
return   driver.wait(until.elementLocated(By.css()))
.then(_=>driver.findElement(By.css()))
}

But the function, waiting for the element, decides driver.findElement(), and returns the result, and not the "driver.findElement()" object itself,
how to make it return the object of the unresolved promise?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton fon Faust, 2018-10-23
@vollthegreat

First, this won't work because promises don't have a click() method, and your
driver.getElementByCss().click() call literally reads: call the driver.getElementByCss() function result method you want to was a promise.
For that matter, it should be something like this:

driver.then(_=> driver.getElementByCss().then(el=>el.click()));

driver.prototype.getElementByCss(){
    return new Promise((resolve, reject) => {
        driver.wait(until.elementLocated(By.css())).then(_=>resolve(driver.findElement(By.css()));
    });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question