Casey
Casey

Reputation: 152

Protractor - get specific element in a list

I have a list of pages in a table and need to click on the next page.

<td><span>1</span></td>
<td><a href="2">2</a></td>
<td><a href="3">3</a></td>

So, if the browser is currently on page 1, I need to click on page 2.

I am currently getting the elements by "td", going through a for loop (or mapping function) to try and find the current page, and then trying to find the next page. I keep getting memory issues or timeouts the way I'm currently trying to solve it.

Mapping function (memory issues) gist: https://gist.github.com/CaseyHaralson/9965492d894565bfe9d7

For loop (timeout) gist: https://gist.github.com/CaseyHaralson/7153be9f437f4e3a7f5c

The for loop also looks like it has the potential issue of not necessarily resolving the pages in the correct order. Note: I can't change the html.

Upvotes: 4

Views: 407

Answers (2)

Brine
Brine

Reputation: 3731

Assuming your current page is has the span, something like this should work...

var clickNextPage = function() {
  $('td span').getText().then(function(pageNum) {
    element(by.cssContainingText('td a', pageNum + 1)).click();
  });
};

Upvotes: 2

yurisich
yurisich

Reputation: 7119

This function should click the href based on the page number you pass in.

var visitPage = function (pageNumber) {
    $('a[href="' + pageNumber + '"']).click();
};

it('should visit page three', function () {
    expect(browser.getCurrentUrl()).toContain('1');
    visitPage('3');
    expect(browser.getCurrentUrl()).toContain('3');
});

Upvotes: 2

Related Questions