Dan Hodson
Dan Hodson

Reputation: 309

Protractor conditional testing

I am attempting to run a suite of tests, and one of the tests relies on whether an element is displayed on the page. The element in question is pagination in a table.

The table is always present, but the pagination is only displayed if the record count is over 20. I am attempting to test the deletion of the last record in the table and so I have either delete the last record, OR click the 'last' button in the pagination, and then delete the final record.

I have attempted to implement the following SO answer, see below code:

it('should test the deletion of a record against whether pagination exists or not', function() {
    element(by.css('.pagination')).isDisplayed().then(function(present) {
        if(present) {
            // Click last button and perform delete on last record
        } else {
            // perform delete on last record
        };
    });
});

However, when running the test, Protractor is moaning that No element found using locator: By.cssSelector(".pagination") when the page has a record count of less than 20 and the pagination is now displayed.

I have passing test for both instances when I know that pagination is present or not, but to keep it as DRY as possible I want to conditionally check and ensure both sets of tests are passable dependant on the record count.

Any help would be much appreciated!

Upvotes: 2

Views: 2003

Answers (1)

Iamisti
Iamisti

Reputation: 1710

The problem is that you have to use isPresent instead of isDisplayed. It might help you having a true/false value.

Upvotes: 2

Related Questions