简体   繁体   中英

How can I click on the same element multiple times without nesting? (Selenium-Webdriver + NodeJS + Mocha)

I have a small jQuery plugin, which takes a list of divs (which contain links and images) and turns them into a list you can scroll through by clicking up and down arrows.

I'm writing automated tests using selenium-webdriver, and the tests are written in Node + Chai and are run using Mocha. One of the tests scrolls all the way to the bottom of the list by clicking the down arrow a bunch of times, and right now that code looks like this:

driver.findElement(by.css('.module-slider-1 > div.next_arrow')).click().then(function() {
        driver.findElement(by.css('.module-slider-1 > div.next_arrow')).click().then(function() {
            driver.findElement(by.css('.module-slider-1 > div.next_arrow')).click().then(function() {
                driver.findElement(by.css('.module-slider-1 > div.next_arrow')).click().then(function() {
                    driver.findElement(by.css('.module-slider-1 > div.next_arrow')).click().then(function() {
                        driver.findElement(by.css('.module-slider-1 > div.next_arrow')).click().then(function() {
                            expect('.module-slider-1 div.moduleItem.active img').dom.to.have.attribute('src', slider1FinalTopImg);
                        });
                    });
                });
            });
        });
    });

Basically, I'd like to be able to click on an element multiple times without having to nest the code this much.

It's tough finding an answer to this question via Google or the docs, as most searches return results for Python, Java, or C#. Any help would be appreciated!

Recursion would of course be quite an easy way - wrap the call in a function and call it again in then block until satisfied. Or since it looks like you have a bunch of promises at hand you should be able to do (untested)

var clicketyclick = function() {
  return driver.findElement(by.css('.module-slider-1 > div.next_arrow')).click();
};

clicketyclick()
.then(clicketyclick)
.then(clicketyclick)
.then(clicketyclick)
.then(clicketyclick)
.then(clicketyclick)
.then(function() {
  expect(...);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM