如何在 Protractor 中分片測試檔案?
分片是一種將資料或任務分佈到多臺機器上的方法,以提高效能和可擴充套件性。在測試自動化中,分片是一種將測試用例分佈到測試框架的多個例項中的技術,以加快測試執行速度。Protractor 是一個測試框架,它使用 Jasmine 測試框架來促進測試用例的分片。
要在 Protractor 中分片測試檔案,必須編寫一個配置檔案,其中詳細說明測試檔案的路徑和要建立的例項數。為了縮短整體測試執行時間,Protractor 將測試用例分佈到多個例項中並併發執行它們。
必須仔細規劃和配置分片,以確保測試結果可靠一致。在實現分片時,必須考慮測試速度和準確性之間的權衡,並且必須密切關注測試結果,以發現任何潛在的問題。
使用 Protractor 分片的步驟
要安裝和設定 Protractor,您需要執行以下步驟:
從官方網站在您的系統中安裝 Node.js。
使用以下命令安裝 Protractor:
npm install -g protractor
使用以下命令檢查版本以確保安裝成功:
protractor --version
使用以下命令更新 webdriver-manager:
webdriver-manager update
更新 conf.js 檔案以描述 Protractor 流程
在 conf.js 檔案的 capabilities 塊中宣告兩個功能以啟用分片:
shardTestFiles - 啟用多個規範併發執行。如果為 true,則規範將按檔案分片(即,此功能集執行的所有檔案都將並行執行)。預設值為 false。
maxInstances - 為此功能集併發執行的瀏覽器例項數。只有在 shardTestFiles 設定為 true 時才需要此項。預設值為 1。
示例
在這個例子中,我們將看到如何在 Protractor 中測試分片檔案。我們需要使用 shardTestFiles 和 maxInstances 配置 conf.js 檔案。我們使用 test-file1.js 和 test-file2.js 作為我們的規範來測試 index.html 和 index2.html 檔案。所有這些檔案的程式碼如下所示:
conf.js - 這是我們的配置檔案
// An example configuration file.
exports.config = {
directConnect: true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome',
// Sharding
'shardTestFiles': true,
'maxInstances': 1,
},
// Framework to use. Jasmine is recommended.
framework: 'jasmine',
// Spec patterns are relative to the current working directory when
// protractor is called.
specs: ['test-file1.js' , 'test-file2.js'],
SELENIUM_PROMISE_MANAGER: false,
// Options to be passed to Jasmine.
jasmineNodeOpts: {
defaultTimeoutInterval: 30000
}
};
test-file1.js - 測試檔案 1
describe ('Protractor Test App' , function () {
it ('Test 1' , async function () {
// Disable Angular render update waiting
await browser.waitForAngularEnabled (false);
// Get the HTML file that must be tested
await browser.get ('http://127.0.0.1:5500/index.html');
// Test Element
let testElement = element (by.id ('test-element'));
// Allow the fade in process to finish
await browser.driver.wait ( async function () {
return await testElement.getCssValue ('opacity') === '1';
} , 30000, "It is taking more time than expected!");
});
});
test-file2.js - 測試檔案 2
describe ('Protractor Test App' , function () {
it ('Test 2', async function () {
// Disable Angular render update waiting
await browser.waitForAngularEnabled (false);
// Get the HTML file that must be tested
await browser.get ('http://127.0.0.1:5500/index2.html');
// Test Element
let testElement = element (by.id ('test-element'));
expect(testElement.isDisplayed()).toBe(false);
});
});
index.html - 要測試的檔案
<!DOCTYPE html>
<html>
<head>
<title>Using JavaScript to Implement Fade-In effect</title>
</head>
<body>
<!-- Test Element -->
<h4 id="test-element" style="opacity: 0">Some Text</h4>
<script type="text/javascript">
let elementOpacity = 0
let elementInterval = 0
window.onload = function () {
elementInterval = setInterval (() => {
let testElement = document.getElementById ('test-element')
elementOpacity = Number(
window.getComputedStyle(testElement).getPropertyValue('opacity')
)
if (elementOpacity < 1) {
elementOpacity = elementOpacity + 0.1
testElement.style.opacity = elementOpacity
} else {
clearInterval(elementInterval)
}
}, 200)
}
</script>
</body>
</html>
index2.html - 要測試的檔案
<!DOCTYPE html> <html> <body> <!-- Test Element --> <div id="test-element" style="display: none"> Some Text </div> </body> </html>
執行以下命令:
protractor conf.js
輸出

Protractor 是一個方便的自動化測試工具,但我們需要確保步驟正確且按順序執行。
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP