无法通过量角器进行测试

时间:2015-12-10 08:11:00

标签: angularjs protractor

我做了简单的测试登录并尝试检查它是否成功:

describe('My Angular App', function () {

  describe('visiting the main homepage', function () {

    beforeEach(function () {
      browser.get('/');
      element(By.id("siteLogin")).click();
    });

    it('should login successfully', function() {

      element(By.name("login_username")).sendKeys("test@email.com");
      element(By.name("login_password")).sendKeys("testpass");

      element(By.id("formLoginButton")).click().then(function() {

        browser.getCurrentUrl().then(function(url){
          expect(url).toContain("profile");
        });

      });

    });

  });

});

直到我检查网址的最后一部分,并且在 Selenium Server 中我得到:

INFO - Executing: [execute async script: try { return (function (rootSelector, callback) {
  var el = document.querySelector(rootSelector);

  try {
    if (window.getAngularTestability) {
      window.getAngularTestability(el).whenStable(callback);
      return;
    }
    if (!window.angular) {
      throw new Error('angular could not be found on the window');
    }
    if (angular.getTestability) {
      angular.getTestability(el).whenStable(callback);
    } else {
      if (!angular.element(el).injector()) {
        throw new Error('root element (' + rootSelector + ') has no injector.' +
           ' this may mean it is not inside ng-app.');
      }
      angular.element(el).injector().get('$browser').
          notifyWhenNoOutstandingRequests(callback);
    }
  } catch (err) {
    callback(err.message);
  }
}).apply(this, arguments); }
catch(e) { throw (e instanceof Error) ? e : new Error(e); }, [body]])

我也得到了:

Failures:
1) My Angular App visiting the main homepage should login successfully
  Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

我的量角器-conf.js

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  baseUrl: 'http://localhost:8080/Mysite',

  capabilities: {
    'browserName': 'firefox' // muste use firefox because I can't get .click() to work in Chrome
  },

  specs: [
    'spec-e2e/*.js'
  ],

  framework: 'jasmine2',

  jasmineNodeOpts: {
    isVerbose: true,
    showColors: true,
    defaultTimeoutInterval: 30000
  }
};

我很感激这方面的任何帮助。 感谢

1 个答案:

答案 0 :(得分:1)

看起来点击后打开了非Angular页面。如果是这种情况,您需要将Protractor和Angular之间的同步关闭:

afterEach(function () {
  browser.ignoreSynchronization = false;
});

it('should login successfully', function() {
  element(By.name("login_username")).sendKeys("test@email.com");
  element(By.name("login_password")).sendKeys("testpass");

  browser.ignoreSynchronization = true;
  element(By.id("formLoginButton")).click().then(function() {
    expect(browser.getCurrentUrl()).toContain("profile");
  });
});

请注意,您无需显式解析getCurrentUrl返回的承诺,并且可以让expect()隐式为您执行此操作。

您可能还需要wait for the URL to change

var urlChanged = function(desiredUrl) {
  return browser.getCurrentUrl().then(function(url) {
    return url == desiredUrl;
  });
};

browser.wait(urlChanged("my desired url"), 5000);