--- id: ci-intro title: "CI GitHub Actions" --- When installing Playwright you are given the option to add a [GitHub Actions](https://docs.github.com/en/actions). This creates a `playwright.yml` file inside a `.github/workflows` folder containing everything you need so that your tests run on each push and pull request into the main/master branch. **What you will learn:** - [GitHub Actions](#github-actions) - [Create a Repo and Push to GitHub](#create-a-repo-and-push-to-github) - [Opening the Workflows](#opening-the-workflows) - [Viewing Test Logs](#viewing-test-logs) - [HTML Report](#html-report) - [Downloading the HTML Report](#downloading-the-html-report) - [Viewing the HTML Report](#viewing-the-html-report) - [Viewing the Trace](#viewing-the-trace) - [Publishing report on the web](#publishing-report-on-the-web) - [What's Next](#whats-next) ## GitHub Actions Tests will run on push or pull request on branches main/master. The [workflow](https://docs.github.com/en/actions/using-workflows/about-workflows) will install all dependencies, install Playwright and then run the tests. It will also create the HTML report. ```yaml name: Playwright Tests on: push: branches: [main, master] pull_request: branches: [main, master] jobs: test: timeout-minutes: 60 runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: 18 - name: Install dependencies run: npm ci - name: Install Playwright Browsers run: npx playwright install --with-deps - name: Run Playwright tests run: npx playwright test - uses: actions/upload-artifact@v3 if: always() with: name: playwright-report path: playwright-report/ retention-days: 30 ``` ### Create a Repo and Push to GitHub [Create a repo on GitHub](https://docs.github.com/en/get-started/quickstart/create-a-repo) and create a new repository or push an existing repository. Follow the instructions on GitHub and don't forget to [initialize a git repository](https://github.com/git-guides/git-init) using the `git init` command so you can [add](https://github.com/git-guides/git-add), [commit](https://github.com/git-guides/git-commit) and [push](https://github.com/git-guides/git-push) your code. Create a Repo and Push to GitHub ### Opening the Workflows Click on the **Actions** tab to see the workflows. Here you will see if your tests have passed or failed. Opening the Workflows On Pull Requests you can also click on the **Details** link in the [PR status check](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/collaborating-on-repositories-with-code-quality-features/about-status-checks). pr status checked ### Viewing Test Logs Clicking on the workflow run will show you the all the actions that GitHub performed and clicking on **Run Playwright tests** will show the error messages, what was expected and what was received as well as the call log. Viewing Test Logs ## HTML Report The HTML Report shows you a full report of your tests. You can filter the report by browsers, passed tests, failed tests, skipped tests and flaky tests. ### Downloading the HTML Report In the Artifacts section click on the **playwright-report** to download your report in the format of a zip file. Downloading the HTML Report ### Viewing the HTML Report Locally opening the report will not work as expected as you need a web server in order for everything to work correctly. First, extract the zip, preferably in a folder that already has Playwright installed. Using the command line change into the directory where the report is and use `npx playwright show-report` followed by the name of the extracted folder. This will serve up the report and enable you to view it in your browser. ```bash npx playwright show-report name-of-my-extracted-playwright-report ``` Playwright HTML Report To learn more about reports check out our detailed guide on [HTML Reporter](/test-reporters.md#html-reporter) ### Viewing the Trace Once you have served the report using `npx playwright show-report`, click on the trace icon next to the test's file name as seen in the image above. You can then view the trace of your tests and inspect each action to try to find out why the tests are failing. Playwright Trace Viewer ## Publishing report on the web Downloading the HTML report as a zip file is not very convenient. However, we can utilize Azure Storage's static websites hosting capabilities to easily and efficiently serve HTML reports on the Internet, requiring minimal configuration. 1. Create an [Azure Storage account](https://learn.microsoft.com/en-us/azure/storage/common/storage-account-create). 1. Enable [Static website hosting](https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website-how-to#enable-static-website-hosting) for the storage account. 1. Create a Service Principal in Azure and grant it access to Azure Blob storage. Upon successful execution, the command will display the credentials which will be used in the next step. ```bash az ad sp create-for-rbac --name "github-actions" --role "Storage Blob Data Contributor" --scopes /subscriptions//resourceGroups//providers/Microsoft.Storage/storageAccounts/ ``` 1. Use the credentials from the previous step to set up encrypted secrets in your GitHub repository. Go to your repository's settings, under [GitHub Actions secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository), and add the following secrets: - `AZCOPY_SPA_APPLICATION_ID` - `AZCOPY_SPA_CLIENT_SECRET` - `AZCOPY_TENANT_ID` For a detailed guide on how to authorize a service principal using a client secret, refer to [this Microsoft documentation](https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-authorize-azure-active-directory#authorize-a-service-principal-by-using-a-client-secret-1). 1. Add a step that uploads HTML report to Azure Storage. ```yaml ... - name: Upload HTML report to Azure shell: bash run: | REPORT_DIR='run-${{ github.run_id }}-${{ github.run_attempt }}' azcopy cp --recursive "./playwright-report/*" "https://.blob.core.windows.net/\$web/$REPORT_DIR" echo "::notice title=HTML report url::https://.z1.web.core.windows.net/$REPORT_DIR/index.html" env: AZCOPY_AUTO_LOGIN_TYPE: SPN AZCOPY_SPA_APPLICATION_ID: '${{ secrets.AZCOPY_SPA_APPLICATION_ID }}' AZCOPY_SPA_CLIENT_SECRET: '${{ secrets.AZCOPY_SPA_CLIENT_SECRET }}' AZCOPY_TENANT_ID: '${{ secrets.AZCOPY_TENANT_ID }}' ``` The contents of `$web` storage container can be accessed from a browser by using the [public URL](https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blob-static-website-how-to?tabs=azure-portal#portal-find-url) of the website. :::note This step will not work for pull requests created from a forked repository because such workflow [doesn't have access to the secrets](https://docs.github.com/en/actions/security-guides/encrypted-secrets#using-encrypted-secrets-in-a-workflow). ::: To learn more about traces check out our detailed guide on [Trace Viewer](/trace-viewer.md). To learn more about running tests on CI check out our detailed guide on [Continuous Integration](/ci.md). ## What's Next - [Learn how to use Locators](./locators.md) - [Learn how to perform Actions](./input.md) - [Learn how to write Assertions](./test-assertions.md)