3

我的測試文件夾中有多個測試,其中所有測試的命名約定以spec.js結束。我使用*/spec.js選項運行配置文件中的所有測試。避免在Firefox中的多個測試文件中進行一次測試

我想跳過在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中跑步的測試仍在運行。

請指教。

回答

4

一個簡單的方法是使用exclude標籤更新您的Firefox multicapabilities以排除特定的測試規範。這可以防止使用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 
    }], 

希望它有幫助。

+0

非常感謝..不知道排除標籤 – user2744620

+0

您知道2.4.0支持排除標籤嗎?我升級了量角器版本,現在我的排除標籤根本不起作用。我得到警告您的規範模式不匹配任何文件消息 – user2744620

+0

@ user2744620它應該工作。看起來你的錯誤,似乎你提供了一個不正確的文件路徑或文件名。你可以驗證一次嗎?如果它仍然正確,那麼請在github頁面中使用量角器打開一個問題。謝謝。 –

1

只要browser.getCapabilities()是異步的並且基於Promises.then()中的代碼可能比其餘代碼更晚執行。我想你的if條件放在describe區塊內,它實際上在browser.browserName的值被設置之前運行,結果你得到的值爲undefined,條件失敗。爲了確保所有的準備工作完成後,你的測試運行,您應該從onPrepare返回一個承諾:

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

量角器會explicilty等待,直到它解決了,然後開始執行測試。

describe('Suite', function() { 

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

    it('spec', function() { 
     expect(true).toBe(true); 
    }); 
}); 
相關問題