|
| 1 | +--- |
| 2 | +title: Playwright and GitHub Actions - Run tests in CI |
| 3 | +authorName: Nabin Ale |
| 4 | +authorAvatar : https://avatars.githubusercontent.com/u/61624650?v=4 |
| 5 | +authorLink: https://github.com/nabim777 |
| 6 | +createdAt: Apr 8, 2025 |
| 7 | +tags: continuous integration, continuous delivery, continuous deployment, github actions, ci, cd, playwright |
| 8 | +banner: https://blog.jankaritech.com/src/assets/RunPlaywrightOnCI/images/githubaction_with_playwright_banner |
| 9 | +--- |
| 10 | + |
| 11 | +This is a blog about how we can run Playwright UI tests in GitHub Actions. If you are not familiar and new to UI testing using Playwright, then it would be good to check at this blog about [Playwright](https://blog.jankaritech.com/#/blog/E2E%20Testing%20using%20BDD%20with%20Playwright/Behavior%20Driven%20Development%20(BDD)%20using%20Playwright). Similarly, if you are unfamiliar with GitHub Actions, you can check another blog about [Introduction to GitHub Actions](https://blog.jankaritech.com/#/blog/Introduction%20to%20GitHub%20Actions%20-%20CI%20%26%20CD). |
| 12 | + |
| 13 | +## 🤔 Why to run tests on CI? |
| 14 | +Tests are run on CI(continuous integration) because it ensures that code runs properly every time you make a change and is in an isolated, clean environment. Here are some reasons: |
| 15 | + |
| 16 | +**1. Early bug detection:** |
| 17 | +By automatically running tests after every code commit, you can quickly identify issues as they arise, preventing them from accumulating and causing larger problems later on. |
| 18 | + |
| 19 | +**2. Fast feedback loop:** |
| 20 | +Developers get immediate notification for failing tests. That way they can fix bugs immediately and iterate quickly. |
| 21 | + |
| 22 | +**3. Consistent testing environment:** |
| 23 | +The CI servers run tests in a uniform environment, and therefore there is no correlation between the developer configurations. |
| 24 | + |
| 25 | +**4. Improved code quality:** |
| 26 | +Regular testing done in CI, ensure existing functionality does not become unusable if changes are made. |
| 27 | + |
| 28 | +**5. Reduced integration issues:** |
| 29 | +By regularly integrating code and testing that code in a CI environment you reduce the risk of conflicts when merging large patches of code. |
| 30 | + |
| 31 | +**6. Automated process:** |
| 32 | +CI systems enable developers to save time and effort testing things. |
| 33 | + |
| 34 | +## 📘 About the Project |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +In this blog, I have taken simple applications built using Vue.js. Let me introduce the app that the CI will run tests on. The GitHub repository is available at: https://github.com/nabim777/momo-restro-list.git |
| 39 | + |
| 40 | +This is a basic application that includes login and logout functionality. A UI test has been written using Playwright to verify the login feature. |
| 41 | + |
| 42 | +## 🛠️ Running App Locally |
| 43 | +To run the application locally, follow these steps: |
| 44 | + |
| 45 | +**1. Project Setup** |
| 46 | + |
| 47 | +Install all dependencies: |
| 48 | + |
| 49 | +```bash |
| 50 | +npm install |
| 51 | +``` |
| 52 | + |
| 53 | +**2. Compile and hot-reload for development** |
| 54 | + |
| 55 | +To run both the frontend and backend locally: |
| 56 | + |
| 57 | +```bash |
| 58 | +npm run serve # Start frontend |
| 59 | +npm run backend # Start backend |
| 60 | +``` |
| 61 | + |
| 62 | +## 🧪 Running Tests Locally |
| 63 | + |
| 64 | +> **_NOTE:_** Make sure the application and backend are running before you start the tests. |
| 65 | +
|
| 66 | +To run the UI tests locally, use the following command: |
| 67 | + |
| 68 | +```bash |
| 69 | +npm run test:e2e tests |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | +## ⚙️ Setting Up CI in GitHub Actions |
| 75 | +Like locally we will now set up the system in CI. for that first of all, create a file `ci.yml` in a project using the following folder structure: |
| 76 | + |
| 77 | +``` |
| 78 | +📦momo-restro-list |
| 79 | +┗ 📂.github |
| 80 | +┃ ┣ 📂workflows |
| 81 | +┃ ┃ ┗ 📜ci.yml |
| 82 | +``` |
| 83 | + |
| 84 | +Then, add below code in the `ci.yml`. |
| 85 | + |
| 86 | +```yml |
| 87 | +name: Run-project |
| 88 | + |
| 89 | +on: |
| 90 | + push: |
| 91 | + branches: |
| 92 | + - master |
| 93 | + pull_request: |
| 94 | + branches: |
| 95 | + - master |
| 96 | + |
| 97 | +jobs: |
| 98 | + run-Restro-project: |
| 99 | + runs-on: ubuntu-latest |
| 100 | + steps: |
| 101 | + - name: Checkout repo code |
| 102 | + uses: actions/checkout@v3 |
| 103 | + |
| 104 | + - name: set up node |
| 105 | + uses: actions/setup-node@v3 |
| 106 | + with: |
| 107 | + node-version: 20.x |
| 108 | + |
| 109 | + - name: Run the project |
| 110 | + run: | |
| 111 | + npm run serve & |
| 112 | + npm run backend & |
| 113 | +
|
| 114 | + - name: wait for services |
| 115 | + run: | |
| 116 | + sudo apt-get install wait-for-it -y |
| 117 | + wait-for-it -h localhost -p 8080 -t 10 |
| 118 | + wait-for-it -h localhost -p 3000 -t 10 |
| 119 | +
|
| 120 | + - name: run web-ui tests |
| 121 | + run: | |
| 122 | + npx playwright install |
| 123 | + npm run test:e2e tests |
| 124 | +``` |
| 125 | +
|
| 126 | +
|
| 127 | +## 🔍 What this Workflow Does? |
| 128 | +This GitHub Actions file runs when you push to the master branch or create a pull request to master |
| 129 | +
|
| 130 | +It has one job called run-Restro-project with these steps: |
| 131 | +1. **Checkout repo code** - Gets the project code from GitHub. |
| 132 | +2. **Set up node** - Installs Node.js version 20. |
| 133 | +3. **Run the project** - Starts the Vue app, and starts the backend using json-server. |
| 134 | +4. **Wait for services** - Waits for the frontend (port 8080) and backend (port 3000) to be ready. |
| 135 | +5. **Run web-ui tests** - Installs Playwright and runs the UI tests. |
| 136 | +
|
| 137 | +## 📝 Conclusion |
| 138 | +Using GitHub Actions to run your Playwright UI tests ensures your app is always tested in a clean, repeatable environment. It helps catch bugs early, improves collaboration, and keeps your project in a healthy state. |
0 commit comments