0

Here is the code I had used for clicking the button. In the first page I have an button, by clicking that it will redirect to next page. The second page is not loading for a long and the test is getting failed

describe('Login', function() {


it('should display Login home', function() {
browser.get('https://xxxxx.org/yyyy/');
browser.driver.manage().window().maximize();
var acrIdBtn  = browser.driver.findElement(by.css('a.btn.btn-lg.btn-success'));

acrIdBtn.click().then(function() {
    browser.driver.findElement(by.id('ContentPlaceHolder1_MFALoginControl1_UserIDView_txtUserid_UiInput')).click();

  });
});
});

HTML code:

<div class="col-sm-12">
<!-- ngIf: method.label.text !== ' *' && method.input.id.length > 0 --><input tabindex="0" class="form-control ng-pristine ng-scope ng-empty ng-invalid ng-invalid-required ng-touched" id="ContentPlaceHolder1_MFALoginControl1_UserIDView_txtUserid_UiInput" aria-invalid="true" required="" type="text" placeholder="Username" autocomplete="off" ng-if="method.label.text !== ' *' &amp;&amp; method.input.id.length > 0" ng-init="AuthMethod.getUser()" focus-if="" ng-disabled="false" ng-change="AuthMethod.getMethodOnChange(method.input.id)" ng-model="AuthMethod.user"><!-- end ngIf: method.label.text !== ' *' && method.input.id.length > 0 -->
<!-- ngIf: AuthMethod.forgotUser.link --><a class="help-block link--color ng-binding ng-scope" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$MFALoginControl1$UserIDView$ctl00$ContentPlaceHolder1_MFALoginControl1_UserIDView_hlinkUsernameLink','')" ng-if="AuthMethod.forgotUser.link">Forgot User ID</a><!-- end ngIf: AuthMethod.forgotUser.link -->

Deepak Sabastein
  • 197
  • 3
  • 16

2 Answers2

1

So first let's go through the code and understand why this is happening.

describe('Login', function() {
  it('should display Login home', function() {
    browser.get('https://xxxxx.org/yyyy/');
    browser.driver.manage().window().maximize();
    var acrIdBtn  = browser.driver.findElement(by.css('a.btn.btn-lg.btn-success'));

    // click is thenable, this is okay
    acrIdBtn.click().then(function() {
       // This promise gets queued but this is a void function meaning. So in jasminwd, the
       // function to find the element and click is a void function and the promise
       // would not be awaited.         
       browser.driver.findElement(by.id('ContentPlaceHolder1_MFALoginControl1_UserIDView_txtUserid_UiInput')).click();
    });
  });
});

The quick fix:

So the quick fix, make sure the callback returns the promise:

    acrIdBtn.click().then(function() {
       // If we return the click, jasminewd should await this callback.
       return browser.driver.findElement(by.id('ContentPlaceHolder1_MFALoginControl1_UserIDView_txtUserid_UiInput')).click();

Getting off of the control flow

This appears to still rely on the control flow. As a side note, I would suggest moving off of the control flow with SELENIUM_PROMISE_MANAGER: false, in your configuration file and using async await. See Protractor in STS IDE -> Could not find update-config.json for a pretty good example of the configuration file and async / await.

describe('Login', () => {
  it('should display Login home', async () => {
    await browser.get('https://xxxxx.org/yyyy/');
    await browser.driver.manage().window().maximize();
    // If you are using "element" but do not want to use wait for angular
    // think about using "await waitForAngularEnabled(false);"
    // If you prefer to use the WebDriver findElement version, then you could just keep it the same.
    // const acrIdBtn = browser.driver.findElement(by.css('a.btn.btn-lg.btn-success'));
    const acrIdBtn = element(by.css('a.btn.btn-lg.btn-success'));
    await acrIdBtn.click();
    const contentBtn = element(by.id('ContentPlaceHolder1_MFALoginControl1_UserIDView_txtUserid_UiInput'));
    await contentBtn .click();
  });
});
cnishina
  • 5,016
  • 1
  • 23
  • 40
0

It is always good, to first check, the button to be clickable:

const button = element(by.id('ContentPlaceHolder1_MFALoginControl1_UserIDView_txtUserid_UiInput'));
await browser.wait(ExpectedConditions.elementToBeClickable(button));
await button.click();
Michael R.
  • 388
  • 3
  • 9