|
| 1 | +--- |
| 2 | +title: 'Quickstart: Generate rich reports for Playwright tests' |
| 3 | +description: 'This quickstart shows how to troubleshoot your test runs using Microsoft Playwright Testing Preview.' |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 09/23/2024 |
| 6 | +ms.custom: playwright-testing-preview |
| 7 | +--- |
| 8 | + |
| 9 | +# Quickstart: Troubleshoot tests with Microsoft Playwright Testing Preview |
| 10 | + |
| 11 | +In this quickstart, you learn how to troubleshoot your Playwright tests easily using reports and artifacts published on Microsoft Playwright Testing Preview. Additionally, this guide demonstrates how to utilize the reporting feature, regardless of whether you're running tests on the cloud-hosted browsers provided by the service. |
| 12 | + |
| 13 | +After you complete this quickstart, you'll have a Microsoft Playwright Testing workspace to view test results and artifacts in the service portal. |
| 14 | + |
| 15 | +> [!IMPORTANT] |
| 16 | +> Microsoft Playwright Testing is currently in preview. For legal terms that apply to Azure features that are in beta, in preview, or otherwise not yet released into general availability, see the [Supplemental Terms of Use for Microsoft Azure Previews](https://azure.microsoft.com/support/legal/preview-supplemental-terms/). |
| 17 | +
|
| 18 | +## Background |
| 19 | + |
| 20 | +Microsoft Playwright Testing service enables you to: |
| 21 | + |
| 22 | +- Accelerate build pipelines by running tests in parallel using cloud-hosted browsers. |
| 23 | +- Simplify troubleshooting by publishing test results and artifacts to the service, making them accessible through the service portal. |
| 24 | + |
| 25 | +These two features of the service can be used independently of each other and each has its own [pricing plan](https://aka.ms/mpt/pricing). This means you can: |
| 26 | + |
| 27 | +- Expedite test runs and streamline troubleshooting by running tests in cloud-hosted browsers and publishing results to the service. |
| 28 | +- Run tests only in cloud-hosted browsers to finish test runs faster. |
| 29 | +- Publish test results to the service while continuing to run tests locally for efficient troubleshooting. |
| 30 | + |
| 31 | +## Prerequisites |
| 32 | + |
| 33 | +* An Azure account with an active subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F) before you begin. |
| 34 | +* Your Azure account needs the [Owner](/azure/role-based-access-control/built-in-roles#owner), [Contributor](/azure/role-based-access-control/built-in-roles#contributor), or one of the [classic administrator roles](/azure/role-based-access-control/rbac-and-directory-admin-roles#classic-subscription-administrator-roles). |
| 35 | +* A Playwright project. If you don't have project, create one by using the [Playwright getting started documentation](https://playwright.dev/docs/intro) or use our [Microsoft Playwright Testing sample project](https://github.com/microsoft/playwright-testing-service/tree/main/samples/get-started). |
| 36 | +* Azure CLI. If you don't have Azure CLI, see [Install Azure CLI](/cli/azure/install-azure-cli). |
| 37 | + |
| 38 | +## Create a workspace |
| 39 | + |
| 40 | +To get started with publishing test results on Playwright Testing service, first create a Microsoft Playwright Testing workspace in the Playwright portal. |
| 41 | + |
| 42 | +[!INCLUDE [Create workspace in Playwright portal](./includes/include-playwright-portal-create-workspace.md)] |
| 43 | + |
| 44 | +When the workspace creation finishes, you're redirected to the setup guide. |
| 45 | + |
| 46 | +## Install Microsoft Playwright Testing package |
| 47 | + |
| 48 | +To use the service, install the Microsoft Playwright Testing package. |
| 49 | + |
| 50 | +```npm |
| 51 | +npm init @azure/microsoft-playwright-testing |
| 52 | +``` |
| 53 | + |
| 54 | +This command generates `playwright.service.config.ts` file which serves to: |
| 55 | + |
| 56 | +- Direct and authenticate your Playwright client to the Microsoft Playwright Testing service. |
| 57 | +- Adds a reporter to publish test results and artifacts. |
| 58 | + |
| 59 | +If you already have this file, the prompt asks you to override it. |
| 60 | + |
| 61 | +To use only reporting feature for the test run, disable cloud-hosted browsers by setting `useCloudHostedBrowsers` as false. |
| 62 | + |
| 63 | +```typescript |
| 64 | +export default defineConfig( |
| 65 | + config, |
| 66 | + getServiceConfig(config, { |
| 67 | + timeout: 30000, |
| 68 | + os: ServiceOS.LINUX, |
| 69 | + useCloudHostedBrowsers: false // Do not use cloud hosted browsers |
| 70 | + }), |
| 71 | + { |
| 72 | + reporter: [['list'], ['@azure/microsoft-playwright-testing/reporter']], // Reporter for Microsoft Playwright Testing service |
| 73 | + } |
| 74 | +); |
| 75 | +``` |
| 76 | +Setting the value as `false` ensures that cloud-hosted browsers aren't used to run the tests. The tests run on your local machine but the results and artifacts are published on the service. |
| 77 | + |
| 78 | +> [!TIP] |
| 79 | +> If you wish to accelerate your test run using cloud-hosted browser, you can set `useCloudHostedBrowsers` as true. This will run your tests on the service managed browsers. |
| 80 | +
|
| 81 | +## Configure the service region endpoint |
| 82 | + |
| 83 | +In your setup, you have to provide the region-specific service endpoint. The endpoint depends on the Azure region you selected when creating the workspace. |
| 84 | + |
| 85 | +To get the service endpoint URL: |
| 86 | + |
| 87 | +1. In **Add region endpoint in your setup**, copy the region endpoint for your workspace. |
| 88 | + |
| 89 | + The endpoint URL matches the Azure region that you selected when creating the workspace. |
| 90 | + |
| 91 | + :::image type="content" source="./media/quickstart-run-end-to-end-tests/playwright-testing-region-endpoint.png" alt-text="Screenshot that shows how to copy the workspace region endpoint in the Playwright Testing portal." lightbox="./media/quickstart-run-end-to-end-tests/playwright-testing-region-endpoint.png"::: |
| 92 | + |
| 93 | +## Set up your environment |
| 94 | + |
| 95 | +To set up your environment, you have to configure the `PLAYWRIGHT_SERVICE_URL` environment variable with the value you obtained in the previous steps. |
| 96 | + |
| 97 | +We recommend that you use the `dotenv` module to manage your environment. With `dotenv`, you define your environment variables in the `.env` file. |
| 98 | + |
| 99 | +1. Add the `dotenv` module to your project: |
| 100 | + |
| 101 | + ```shell |
| 102 | + npm i --save-dev dotenv |
| 103 | + ``` |
| 104 | + |
| 105 | +1. Create a `.env` file alongside the `playwright.config.ts` file in your Playwright project: |
| 106 | + |
| 107 | + ``` |
| 108 | + PLAYWRIGHT_SERVICE_URL={MY-REGION-ENDPOINT} |
| 109 | + ``` |
| 110 | + |
| 111 | + Make sure to replace the `{MY-REGION-ENDPOINT}` text placeholder with the value you copied earlier. |
| 112 | + |
| 113 | + |
| 114 | +## Set up authentication |
| 115 | + |
| 116 | +To publish test results and artifacts to your Microsoft Playwright Testing workspace, you need to authenticate the Playwright client where you're running the tests with the service. The client could be your local dev machine or CI machine. |
| 117 | +
|
| 118 | +The service offers two authentication methods: Microsoft Entra ID and Access Tokens. |
| 119 | +
|
| 120 | +Microsoft Entra ID uses your Azure credentials, requiring a sign-in to your Azure account for secure access. Alternatively, you can generate an access token from your Playwright workspace and use it in your setup. |
| 121 | +
|
| 122 | +##### Set up authentication using Microsoft Entra ID |
| 123 | +
|
| 124 | +Microsoft Entra ID is the default and recommended authentication for the service. From your local dev machine, you can use [Azure CLI](/cli/azure/install-azure-cli) to sign-in |
| 125 | +
|
| 126 | +```CLI |
| 127 | +az login |
| 128 | +``` |
| 129 | +> [!NOTE] |
| 130 | +> If you're a part of multiple Microsoft Entra tenants, make sure you sign in to the tenant where your workspace belongs. You can get the tenant ID from Azure portal. See [Find your Microsoft Entra Tenant](/azure/azure-portal/get-subscription-tenant-id#find-your-microsoft-entra-tenant). Once you get the ID, sign-in using the command `az login --tenant <TenantID>` |
| 131 | + |
| 132 | +##### Set up authentication using access tokens |
| 133 | + |
| 134 | +You can generate an access token from your Playwright Testing workspace and use it in your setup. However, we strongly recommend Microsoft Entra ID for authentication due to its enhanced security. Access tokens, while convenient, function like long-lived passwords and are more susceptible to being compromised. |
| 135 | + |
| 136 | +1. Authentication using access tokens is disabled by default. To use, [Enable access-token based authentication](./how-to-manage-authentication.md#enable-authentication-using-access-tokens) |
| 137 | + |
| 138 | +2. [Set up authentication using access tokens](./how-to-manage-authentication.md#set-up-authentication-using-access-tokens) |
| 139 | + |
| 140 | +> [!CAUTION] |
| 141 | +> We strongly recommend using Microsoft Entra ID for authentication to the service. If you are using access tokens, see [How to Manage Access Tokens](./how-to-manage-access-tokens.md) |
| 142 | + |
| 143 | +## Enable artifacts in Playwright configuration |
| 144 | +In the `playwright.config.ts` file of your project, make sure you're collecting all the required artifacts. |
| 145 | +```typescript |
| 146 | + use: { |
| 147 | + trace: 'on-first-retry', |
| 148 | + video:'retain-on-failure', |
| 149 | + screenshot:'on' |
| 150 | + }, |
| 151 | +``` |
| 152 | +
|
| 153 | +## Run your tests and publish results on Microsoft Playwright Testing |
| 154 | +
|
| 155 | +You've now prepared the configuration for publishing test results and artifacts with Microsoft Playwright Testing. Run tests using the newly created `playwright.service.config.ts` file and publish test results and artifacts to the service. |
| 156 | + |
| 157 | + ```bash |
| 158 | + npx playwright test --config=playwright.service.config.ts |
| 159 | +``` |
| 160 | +> [!NOTE] |
| 161 | +> For the Reporting feature of Microsoft Playwright Testing, you get charged based on the number test results published. If you're a first-time user or [getting started with a free trial](./how-to-try-playwright-testing-free.md), you might start with publishing single test result instead of your full test suite to avoid exhausting your free trial limits. |
| 162 | +
|
| 163 | +After the test completes, you can view the test status in the terminal. |
| 164 | +
|
| 165 | +```output |
| 166 | +Running 6 test using 2 worker |
| 167 | + 5 passed, 1 failed (20.2s) |
| 168 | + |
| 169 | +Test report: https://playwright.microsoft.com/workspaces/<workspace-id>/runs/<run-id> |
| 170 | +``` |
| 171 | +
|
| 172 | +> [!CAUTION] |
| 173 | +> Depending on the size of your test suite, you might incur additional charges for the test results beyond your allotted free test results. |
| 174 | +
|
| 175 | +## View test runs and results in the Playwright portal |
| 176 | +
|
| 177 | +You can now troubleshoot the failed test cases in the Playwright portal. |
| 178 | +
|
| 179 | +[!INCLUDE [View test runs and results in the Playwright portal](./includes/include-playwright-portal-view-test-results.md)] |
| 180 | +
|
| 181 | +
|
| 182 | +> [!TIP] |
| 183 | +> You can also use Microsoft Playwright Testing service to run tests in parallel using cloud-hosted browsers. Both Reporting and cloud-hosted browsers are independent features and are billed separately. You can use either of these or both. For details, see [How to use service features](./how-to-use-service-features.md) |
| 184 | +
|
| 185 | +> [!NOTE] |
| 186 | +> The test results and artifacts that you publish are retained on the service for 90 days. After that, they are automatically deleted. |
| 187 | +
|
| 188 | +
|
| 189 | +## Next step |
| 190 | +
|
| 191 | +You've successfully created a Microsoft Playwright Testing workspace in the Playwright portal and run your Playwright tests on cloud browsers. |
| 192 | + |
| 193 | +Advance to the next quickstart to set up continuous end-to-end testing by running your Playwright tests in your CI/CD workflow. |
| 194 | + |
| 195 | +> [!div class="nextstepaction"] |
| 196 | +> [Set up continuous end-to-end testing in CI/CD](./quickstart-automate-end-to-end-testing.md) |
0 commit comments