简体   繁体   中英

Protractor no waiting for web page to change

Using Angular 5 with Protractor.

Having trouble waiting for the browser url to change to see if the user logs in and then lands on the internal dashboard page.

Spec:

fdescribe('Suite 1', function() {
  it('should login and land on the dashboard page', function() {
    // Arrange
    browser.ignoreSynchronization = true;

    const baseUrl = 'confidentialUrl';
    const email = 'email';
    const password = 'password';

    // Act
    browser.get(baseUrl);
    element(by.id('username')).sendKeys(email);
    element(by.id('password')).sendKeys(password);
    element(by.css('.submit-btn')).click();

    // Assert
    browser.wait(5000);
    const currentUrl = browser.getCurrentUrl();
    expect(currentUrl).toEqual('confidentialUrl/dashboard');
  });

Error I get when I do a "protractor conf.js":

Failed: Wait condition must be a promise-like object, function, or a Condition object

browser.wait requires an object as a first parameter which is condition object or promise-like object. For example you can create an expected condition from protractor

// Expected condition object
const EC = protractor.ExpectedConditions;
// wait 5 seconds for expected condition
// in this case it will wait for url contains a given value
browser.wait(EC.urlContains('foo'), 5000).then(() => {
   // then you can assert here
   const currentUrl = browser.getCurrentUrl();
   return expect(currentUrl).toEqual('confidentialUrl/dashboard');
});

We resolved this kind of problems with using ExpectedConditions .

I edited your code, so in your case:

fdescribe('Suite 1', function() {
  it('should login and land on the dashboard page', function() {
    // Arrange
    browser.ignoreSynchronization = true;

    const baseUrl = 'confidentialUrl';
    const email = 'email';
    const password = 'password';

    // Act
    browser.get(baseUrl);
    element(by.id('username')).sendKeys(email);
    element(by.id('password')).sendKeys(password);
    element(by.css('.submit-btn')).click();

    // Assert
    const EC = protractor.ExpectedConditions;
    browser.wait(EC.urlContains('confidentialUrl/dashboard'), 5000).then(() => {
      const currentUrl = browser.getCurrentUrl();
      return expect(currentUrl).toEqual('confidentialUrl/dashboard');
    })

  }); 

or you can:

fdescribe('Suite 1', function() {
      it('should login and land on the dashboard page', function() {
        // Arrange
        browser.ignoreSynchronization = true;

        const baseUrl = 'confidentialUrl';
        const email = 'email';
        const password = 'password';

        // Act
        browser.get(baseUrl);
        element(by.id('username')).sendKeys(email);
        element(by.id('password')).sendKeys(password);
        element(by.css('.submit-btn')).click();

        // Assert
        return browser.driver.wait(function() {
          return browser.driver.getCurrentUrl().then(function(url) {
            return expect(url).toEqual('confidentialUrl/dashboard');
          });
        }, 10000);

      }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM