|
| 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. |
| 12 | +
|
| 13 | +After you complete this quickstart, you 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 | +## Prerequisites |
| 19 | +
|
| 20 | +* 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. |
| 21 | +* 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). |
| 22 | +* 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). |
| 23 | +* Azure CLI. If you don't have Azure CLI, see [Install Azure CLI](/cli/azure/install-azure-cli). |
| 24 | + |
| 25 | +## Create a workspace |
| 26 | + |
| 27 | +To get started with running your Playwright tests at scale on cloud browsers, you first create a Microsoft Playwright Testing workspace in the Playwright portal. |
| 28 | + |
| 29 | +[!INCLUDE [Create workspace in Playwright portal](./includes/include-playwright-portal-create-workspace.md)] |
| 30 | + |
| 31 | +When the workspace creation finishes, you're redirected to the setup guide. |
| 32 | + |
| 33 | +## Install Microsoft Playwright Testing package |
| 34 | + |
| 35 | +To use the service, install Microsoft Playwright Testing package. |
| 36 | + |
| 37 | +```npm |
| 38 | +npm init @azure/microsoft-playwright-testing |
| 39 | +``` |
| 40 | + |
| 41 | +This generates `playwright.service.config.ts` file which serves to: |
| 42 | + |
| 43 | +- Direct and authenticate Playwright to the Microsoft Playwright Testing service. |
| 44 | +- Adds a reporter to publish test results and artifacts. |
| 45 | + |
| 46 | +If you already have this file, the package asks you to override it. |
| 47 | + |
| 48 | +Once you have the `playwright.service.config.ts` file, disable cloud-hosted browsers. |
| 49 | + |
| 50 | +```typescript |
| 51 | +useCloudHostedBrowsers: false |
| 52 | +``` |
| 53 | +Setting this as `false` ensures that cloud-hosted browsers are not used to run the tests. The tests run on your local machine but the results and artifacts are published on the service. |
| 54 | + |
| 55 | +## Configure the service region endpoint |
| 56 | + |
| 57 | +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. |
| 58 | + |
| 59 | +To get the service endpoint URL, perform the following steps: |
| 60 | + |
| 61 | +1. In **Add region endpoint in your setup**, copy the region endpoint for your workspace. |
| 62 | + |
| 63 | + The endpoint URL matches the Azure region that you selected when creating the workspace. |
| 64 | + |
| 65 | + :::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"::: |
| 66 | + |
| 67 | +## Set up your environment |
| 68 | + |
| 69 | +To set up your environment, you have to configure the `PLAYWRIGHT_SERVICE_URL` environment variable with the value you obtained in the previous steps. |
| 70 | + |
| 71 | +We recommend that you use the `dotenv` module to manage your environment. With `dotenv`, you define your environment variables in the `.env` file. |
| 72 | + |
| 73 | +1. Add the `dotenv` module to your project: |
| 74 | + |
| 75 | + ```shell |
| 76 | + npm i --save-dev dotenv |
| 77 | + ``` |
| 78 | + |
| 79 | +1. Create a `.env` file alongside the `playwright.config.ts` file in your Playwright project: |
| 80 | + |
| 81 | + ``` |
| 82 | + PLAYWRIGHT_SERVICE_URL={MY-REGION-ENDPOINT} |
| 83 | + ``` |
| 84 | + |
| 85 | + Make sure to replace the `{MY-REGION-ENDPOINT}` text placeholder with the value you copied earlier. |
| 86 | + |
| 87 | + |
| 88 | +## Set up Authentication |
| 89 | + |
| 90 | +To run your Playwright tests in your Microsoft Playwright Testing workspace, you need to authenticate the Playwright client where you're running the tests with the service. This could be your local dev machine or CI machine. |
| 91 | + |
| 92 | +The service offers two authentication methods: Microsoft Entra ID and Access Tokens. |
| 93 | + |
| 94 | +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. |
| 95 | + |
| 96 | +##### Set up authentication using Microsoft Entra ID |
| 97 | + |
| 98 | +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 |
| 99 | + |
| 100 | +```CLI |
| 101 | +az login |
| 102 | +``` |
| 103 | +> [!NOTE] |
| 104 | +> 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>` |
| 105 | + |
| 106 | +##### Set up authentication using access tokens |
| 107 | + |
| 108 | +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. |
| 109 | + |
| 110 | +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) |
| 111 | + |
| 112 | +2. [Set up authentication using access tokens](./how-to-manage-authentication.md#set-up-authentication-using-access-tokens) |
| 113 | + |
| 114 | +> [!CAUTION] |
| 115 | +> 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) |
| 116 | + |
| 117 | +## Enable artifacts in Playwright configuration |
| 118 | +In the `playwright.config.ts` file of your project, make sure you are collecting all the required artifacts. |
| 119 | +```typescript |
| 120 | + use: { |
| 121 | + trace: 'on-first-retry', |
| 122 | + video:'retain-on-failure', |
| 123 | + screenshot:'on' |
| 124 | + }, |
| 125 | +``` |
| 126 | + |
| 127 | +## Run your tests and publish results on Microsoft Playwright Testing |
| 128 | + |
| 129 | +You've now prepared the configuration for publishing test results with Microsoft Playwright Testing. Run tests using the newly created `playwright.service.config.ts` file and publish test results to the service. |
| 130 | + |
| 131 | + ```bash |
| 132 | + npx playwright test --config=playwright.service.config.ts |
| 133 | +``` |
| 134 | +> [!NOTE] |
| 135 | +> 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. |
| 136 | + |
| 137 | +After the test completes, you can view the test status in the terminal. |
| 138 | + |
| 139 | +```output |
| 140 | +Running 6 test using 2 worker |
| 141 | + 5 passed, 1 failed (20.2s) |
| 142 | + |
| 143 | +Test report: https://playwright.microsoft.com/workspaces/<workspace-id>/runs/<run-id> |
| 144 | +``` |
| 145 | + |
| 146 | +> [!CAUTION] |
| 147 | +> Depending on the size of your test suite, you might incur additional charges for the test minutes beyond your allotted free test minutes and free test results. |
| 148 | + |
| 149 | +## View test runs and results in the Playwright portal |
| 150 | + |
| 151 | +To view results and troubleshoot the test run with Microsoft Playwright Testing, |
| 152 | + |
| 153 | +[!INCLUDE [View test runs and results in the Playwright portal](./includes/include-playwright-portal-view-test-results.md)] |
| 154 | + |
| 155 | + |
| 156 | +> [!TIP] |
| 157 | +> 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. |
| 158 | + |
| 159 | +> [!NOTE] |
| 160 | +> The test results and artifacts that you publish are retained on the service for 90 days. After that, they are automatically deleted. |
| 161 | + |
| 162 | + |
| 163 | +## Next step |
| 164 | + |
| 165 | +You've successfully created a Microsoft Playwright Testing workspace in the Playwright portal and run your Playwright tests on cloud browsers. |
| 166 | + |
| 167 | +Advance to the next quickstart to set up continuous end-to-end testing by running your Playwright tests in your CI/CD workflow. |
| 168 | + |
| 169 | +> [!div class="nextstepaction"] |
| 170 | +> [Set up continuous end-to-end testing in CI/CD](./quickstart-automate-end-to-end-testing.md) |
0 commit comments