Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.iml

/target/
/.idea/
/.idea/
/failedScreenshots/
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/cz/czechitas/automation/ExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ void overKontaktniWwwAdresu() {
overeni.overAdresuWwwStranky("www.czechitas.cz");
}

@Test
void failDemo() {
assert false;
}

@Test
void overUspesnePrihlaseni() {
prohlizec.prihlasovani.klikniNaTlacitkoPrihlasit();
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/cz/czechitas/automation/TestRunner.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}