使用phantomjs浏览器运行量角器时,只能运行一次测试

时间:2014-01-27 14:07:55

标签: angularjs phantomjs protractor angularjs-e2e

测试代码:

describe('mysite',function(){

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服务器。

1 个答案:

答案 0 :(得分:7)

这是Protractor的一个问题,在版本0.17中得到了解决,并在0.18中更好。

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

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

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

相关的更改日志条目:

v0.18

  

(10aec0f)fix(pageload):增加等待超时

     

在Sauce Labs上测试IE时,300毫秒等待会导致问题。这似乎太短了。 &#34; browser.get()&#34;总是超时。增加它解决了我们的问题。

v0.17

  

(a0bd84b)fix(pageload):在protractor.get()期间添加等待以解决卸载问题

     

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

     

关闭#406。关闭#85。

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

Test

快乐测试!

相关问题