在Protractor测试中获取当前浏览器名称

14

我正在进行一些测试并创建用户。由于它连接到后端并创建真实用户,因此我需要固定装置(fixtures)。我想使用浏览器名称来创建唯一的用户。然而,获取它已被证明相当困难...

有人能指导我正确的方向吗?

3个回答

40

另一个 橡皮鸭调试 的案例 :)

答案实际上非常简单。

在我的 onPrepare 函数中,我添加了以下函数,现在它可以完美地工作了。

browser.getCapabilities().then(function (cap) {
  browser.browserName = cap.caps_.browserName;
});

我可以使用browser.browserName来获取我的测试中的名称。

12
我作为一名程序员已经工作了27年,从来没有听说过“橡皮鸭调试”的术语。谢谢你告诉我这个。 - John Munsch

18

从Protractor 3.2版本(selenium webdriver 2.52)开始,这个已经发生了变化。

现在应该调用以下方法:

browser.driver.getCapabilities().then(function(caps){
    browser.browserName = caps.get('browserName');
}

0
如果你想避免使用浏览器,你可能想要这样做:
it('User should see a message that he has already been added to the campaing when entering the same email twice', function () {

    browser.getCapabilities().then(function (capabilities) {
        browserName = capabilities.caps_.browserName;
        platform = capabilities.caps_.platform;
    }).then(function () {
        console.log('Browser:', browserName, 'on platform', platform);
        if (browserName === 'internet explorer') {
            console.log('IE Was avoided for this test.');
        } else {
            basePage.email.sendKeys('bruno@test.com');
            console.log('Mande el mail');
            basePage.subscribe.click().then(function () {
                basePage.confirmMessage('Contact already added to target campaign');
            });
        }
    });

}); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接