1

I have a page with a button, which is being controlled by a backend job. once job completes => button gets enabled. user have to manually refresh the page to see the button enabled.

My Goal:

  1. Check if the button is enabled
  2. If the button is Enabled = exit the function and proceed to the rest of control flow
  3. If the button is NOT enabled, wait 10 seconds and refresh the page to check If the button is Enabled, and so on
  4. Do not go over 10 iterations of waiting. if the button is still not available - exit the function and fail the test

I'm using Protractor with JavaScript

here is what I have so far:

----------my control flow-----

it ('waiting for button', () => {
        itTopicReg.waitForBtn();
        });

---------my help script from itTopicReg--------

proceedToQaBtn = element(by.buttonText('Proceed to Qa Environment'))
nIntervId = null



 waitForBtn() {
        this.nIntervId = setInterval(this.isBtnEnabled(), 10000);
    };

isBtnEnabled() {
    let count = 0;
    if (this.proceedToQaBtn.isEnabled()) {
         expect(this.proceedToQaBtn.isDisplayed()).toBe(true);
    } else if (count < 10) 
        count++;
        browser.navigate().refresh();
    } else if (count >= 10) {
        clearInterval(this.nIntervId);
    };

I get the following error:

 ✗ waiting for button
            - Failed: "callback" argument must be a function
                at exports.setInterval (timers.js:411:11)
                at ItTopicReg.waitForBtn 

I'm new to Protractor and to JavaScript, pardon if that's something obvious I've searched here and have not found resent clean solution (like Protractor : wait for element to become invisible/hidden or Refreshing page until element appears - JAVA - Selenium)

toxicBurn
  • 127
  • 11

1 Answers1

1

The first argument passed to setInterval() needs to be a function. Do not call isBtnEnabled, just pass it as an argument:

this.nIntervId = setInterval(this.isBtnEnabled, 10000);
//                          NOTE: no () here ^^

As a side note, your enabledness check:

if (this.proceedToQaBtn.isEnabled()) {

has a problem: it will always evaluate to true regardless of the state of the element - isEnabled() returns a promise, which is truthy by definition. If you want to get the actual boolean value of whether the element is enabled or not, resolve the promise:

this.proceedToQaBtn.isEnabled().then(function (isEnabled) {
    if (isEnabled) {
        // ...
    }
});

Also, I don't think you should not be using setInterval() at all. Protractor has a built-in mechanism to explicitly wait for and periodically check a specific condition to be met on a page - check out browser.wait() and a set of built-in expected conditions. elementToBeClickable feels like it fits your use case:

var EC = protractor.ExpectedConditions;
browser.wait(EC.elementToBeClickable(this.proceedToQaBtn), 10000);
this.proceedToQaBtn.click();
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • thank you for your answer! and the note on "has a problem" I'm gonna try that right now – toxicBurn Apr 16 '18 at 16:35
  • comment to "Also, I don't think you should not be using setInterval()" : the unfortunate part, I have to refresh the page in order to see the button enabled. is it possible to insert a page refresh in EC ? – toxicBurn Apr 16 '18 at 16:55
  • @toxicBurn ah, gotcha. You can still do it through expected conditions, but you would need to define your own custom expected condition function. Thanks. – alecxe Apr 16 '18 at 16:56
  • Thank you for your comment, it solves the error I've reported. ...but for some reason, the code is not even going into isBtnEnabled() I've added console.log statements on every line to see where it goes. and it exits out right after it gets into waitForBtn() – toxicBurn Apr 16 '18 at 17:41
  • @toxicBurn could you please create a separate question with the problem you currently have? This way, we are gonna keep this topic clean and more people may potentially help with your new issue. Thanks! Throw me a link here and I'll take a look as well. – alecxe Apr 16 '18 at 17:43
  • Awesome, wanted to avoid the dup onece again, thank you! – toxicBurn Apr 16 '18 at 17:49