1

I have a table where i have certain links. As this table is very dynamic, i want to search for a link.

this.invoiceTable.element(by.cssContainingText('a','1616050'));

If not found on current page, i want to click on Next 30 records button to get next 30 records.

this.invoiceTable.element(by.cssContainingText('a','Next 30')).click();

and again start searching for previous link on next page. I am not sure how to achieve this and what error should i record for element not found so that it click on Next 30 link and again start searching.

Can someone help with right approach to record error for element not found in table and then click next button followed by search again?

Tried using below code but it is throwing error on calling test()

    test() {
                var invoicetable1 = element(by.css('[id$="MainContent_uxInvoiceList"]'));   
browser.isElementPresent(by.cssContainingText('a','1626547')).then(function(isPresent) {
            if (isPresent) {
                invoicetable1.element(by.cssContainingText('a','1626547')).click();
            } else {
                invoicetable1.element(by.cssContainingText('a','Next 30 Invoices')).click();
                test();
            }
    }); 
    }
NewWorld
  • 764
  • 1
  • 10
  • 31

2 Answers2

2

You can apply the isElementPresent() check recursively, something along these lines:

function test() {
    this.invoiceTable.isElementPresent(by.cssContainingText('a','1616050')).then(function(isPresent) {
        if (!isPresent) {
            this.invoiceTable.element(by.cssContainingText('a','Next 30')).click();
            test();
        }
    }); 
}
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I am not able to use test again somehow. This function is my pageobject which i have created as a class. Inside class, I have defined my function as defined in my updated question. I am getting error while calling test inside function stating test is not a function. – NewWorld Nov 02 '16 at 17:07
  • Error i am getting: `Failed: test is not defined` I also tried to use this.test() but then i am getting error `Failed: Cannot read property 'test' of undefined` – NewWorld Nov 02 '16 at 17:10
  • @NewWorld you cannot use `this` inside the callback - create a closure: `var self = this` on top of the `test()` function and use `self.test()` when calling itself recursively. For more info please see http://stackoverflow.com/questions/337878/var-self-this. – alecxe Nov 02 '16 at 17:15
  • Thanks @alecxe. Looks like i need to learn more on javascript. – NewWorld Nov 02 '16 at 17:21
1

New usage as recommended by alecxe which solved my issue.

 test() {
                var invoicetable1 = element(by.css('[id$="MainContent_uxInvoiceList"]'));   
**var self = this;**
browser.isElementPresent(by.cssContainingText('a','1626547')).then(function(isPresent) {
            if (isPresent) {
                invoicetable1.element(by.cssContainingText('a','1626547')).click();
            } else {
                invoicetable1.element(by.cssContainingText('a','Next 30 Invoices')).click();
                **self.test();**
            }
    }); 
    }
NewWorld
  • 764
  • 1
  • 10
  • 31