Get the current browser name in Protractor test

2019-01-12 03:58发布

问题:

I'm creating users in some test. Since it is connected to the backend and create real users I need fixtures. I was thinking of using the browser name to create unique user. However, It has proven to be quite difficult to get to it...

Anyone can point me in the right direction?

回答1:

Another case of rubber ducking :)

The answer was actually quite simple.

in my onPrepare function I added the following function and it works flawlessly.

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

I can get access the name in my test using browser.browserName.



回答2:

This has changed in version of protractor starting from 3.2 (selenium webdriver 2.52)

Now one should call:

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


回答3:

If you want to avoid the a browser, you may want to do this:

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');
            });
        }
    });

});