使用雲端裝載瀏覽器,在本機部署或私人裝載的應用程式搭配 Microsoft Playwright Testing Preview
瞭解如何使用 Microsoft Playwright Testing Preview 來執行本機部署應用程式的端對端測試。 Microsoft Playwright 測試會使用雲端裝載的遠端瀏覽器大規模執行 Playwright 測試。 您可以使用服務在localhost上執行應用程式的測試,或裝載於基礎結構上的應用程式。
Playwright 可讓您將用戶端電腦上可用的網路公開給遠端瀏覽器。 當您公開網路時,您可以從 Playwright 測試程式代碼連線到本機資源,而不需要設定其他防火牆設定。
重要
Microsoft Playwright Testing 目前為預覽版。 如需適合測試版、處於預覽狀態或其他尚未發行正式推出之 Azure 功能的法律條款,請參閱 Microsoft Azure 預覽版補充使用條款。
設定 Playwright 以公開局域網路
若要將局域網路和資源公開給遠端瀏覽器,您可以使用 exposeNetwork
Playwright 中的 選項。 深入瞭解 exposeNetwork
Playwright 檔中的選項 。
您可以使用規則清單來指定一或多個網路。 例如,若要公開測試/預備部署和 localhost: *.test.internal-domain,*.staging.internal-domain,<loopback>
。
您可以在 中playwright.service.config.ts
設定 exposeNetwork
選項。 下列範例示範如何使用 規則來公開 localhost
網路 <loopback>
。 您也可以將 取代 localhost
為您想要為服務啟用的網域。
import { getServiceConfig, ServiceOS } from "@azure/microsoft-playwright-testing";
import { defineConfig } from "@playwright/test";
import { AzureCliCredential } from "@azure/identity";
import config from "./playwright.config";
export default defineConfig(
config,
getServiceConfig(config, {
exposeNetwork: '<loopback>', // Allow service to access the localhost.
}),
);
您現在可以在 Playwright 測試程式代碼中參考 localhost
,並使用 Microsoft Playwright Testing 在雲端裝載的瀏覽器上執行測試:
npx playwright test --config=playwright.service.config.ts --workers=20
您可以在 中.runsettings
設定 ExposeNetwork
選項。 下列範例示範如何使用 規則來公開 localhost
網路 <loopback>
。 您也可以將 取代 localhost
為您想要為服務啟用的網域。
<TestRunParameters>
<!--Use this option to connect to local resources from your Playwright test code without having to configure additional firewall-->
<Parameter name="ExposeNetwork" value="loopback" />
</TestRunParameters>
您現在可以在 Playwright 測試程式代碼中參考 localhost
,並使用 Microsoft Playwright Testing 在雲端裝載的瀏覽器上執行測試:
dotnet test --settings:.runsettings --logger "microsoft-playwright-testing" -- NUnit.NumberOfTestWorkers=20