带有 phantomjs 的量角器永远挂起

Protractor with phantomjs hangs forever

这是我的配置文件:

exports.config = { 
   seleniumServerJar: './selenium-server-standalone-2.43.1.jar', 
  "capabilities": { 
    "browserName": "phantomjs"   
 }, 
 specs: [ 
        "test.js" 
    ], 
    jasmineNodeOpts: { 
        isVerbose: false, 
        showColors: true, 
        includeStackTrace: true 
    }, 
};

这是所谓的 test.js。它是针对 ng-europe 社区站点的,所以每个愿意的人都可以 运行 this.I 选择它来学习量角器写成 angular:

describe('test ng-europe', function() { 

        beforeEach(function() { 
                browser.get('http://ngeurope.org'); 
        }); 

         it('should have a title', function() { 
                expect(browser.getTitle()).toContain('ng-europe'); 
                element.all(by.repeater('avatar in talk.avatars')).count().then(function(count) { 
                        console.log(count); 
                }); 
        }); 

        it('search something', function() { 
                var search_=element(by.model('speakerq')); 
                var speakers_=element.all(by.repeater('speaker in config.speakers').column('name')); 
                var speakers = $$('div.speaker.col-xs-12.col-sm-4.text-center.ng-scope img.img-responsive.img-thumbnail.img-circle.avatar');
                search_.sendKeys('igor'); 
                expect(speakers.count()).toBe(1); 
                console.log("speakers count" + speakers.count()); 
        }); 

        it('search something else', function() { 
                var search_=element(by.model('talksq')); 
                var talkers = $$('ul.list-unstyled li.ng-scope.ng-binding span.speakers-names.ng-binding'); 
                search_.sendKeys('with'); 
                expect(talkers.count()).toBe(8); 
                console.log("talkers count" + talkers.count()); 
        }); 

        it('click on jobs', function() { 
                var jobs_button=element(by.linkText('Jobs')); 
                jobs_button.click(); 
                var sponsors=$$('div.col-md-4 img.sponsor-logo.gold-sponsor') 
                expect(sponsors.count()).toBe(4);        
        }); 
});

我是这样称呼它的:

>protractor conf.js

selenium驱动在同一目录节点,%appdata%\npm在路径,phantomjs已安装,updated.Everything 运行没问题如果我将驱动程序设置为 chromefirefox 但使用 phantomjs 脚本就会挂起永远不会显示 happens.No 错误,nothing.Just 这个:

Starting selenium standalone server... 
[launcher] Running 1 instances of WebDriver 
Selenium standalone server started at http://XX.XX.XXX.XX:60324/wd/hub

我错过了什么?

我至少成功地 运行 通过了测试:

  • phantomjs 1.9.7
  • protractor 2.0.0

但是,现在它在成功通过第一个 it() 块后立即抛出以下错误消息:

Error: Failed: Error communicating with the remote browser. It may have died.

这是我使用的配置(应用了几个 phantomjs 助手 "tricks"):

exports.config = {
    seleniumArgs: ['-browserTimeout=60'],

    "capabilities": {
        "browserName": "phantomjs",
        'phantomjs.cli.args': [
            '--ignore-ssl-errors=true',  
            '--web-security=false'
        ]
    },

    specs: [
        "test.js"
    ],

    framework: "jasmine2",

    allScriptsTimeout: 20000,

    onPrepare: function () {
        browser.driver.manage().window().maximize();
    },

    jasmineNodeOpts: {
        showColors: true,
        isVerbose: true,
        includeStackTrace: true,

        defaultTimeoutInterval: 25000
    }
};

有很多人问如何解决这样的错误:

有不同的解决方法建议,但 none 对我和其他人都有效。

这引出了答案的要点:为什么首先使用 phantomjs?

正如 protractor developers 所述:

We recommend against using PhantomJS for tests with Protractor. There are many reported issues with PhantomJS crashing and behaving differently from real browsers.

延长点。

如果使用 protractor 的主要目的是端到端测试,那么您绝对应该重新考虑您的浏览器选择。端到端测试应该模仿您的应用程序的真实用户,我敢肯定,他不会使用 phantomjs 来浏览您的站点。

如果选择phantomjs的原因是没有真实显示,可以借助xvfb使用虚拟显示(查看更多here) or run your tests on a remote selenium server, either your own, or provided by BrowserStack or Sauce Labs.