0

I am writing a test for login into non-angular application.

describe('login Test for App', function () {

    beforeEach(function () {
        browser.ignoreSynchronization=true;
        browser.get(loginPageURL,2000);
    });


    it('It should Login a User', function () {
        element(by.id('username')).sendKeys(constants.userName);
        element(by.id('password')).sendKeys(constants.password);
        element(by.id('Login')).click().then(function () {
            // Waiting to open modal
            browser.wait(function () {
                return browser.getCurrentUrl().then(function (url) {
                    return url==dashboardUrl;
                });
            });
        });
    });
});

After login, I want to check currentUrl. But After login button click, It waits till dashboard url appeared. But when after loginbutton click, It go to diffrent url, Then it also wait for dashboard url infinite. I want to check current url after login event, if it is not dashboard url, then it should fail, and do not run next test suite cases, because login Test is failed.

Like-

When clickOn login button.

  1. Wait for complete page load.

2.Then check current url, If it is not dashboard url, then test should failed, and not proceed for any test cases.

Vikas Gupta
  • 1,183
  • 1
  • 10
  • 25

1 Answers1

2

It is not best practice to wait for the pages url to load in order to know if the page is loaded, because there could be redirects or other things.

It is best practice to wait for a specific element on the next page(after log-in). Here is your code refactored to use a custom wait() function that will wait for an element to appear before continuing to retrieve the current url:

    describe('login Test for App', function () {
    browser.ignoreSynchronization = true;

    it('should load the log-in page', function(done) {
        browser.driver.get(loginPageURL).then(function() {
            browser.driver.sleep(2000);
            wait('#username', 10000);
            done();
        });
    });

    it('It should Login a User', function (done) {
        element(by.id('username')).sendKeys(constants.userName);
        element(by.id('password')).sendKeys(constants.password);
        element(by.id('Login')).click().then(function () {
            // Waiting to open modal
            wait('#element_on_the_next_page', 10000);

            browser.getCurrentUrl().then(function (url) {
              // do anything with the url
              done();
            });
        });
    });

    function wait(selector, timeout) {
      browser.driver.wait(function() {
        return browser.driver.isElementPresent(by.css(selector)).then(function(present) {
          return present;
        });
      }, timeout);
      browser.driver.wait(function() {
        return browser.driver.findElement(by.css(selector)).isDisplayed().then(function(displayed) {
          return displayed;
        });
      }, timeout).then(function() {
        return;
      });
    }
});

Hope this helps!

GoLGo13
  • 81
  • 5
  • Close enough, but After timeout, If that element is not displayed or present, I dont want to move on further test-suites. because my login Test is failed. How to do that? Right Now, When my login Test fails, then it also move on further test-suites, that I have defined in spec property of conf.js – Vikas Gupta Oct 05 '17 at 06:22
  • Check out this answer here to fail the suite after initial failure if that is what you desire: https://stackoverflow.com/a/34462368/5623572 – GoLGo13 Oct 05 '17 at 18:41