diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..f1d61ca --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,100 @@ +name: Dockerized App Tests + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +permissions: + checks: write + contents: read + issues: read + pull-requests: write + +jobs: + integration-tests: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Install jq + run: sudo apt-get update && sudo apt-get install -y jq + + - name: Build containers + run: docker compose build + + - name: Start app + test stack + run: docker compose up -d + + - name: Wait for selenium-hub to be ready + run: | + echo "Waiting for selenium-hub to be ready..." + for i in {1..100}; do + if docker logs selenium-grid-docker-selenium-hub-1 2>&1 | grep -q "Started Selenium Hub"; then + echo "Selenium Hub is ready!" + break + fi + sleep 2 + done + + - name: Log Selenium Grid Status + run: | + echo "Grid status:" + curl -s http://localhost:4444/status | jq . + + - name: Print environment variables + run: env + + - name: Show Docker networks + run: docker network ls && docker network inspect bridge + + - name: Show Docker container status before tests + run: docker ps -a + + - name: Show Docker container memory usage + run: docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" + + - name: Run tests in eatest + run: docker exec eatest dotnet test /src/EAAppTest/EAAppTest.csproj --logger:trx --no-build --configuration Release + + - name: Show Docker container memory usage + run: docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" + + - name: Copy test results from container (if running) + run: | + mkdir -p test-results + if docker ps -a --format '{{.Names}}' | grep -q '^eatest$'; then + docker cp eatest:/src/EAAppTest/TestResults test-results || echo "Could not copy test results; container may have exited." + else + echo "eatest container is not running or has exited. Skipping docker cp." + fi + + - name: Upload test results + uses: actions/upload-artifact@v4 + with: + name: test-results + path: test-results/TestResults/*.trx + + - name: Publish Test Results + uses: EnricoMi/publish-unit-test-result-action@v2 + with: + files: 'test-results/TestResults/*.trx' + + - name: Show test container logs on failure + if: failure() + run: docker logs eatest + + - name: Show selenium hub logs on failure + if: failure() + run: docker logs selenium-grid-docker-selenium-hub-1 + + - name: Show Docker container memory usage on failure + if: failure() + run: docker stats --no-stream --format "table {{.Name}}\t{{.MemUsage}}\t{{.MemPerc}}" + diff --git a/EAAppTest/Dockerfile b/EAAppTest/Dockerfile index da251c3..48ecf2f 100644 --- a/EAAppTest/Dockerfile +++ b/EAAppTest/Dockerfile @@ -7,6 +7,9 @@ WORKDIR /app FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build WORKDIR /src +# Install ping utility +#RUN apt-get update && apt-get install -y iputils-ping + # Copy only the test first for layer caching COPY ["EAAppTest/EAAppTest.csproj", "EAAppTest/"] RUN dotnet restore "EAAppTest/EAAppTest.csproj" @@ -21,4 +24,11 @@ RUN dotnet build "EAAppTest.csproj" -c Release # Run the tests FROM build AS test-runner WORKDIR "/src/EAAppTest" -CMD ["dotnet", "test", "--no-build", "--logger:trx"] + +# Debug: list contents of build output directory +RUN ls -l /src/EAAppTest/bin/Release/net6.0/ + +# Run the tests using the .csproj and explicitly set Release config +# CMD ["dotnet", "test", "EAAppTest.csproj", "--configuration", "Release", "--logger:trx;LogFileName=test-results.trx"] +CMD ["/bin/sh", "-c", "dotnet test EAAppTest.csproj --configuration Release --logger:trx;LogFileName=test-results.trx; tail -f /dev/null"] + diff --git a/EAAppTest/DriverFixture.cs b/EAAppTest/DriverFixture.cs index 08f3fb2..4f27fd0 100644 --- a/EAAppTest/DriverFixture.cs +++ b/EAAppTest/DriverFixture.cs @@ -13,13 +13,26 @@ public class DriverFixture : IDisposable public void Setup(BrowserType browserType) { - driver = new RemoteWebDriver(new Uri("http://selenium-hub:4444/"), GetBrowserOptions(browserType)); + int retries = 9; + Exception? lastException = null; - if (driver == null) + for (int i = 0; i < retries; i++) { - // Do something here + try + { + driver = new RemoteWebDriver( + new Uri("http://selenium-hub:4444/wd/hub"), + GetBrowserOptions(browserType)); + return; // Exit if driver is successfully initialized + } + catch (Exception ex) + { + lastException = ex; + System.Threading.Thread.Sleep(8000); // Wait before retrying + } } + throw new InvalidOperationException("Driver initialization failed after retries", lastException); } // This solution provided by co-pilot. @@ -42,7 +55,7 @@ private dynamic GetBrowserOptions(BrowserType browserType) case BrowserType.Chrome: { var chromeOption = new ChromeOptions(); - chromeOption.AddAdditionalOption("se:recordVideo", true); + //chromeOption.AddAdditionalOption("se:recordVideo", true); return chromeOption; } default: diff --git a/EAAppTest/UnitTest2.cs b/EAAppTest/UnitTest2.cs index bf92021..f6a1362 100644 --- a/EAAppTest/UnitTest2.cs +++ b/EAAppTest/UnitTest2.cs @@ -9,7 +9,7 @@ public class UnitTest2 : IClassFixture public UnitTest2(DriverFixture driverFixture) { - driverFixture.Setup(BrowserType.Firefox); + driverFixture.Setup(BrowserType.Chrome); this.driverFixture = driverFixture; } diff --git a/docker-compose.yml b/docker-compose.yml index 273fdfa..47c3800 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -40,11 +40,25 @@ services: networks: - ea_network - node-docker: - image: selenium/node-docker:4.1.0-20211209 - volumes: - - ./assets:/opt/selenium/assets - - ./config.toml:/opt/bin/config.toml + # selenium-hub: + # image: selenium/hub:4.1.0-20211209 + # container_name: selenium-hub + # ports: + # - "4442:4442" + # - "4443:4443" + # - "4444:4444" + # networks: + # - ea_network + + selenium-hub: + image: selenium/hub:4.1.0 + ports: + - "4444:4444" + networks: + - ea_network + + chrome: + image: selenium/node-chrome:4.1.0 depends_on: - selenium-hub environment: @@ -54,13 +68,14 @@ services: networks: - ea_network - selenium-hub: - image: selenium/hub:4.1.0-20211209 - container_name: selenium-hub - ports: - - "4442:4442" - - "4443:4443" - - "4444:4444" + firefox: + image: selenium/node-firefox:4.1.0 + depends_on: + - selenium-hub + environment: + - SE_EVENT_BUS_HOST=selenium-hub + - SE_EVENT_BUS_PUBLISH_PORT=4442 + - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 networks: - ea_network @@ -71,9 +86,10 @@ services: context: . dockerfile: EAAppTest/Dockerfile restart: on-failure - depends_on: - - selenium-hub - - ea_webapp + depends_on: + ea_webapp: + condition : service_started + networks: - ea_network