02loveslollipop-15149890436-workflow_dispatch-react-app-test-selenium-ci #66
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #This workflow test the react app with selenium using python to run the selenium tests | |
| name: React App Test with Selenium | |
| description: This workflow tests the react app with selenium using python to run the selenium tests | |
| run-name: "${{ github.actor }}-${{ github.run_id }}-${{ github.event_name }}-react-app-test-selenium-ci" | |
| # concurrency: | |
| # group: default #limit concurrency to one job at a time (priority to the latest job) | |
| on: | |
| push: | |
| branches: [ main ] | |
| pull_request: | |
| branches: [ main ] | |
| workflow_dispatch: | |
| jobs: | |
| sonarqube: | |
| name: SonarQube | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| cache: 'npm' | |
| cache-dependency-path: react/package-lock.json | |
| - name: Run npm install | |
| run: | | |
| cd react | |
| npm ci | |
| - name: SonarQube Scan | |
| uses: SonarSource/sonarqube-scan-action@v5 | |
| env: | |
| SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
| selenium: | |
| name: Selenium | |
| runs-on: ubuntu-latest | |
| needs: sonarqube | |
| env: | |
| APP_URL: 'http://localhost:3000' | |
| DISPLAY: :99 | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Chrome and ChromeDriver | |
| uses: nanasess/setup-chromedriver@v2 | |
| - name: Setup Xvfb | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y xvfb | |
| Xvfb :99 -screen 0 1280x1024x24 > /dev/null 2>&1 & | |
| echo "DISPLAY=:99" >> $GITHUB_ENV | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '18' | |
| cache: 'npm' | |
| cache-dependency-path: react/package-lock.json | |
| - name: Install React dependencies | |
| run: | | |
| cd react | |
| npm ci | |
| - name: Build React app | |
| run: | | |
| cd react | |
| npm run build | |
| - name: Start React server | |
| run: | | |
| cd react | |
| npm run preview -- --port 3000 & | |
| # Allow time for the server to start up | |
| sleep 10 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.12' | |
| cache: 'pip' | |
| - name: Install Python dependencies | |
| run: | | |
| # Check if tests directory exists, if not create it | |
| mkdir -p tests | |
| # Check if requirements.txt exists, if not create it | |
| if [ ! -f tests/requirements.txt ]; then | |
| echo "selenium" > tests/requirements.txt | |
| fi | |
| pip install -r tests/requirements.txt | |
| - name: Run Selenium tests | |
| run: | | |
| cd tests | |
| pytest us1.py us2.py us3.py us4.py us5.py us7.py us8.py us9.py us10.py | |
| - name: Upload screenshots artifact (on failure) | |
| if: failure() | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: selenium-test-screenshots | |
| path: tests/screenshots/ | |
| retention-days: 7 | |
| - name: Stop React server | |
| if: always() # Ensure server is stopped even if tests fail | |
| run: | | |
| # Find and kill the process listening on port 3000 | |
| PID=$(lsof -t -i:3000) | |
| if [ -n "$PID" ]; then | |
| kill $PID | |
| echo "React server stopped." | |
| else | |
| echo "React server not found or already stopped." | |
| fi |