避免在firefox量角器中使用多个测试文件进行一次测试

时间:2015-09-28 05:52:06

标签: javascript angularjs selenium selenium-webdriver protractor

我在我的tests文件夹中有多个测试,其中所有测试的命名约定以spec.js结尾。我正在使用* / spec.js选项运行Config文件中的所有测试。

我想跳过在FF中运行一个测试,因为该浏览器不支持。这是我试图做的,但它并没有跳过那些测试。请指教。

multiCapabilities: [{
  'browserName': 'chrome',
  'chromeOptions' : {
    args: ['--window-size=900,900']
    // }
  },
},

{
  'browserName': 'firefox',
  'chromeOptions' : {
    args: ['--window-size=900,900']
    // }
  },
}],

specs: [
  '../tests/*.spec.js'
],

我的onPrepare功能中有以下内容:

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

在我希望跳过在FF中运行此测试的测试文件之一中,我正在执行此操作

if(browser.browserName=='firefox') { 
console.log("firefox cannot run *** tests")

} else { 

blah... rest of the tests which I want to execute for Chrome and IE I have put it in this block}

但是我想跳过在FF中运行的测试仍在运行。

请告知。

2 个答案:

答案 0 :(得分:4)

一种简单的方法是使用multicapabilities标记更新您的firefox exclude以排除特定的测试规范。这可以防止使用if条件和其他代码行。 More details are here。这是怎样的 -

multiCapabilities: [{
    browserName: 'chrome',
    chromeOptions : {
              args: ['--window-size=900,900']
                    }, 
    },
    {
    browserName: 'firefox',
    // Spec files to be excluded on this capability only.
    exclude: ['spec/doNotRunInChromeSpec.js'], //YOUR SPEC NAME THAT YOU WANT TO EXCLUDE/SKIP
    }],

希望它有所帮助。

答案 1 :(得分:1)

只要browser.getCapabilities()是异步的并且基于Promises.then()内的代码可能会比其余代码执行得晚。我猜您的if条件放在describe块内,实际上在browser.browserName的值设置之前运行,因此您获得了undefined的值条件失败。为确保在完成所有准备工作后运行测试,您应该从onPrepare返回承诺:

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

量角器将会一直等到它结算然后开始执行测试。

describe('Suite', function () {

    console.log(browser.browserName);  // 'firefox'

    it('spec', function () {
        expect(true).toBe(true);
    });
});