diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml new file mode 100644 index 0000000..b0104ac --- /dev/null +++ b/.github/workflows/run-tests.yml @@ -0,0 +1,25 @@ +name: Run Selenium tests on PR +on: pull_request +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: nanasess/setup-chromedriver@v2 + + - name: Set up JDK + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'microsoft' + + - name: Build with Maven + run: mvn test --file pom.xml + + - name: Upload failed test screenshots + uses: actions/upload-artifact@v3 + if: failure() + with: + name: my-artifact + path: failedScreenshots/* + retention-days: 7 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 6e03978..32cbd76 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.iml /target/ -/.idea/ \ No newline at end of file +/.idea/ +/failedScreenshots/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index dc3be6e..6cb5aa2 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,18 @@ UTF-8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.1.2 + + + + + org.seleniumhq.selenium diff --git a/src/main/java/cz/czechitas/automation/WebDriverProvider.java b/src/main/java/cz/czechitas/automation/WebDriverProvider.java index 901535c..68a1b4b 100644 --- a/src/main/java/cz/czechitas/automation/WebDriverProvider.java +++ b/src/main/java/cz/czechitas/automation/WebDriverProvider.java @@ -48,6 +48,7 @@ private static WebDriver createChromeDriver() { try { ChromeOptions options = new ChromeOptions(); options.addArguments("--remote-allow-origins=*"); + options.addArguments("--headless=new"); return WebDriverManager.chromedriver().capabilities(options).create(); } catch (WebDriverManagerException exception1) { return createEdgeDriver(); diff --git a/src/test/java/cz/czechitas/automation/ExampleTest.java b/src/test/java/cz/czechitas/automation/ExampleTest.java index 6e62f97..c2ca29e 100644 --- a/src/test/java/cz/czechitas/automation/ExampleTest.java +++ b/src/test/java/cz/czechitas/automation/ExampleTest.java @@ -18,6 +18,11 @@ void overKontaktniWwwAdresu() { overeni.overAdresuWwwStranky("www.czechitas.cz"); } + @Test + void failDemo() { + assert false; + } + @Test void overUspesnePrihlaseni() { prohlizec.prihlasovani.klikniNaTlacitkoPrihlasit(); diff --git a/src/test/java/cz/czechitas/automation/TestRunner.java b/src/test/java/cz/czechitas/automation/TestRunner.java index 42bbb9a..539d364 100644 --- a/src/test/java/cz/czechitas/automation/TestRunner.java +++ b/src/test/java/cz/czechitas/automation/TestRunner.java @@ -1,7 +1,9 @@ package cz.czechitas.automation; +import cz.czechitas.automation.extension.ScreenshotOnFailExtension; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.RegisterExtension; import org.openqa.selenium.WebDriver; import cz.czechitas.automation.assertion.AssertionFacade; @@ -18,10 +20,14 @@ class TestRunner { protected final SeleniumActionFacade prohlizec; protected final AssertionFacade overeni; + @RegisterExtension + final ScreenshotOnFailExtension screenshotExtension; + public TestRunner() { this.webDriver = WebDriverProvider.getWebDriver(); this.prohlizec = new SeleniumActionFacade(webDriver); this.overeni = new AssertionFacade(webDriver); + this.screenshotExtension = new ScreenshotOnFailExtension(webDriver); } @BeforeEach diff --git a/src/test/java/cz/czechitas/automation/extension/ScreenshotOnFailExtension.java b/src/test/java/cz/czechitas/automation/extension/ScreenshotOnFailExtension.java new file mode 100644 index 0000000..7126e36 --- /dev/null +++ b/src/test/java/cz/czechitas/automation/extension/ScreenshotOnFailExtension.java @@ -0,0 +1,36 @@ +package cz.czechitas.automation.extension; + +import org.apache.commons.io.FileUtils; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; +import org.openqa.selenium.OutputType; +import org.openqa.selenium.TakesScreenshot; +import org.openqa.selenium.WebDriver; + +import java.io.File; +import java.io.IOException; + +public class ScreenshotOnFailExtension implements TestExecutionExceptionHandler { + + private final WebDriver driver; + + public ScreenshotOnFailExtension(WebDriver driver) { + this.driver = driver; + } + + @Override + public void handleTestExecutionException(ExtensionContext context, Throwable cause) throws Throwable { + + if (driver != null) { + File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); + try { + String SCREENSHOT_DIRECTORY = "failedScreenshots/"; + FileUtils.copyFile(screenshotFile, new File(SCREENSHOT_DIRECTORY + context.getDisplayName() + ".png")); + } catch (IOException e) { + System.out.println("Could not save taken screenshot: " + e); + } + } + + throw cause; // Re-throw the exception to allow JUnit to handle it + } +}