7

测试代码:

描述('我的网站',函数(){

var init_url = 'http://localhost/mySite/#/home';
beforeEach(function(){
    // driver = new webdriver.Builder().
    // withCapabilities(webdriver.Capabilities.phantomjs()).build();
})


it('should click on toolbox and do stuff', function(){
    browser.get(init_url);
    browser.waitForAngular();
    browser.getCurrentUrl().then(function(url){
        console.log('current_url', url);
        expect(init_url).toEqual(init_url);
    })
    expect(true).toBe(true);
    browser.sleep(2000);
})

结果第一次运行,

Using the selenium server at http://localhost:9515
data Zoom Pad
class active

mysite
    should click on toolbox and do stuff

Finished in 3.94 seconds
1 test, 4 assertions, 0 failures

第二次运行,没有任何中断,只需向上箭头并输入:

   Stacktrace:
     Error: Error while running testForAngular: Error Message => 'Detected a pag
e unload event; asynchronous script execution does not work across page loads.'
 caused by Request => {"headers":{"Accept":"application/json; charset=utf-8","Co
nnection":"keep-alive","Content-Length":"689","Content-Type":"application/json;c
harset=UTF-8","Host":"localhost:9515"},"httpVersion":"1.1","method":"POST","post
":"{\"script\":\"return (function () {\\n  var attempts = arguments[0];\\n  var
callback = arguments[arguments.length - 1];\\n  var check = function(n) {\\n
try {\\n      if (window.angular && window.angular.resumeBootstrap) {\\n
callback([true, null]);\\n      } else if (n < 1) {\\n        if (window.angular
) {\\n          callback([false, 'angular never provided resumeBootstrap']);\\n
       } else {\\n          callback([false, 'retries looking for angular exceed

第三次

  1) mysite should click on toolbox and do stuff
   Message:
     Error: ECONNREFUSED connect ECONNREFUSED
   Stacktrace:
     Error: ECONNREFUSED connect ECONNREFUSED
    at ClientRequest.<anonymous> (K:\Users\Congwen\AppData\Roaming\npm\node_modu
les\protractor\node_modules\selenium-webdriver\http\index.js:127:16)
    at ClientRequest.EventEmitter.emit (events.js:95:17)
    at Socket.socketErrorListener (http.js:1547:9)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:441:14

第三次 phantomjs 网络服务器关闭,需要重新连接,然后返回结果 1:

任何线索?

使用的配置文件:

 exports.config = {
     seleniumAddress: 'http://localhost:9515',
     specs: [
         './ptor-tests/mysite-test.js'
     ],
     capabilities: {
         browserName: 'phantomjs',
         version: '',
         platform: 'ANY'
     },
     //baseUrl: 'http://testapp.example.com/index.html',
     rootElement: 'body',
     allScriptsTimeout: 11000,
     onPrepare: function () {},
     jasmineNodeOpts: {
         onComplete: function () {},
         isVerbose: true,
         showColors: true,
         includeStackTrace: true,
         defaultTimeoutInterval: 30000
     }
 };

我还注意到有时不需要第 2 步,它会直接进入 ECONNECT 错误,有时它会卡在第 2 步进行多次测试,最终会终止 phantomjs 服务器。

4

1 回答 1

8

这是 Protractor 的一个问题,已在 0.17 版中解决,并在 0.18 版中改进。

这是一个长尾的错误,但 TL;DR 是 Protractor 的 .get(url) 函数实际上使用客户端 JavaScript 来更改位置;这是为了确保它正确引导。该设计的一个不幸的副作用是,出于某种原因,PhantomJS 需要几秒钟才能正确导航。

该错误已通过向 .get 函数添加更长的超时时间得到解决。

Github 问题:https ://github.com/angular/protractor/issues/85

相关变更日志条目:

v0.18

(10aec0f) 修复(页面加载):增加等待超时

在 Sauce Labs 上测试 IE 时,300 毫秒的等待导致了问题。好像太短了 “browser.get()”总是超时。增加它解决了我们的问题。

v0.17

(a0bd84b) 修复(页面加载):在 protractor.get() 期间添加等待以解决卸载问题

一些系统在开始异步脚本执行之前不会等待浏览器卸载事件完成。

关闭 #406。关闭#85。

我已经在本地运行了您的测试(使用不同的页面,但其他代码相同):

测试

祝测试愉快!

于 2014-02-08T11:29:27.917 回答