使用Protractor和Angular Non-Angular网站进行多次超时

时间:2015-08-28 10:53:25

标签: javascript angularjs jasmine protractor

您好我正在开发一个具有非角度界面的应用程序和带有角度的新模块。 它正处于过渡阶段。我试图使用量角器进行测试,但有些需要记录,登录页面是PHP。 我已经知道要做一些事情来测试我在conf中做过的非角度页面:

  exports.config =  {
    specs: ['app/**/*.e2e.js'],
    baseUrl: 'http://localhost:8099/ma#',
    maxSessions: 1,
    framework: 'jasmine2',
    // rootElement: 'myApp',
    multiCapabilities: [
      {
        browserName: 'chrome',
        chromeOptions: {
            args: [
                '--disable-cache',
                '--disable-application-cache',
                '--disable-offline-load-stale-cache',
                '--disk-cache-size=0',
                '--v8-cache-options=off'
            ]
        }
      }
    ],
    jasmineNodeOpts: {
      isVerbose: false,
      showColors: true,
      includeStackTrace: true,
      defaultTimeoutInterval: 50000
    },
    // getPageTimeout: 500000,
    onPrepare: function () {
      browser.driver.ignoreSynchronization = true;

      browser.driver.wait(browser.driver.get('http://localhost:8099/Authentication.php'));
      browser.driver.sleep(1000);
      browser.driver.findElement(by.id('input_login')).sendKeys('login');
      browser.driver.findElement(by.id('input_password')).sendKeys('pass');
      browser.driver.findElement(by.id('submit_button')).click();
      return browser.wait(function(){
        browser.get('/ma#/99/page/');
        browser.waitForAngular();
      }, 1000);
    }
  };

然后在我的测试中,我会做以下事情:

describe('Test Spec', function () {
  it('should get the correct title', function () {
expect(browser.getCurrentUrl()).toBe('http://localhost:8099/ma#/99/page/');
  });

});

但我得到的是与超时相关的各种错误,例如:

Error: Error: Wait timed out after 14074ms

 Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

或臭名昭着的

Uncaught exception: Error while waiting for Protractor to sync with the page: "angular could not be found on the window"

我迷失在这里,有人可以启发我吗?

2 个答案:

答案 0 :(得分:1)

由于涉及一些异步执行,您需要使用jasmine的异步回调,如下所示:

describe('Test Spec', function () {
  it('should get the correct title', function (done) {
    expect(browser.getCurrentUrl()).toBe('http://localhost:8099/ma#/99/page/');
    // this is the callback, use it in a proper place when all validations are done/complete
    // usually you call it inside another callback from other async execution i.e. ajax request
    done(); 
  });
});

答案 1 :(得分:0)

如果您正在测试非角度页面,那么您应该从conf文件中删除waitForAngular()函数。如果您正在测试混合角度+非角度页面,请使用下面的行以避免仅在非角度测试中等待角度并避免全局声明 -

browser.ignoreSynchronization = true;

要解决异步回调问题increase your wait time,以便量角器等待其请求以这样的承诺形式返回

return browser.wait(function(){
    browser.get('/ma#/99/page/');
}, 20000); //increase time limit here to increase wait time

希望这有帮助。