|
| 1 | +--- |
| 2 | +title: 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/banner.png |
| 9 | +--- |
| 10 | + |
| 11 | +This is a blog about how we can run Playwright UI tests in GitHub Actions. If you are not familiar and have never written UI tests using Playwright, then it would be good to check at this blog about Playwright. Similarly, if you are unfamiliar with GitHub Actions, you can check another blog about GitHub Actions for beginners. |
| 12 | + |
| 13 | +## Why to run tests on CI (continuous integration)? |
| 14 | +Tests are run on CI 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 | +**- 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 | +**- Fast feedback loop:** |
| 20 | +Developers get immediate notification for failing tests. That way they can fix bugs immediately and iterate quickly. |
| 21 | + |
| 22 | +**- 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 | +**- Improved code quality:** |
| 26 | +Regular testing done in CI, ensure existing functionality does not become unusable if changes are made. |
| 27 | + |
| 28 | +**- 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 | +**- 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 | +**Project Setup** |
| 46 | + |
| 47 | +```bash |
| 48 | +npm install |
| 49 | +``` |
| 50 | + |
| 51 | +**Compile and hot-reload for development** |
| 52 | + |
| 53 | +```bash |
| 54 | +npm install -g json-server |
| 55 | +json-server db.json |
| 56 | +``` |
| 57 | + |
| 58 | +## Running Tests Locally |
| 59 | + |
| 60 | +To run the UI tests locally, use the following command: |
| 61 | + |
| 62 | +```bash |
| 63 | +npm run test:e2e tests |
| 64 | +``` |
| 65 | + |
| 66 | +Like this, running the tests locally needs to run the following commands: |
| 67 | + |
| 68 | +```bash |
| 69 | +npm run test:e2e tests |
| 70 | +``` |
| 71 | + |
| 72 | +## Setting Up CI in GitHub Actions |
| 73 | +Like locally we will now set up the system in CI like running locally: |
| 74 | + |
| 75 | + |
| 76 | +```yml |
| 77 | +name: Run-project |
| 78 | + |
| 79 | +on: |
| 80 | + push: |
| 81 | + branches: |
| 82 | + - master |
| 83 | + pull_request: |
| 84 | + branches: |
| 85 | + - master |
| 86 | + workflow_dispatch: |
| 87 | + |
| 88 | +jobs: |
| 89 | + run-Restro-project: |
| 90 | + runs-on: ubuntu-latest |
| 91 | + steps: |
| 92 | + - name: Checkout repo code |
| 93 | + uses: actions/checkout@v3 |
| 94 | + |
| 95 | + - name: set up node |
| 96 | + uses: actions/setup-node@v3 |
| 97 | + with: |
| 98 | + node-version: 20.x |
| 99 | + |
| 100 | + - name: Build and run the project |
| 101 | + run: | |
| 102 | + npm install |
| 103 | + npm run serve & |
| 104 | + npm install -g json-server |
| 105 | + json-server db.json & |
| 106 | +
|
| 107 | + - name: wait for services |
| 108 | + run: | |
| 109 | + sudo apt-get install wait-for-it -y |
| 110 | + wait-for-it -h localhost -p 8080 -t 10 |
| 111 | + wait-for-it -h localhost -p 3000 -t 10 |
| 112 | +
|
| 113 | + - name: run web-ui tests |
| 114 | + run: | |
| 115 | + npx playwright install |
| 116 | + npm run test:e2e tests |
| 117 | +``` |
| 118 | +
|
| 119 | +
|
| 120 | +## What this Workflow Does? |
| 121 | +This GitHub Actions file runs when you: |
| 122 | +- Push to the master branch |
| 123 | +- Create a pull request to master |
| 124 | +- Run the workflow manually |
| 125 | +
|
| 126 | +It has one job called run-Restro-project with these steps: |
| 127 | +1. **Checkout repo code** - Gets the project code from GitHub. |
| 128 | +2. **Set up node** - Installs Node.js version 20. |
| 129 | +3. **Build and run the project** - Installs dependencies, starts the Vue app, and starts the backend using json-server. |
| 130 | +4. **Wait for services** - Waits for the frontend (port 8080) and backend (port 3000) to be ready. |
| 131 | +5. **Run web-ui tests** - Installs Playwright and runs the UI tests. |
| 132 | +
|
| 133 | +## Conclusion |
| 134 | +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