Runs Selenium Web UI tests using Java, Gradle, WebDriverManager, and GitHub actions
- File -> New Project
- Choose project name, directory
- Under
dependencies
section inbuild.gradle
add:-
testImplementation 'junit:junit:4.13.2' implementation 'org.seleniumhq.selenium:selenium-java:4.4.0'
- Click on "Load Gradle Changes" (Shift + Command + I)
-
- We will need to create an instance of a web driver; we can use ChromeDriver for now
- Go to https://chromedriver.chromium.org/downloads to download the appropriate driver for your machine
- Unzip the file and move it to
/src/main/resources
⚠️ Note: In macOS, you may receiveerror: chromedriver cannot be opened because the developer cannot be verified.
We will need to make MacOS trustchromedriver
binary. This can be done in two steps:- Find the directory of
chromedriver
- Tell OS to trust the binary by lifting the quarantine:
xattr -d com.apple.quarantine /usr/local/bin/chromedriver
- Replace
/usr/local/bin
with wherever your driver is
- via Fixing error: “chromedriver” cannot be opened because the developer cannot be verified. Unable to launch the chrome browser on Mac OS ⚡ via TimOnWeb
- Find the directory of
- Unzip the file and move it to
- Go to https://chromedriver.chromium.org/downloads to download the appropriate driver for your machine
- Create a test class file
TestImplementation.java
in/src/test/java
-
import org.junit.*; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; public class TestImplementation { private WebDriver driver; @BeforeClass public static void setupWebdriverChromeDriver() { System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/main/resources/chromedriver"); } @Before public void setup() { driver = new ChromeDriver(); } @After public void teardown() { if (driver != null) { driver.quit(); } } @Test public void verifyGitHubTitle() { driver.get("https://www.github.com"); assertThat(driver.getTitle(), containsString("GitHub")); } }
-
- Code will work but you'll get
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
- Go to https://mvnrepository.com/artifact/org.slf4j/slf4j-simple/1.6.1 and add Gradle implementation:
-
// https://mvnrepository.com/artifact/org.slf4j/slf4j-simple testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'
-
- Go to https://mvnrepository.com/artifact/org.slf4j/slf4j-simple/1.6.1 and add Gradle implementation:
- Run the test
- You can do this via IntelliJ or via CLI:
gradle test
- You can do this via IntelliJ or via CLI:
- You can view HTML reports at the command line via
open build/reports/tests/test/index.html
- This time we'll use [[WebDriverManager]] to automate the browser setup
- After Part 1, add the following
build.gradle
dependency:testImplementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '3.3.0'
- In the test file, change
@BeforeClass
and@Before
appropriately:-
// ... import org.openqa.selenium.chrome.ChromeDriver; // import org.openqa.selenium.firefox.FirefoxDriver; // ... @BeforeClass public static void setupWebdriverBinary() { WebDriverManager.chromedriver().setup(); // WebDriverManager.firefoxdriver().setup(); } @Before public void setup() { driver = new ChromeDriver(); // driver = new FirefoxDriver(); }
-
- To prove we're not using the local ChromeDriver we previously added in Part 1, let's remove the
chromedriver
file and re-run our tests:rm src/main/resources/chromedriver
- See
using-webdriver
branch on https://github.com/ErnieAtLYD/java-selenium-exercise
- WebDriverManager
- Automating management of Selenium webdriver binaries with Java by Hrvoje Ruhek on Medium
- How to manage browsers binaries using WebDriverManager in Selenium?
- Let's try having a CI/CD such as GitHub Actions do the work for us
- We don't have to worry about ChromeDriver executables now that WebDriverManager is handling this
- But we DO have to assume that the Linux machine that GitHub actions will use won't have Chrome installed. We also have to figure out how to do this all in headless mode
- Headless mode operates as your typical browser would, but without a user interface, making it excellent for automated testing
- Go into headless mode
- add
import org.openqa.selenium.chrome.ChromeOptions;
- Change
setup()
to the following:-
@Before public void setup() { ChromeOptions options = new ChromeOptions(); options.addArguments("--no-sandbox"); options.addArguments("--disable-dev-shm-usage"); options.addArguments("--headless"); driver = new ChromeDriver(options); // driver = new FirefoxDriver(); }
-
- add
- Write a bash script called
InstallChrome.sh
that will install-
#!/bin/bash set -ex wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb sudo apt install ./google-chrome-stable_current_amd64.deb
-
- Create the GitHub action workflow
- Create
.github/workflows/gradle.yml
:-
name: Selenium Java CI on: [push] jobs: build: runs-on: ubuntu-latest # Using linux machine steps: - uses: actions/checkout@v2 # Checkout the code - name: Set up JDK 1.8 uses: actions/setup-java@v1 # Setup JAVA with: java-version: 1.8 - name: Install Google Chrome # Using shell script to install Google Chrome run: | chmod +x ./scripts/InstallChrome.sh ./scripts/InstallChrome.sh - name: Grant execute permission for gradlew run: chmod +x gradlew # give permission to Gradle to run the commands - name: Build with Gradle run: ./gradlew test --info # Run our tests using Gradle
-
- Submit a pull request
- Create
- See
using-github-actions
branch on https://github.com/ErnieAtLYD/java-selenium-exercise
- Running Selenium Web Tests with GitHub Actions by Moataz Nabil on LinkedIn
- Executing Selenium Tests ( Maven + Java ) with CI GitHub Actions by Saurabh Dubey on Medium
- How to setup Selenium Java with Gradle in IntelliJ by Milosz Wozniak on Medium
- https://github.com/miosz/intellij-selenium-java
- ChromeDriver - WebDriver for Chrome - Getting started