imnothardcore
imnothardcore

Reputation: 271

how to click same classed element each time on page with protractor

    it('it should click each of the title elements to reveal its content', function(){
        element(by.css('.cardtitle')).click();
    });

I have the above lines which work properly and click the first item in the series of 3 with this class. I need to click all 3 items with this class using protractor, how do I do this? (the are all on the same page, very straightforward)

for whats its worth there will be upcoming instances where there may be 2 or 5 items to click all sharing the same class.

thanks!

Upvotes: 0

Views: 879

Answers (2)

giri-sh
giri-sh

Reputation: 6962

Loop through the elements and click them one after the other using each() function available in protractor. Here's how -

element.all(by.css('.cardtitle')).each(function(elem){
    elem.click();
});

If at all you want to click the elements serially then you need to wait until click is completed resolving its promise. Here's a sample -

element.all(by.css('.cardtitle')).each(function(elem){
    elem.click().then(function(){
        //If you want perform some operation after click, you can do it here.
        browser.sleep(1000);
    });
});

Hope this helps.

Upvotes: 2

masimplo
masimplo

Reputation: 3774

You can get a collection of items and click them individually.

eg.

var cards = element.all(by.css('.cardtitle'));
expect(cards.length).toBe(3);
cards.get(0).click();
cards.get(1).click();
cards.get(2).click();

Upvotes: 2

Related Questions