Template project to run tests with Selenium and Java.
- Selenium
- Chrome driver and Chrome browser for your platform. The driver must match with the browser version.
- Java SE 10+
Get a local copy up and running by following these steps.
Choose an option below. You can develop in your browser or locally.
Gitpod is an online dev environment based on VS Code/Theia and can launch ready-to-code development environments for GitHub projects with a single click. It is free for open source projects like this one and a nice way to sharing code with others. You can open https://gitpod.io/#https://github.com/justunsix/selenium-java-tests or push the Gitpod ready-to-code button at the top of this README.
This option right now is only useful for development. To run the program, a local installation is recommended since it requires a Chrome browser installed.
- Clone this repository to your local computer using
git clone https://github.com/justunsix/selenium-java-tests.git
. - Install Java Development Kit - Set your
JAVA_HOME
environment variable to point to Java folder and add the JDK's bin folder to yourpath
environment variable. - Install Maven - add Maven's bin folder to your
path
environment variable. - Install Chrome in the default location and the Chrome driver, making sure the versions match and add the Chrome driver binary to your
path
environment variable. Web Driver Manager is an alternative and efficient way to manage this part; however, this repository does not use it as a dependency.- Add the chrome driver folder to your 'path' environment variable.
- Open your Chrome browser to check it works. If Chrome opens up as a black screen disable hardware acceleration using this workaround.
Confirm installations and pathes are setup in a terminal
# Maven
mvn -v
# Java
java -version
Build and run the project
cd training
mvn package
# Run main method in class AppExample
mvn exec:java -Dexec.mainClass="training.AppExample"
After running it, Chrome will briefly open and visit https://the-internet.herokuapp.com/, a site for test automation, and then close. You will see something like below in the terminal indicating the driver visited and got a link text meaning the run was successful.
[[ChromeDriver: chrome on WINDOWS (d40a87asdas31c6a93304e19e3)] -> partial link text: Testing]
=== Test Driver Complete ===
Inspect the Java model framework for https://the-internet.herokuapp.com/ and https://formy-project.herokuapp.com/ and see and modify test cases for The-Internet site.
When developing, you may want to download the dependency's source with mvn dependency:sources
and/or Javadocs with mvn dependency:resolve -Dclassifier=javadoc
Placeholders:
- Use this space to show useful examples of how a project can be used. Additional screenshots, code examples and demos work well in this space. You may also link to more resources.
- For more examples, please refer to the Documentation
- Follow course Seleium Webdriver Java course and try code here.
Contributions are what make the open source community a good place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Justin Tung - @justin_tung
Project Link: https://github.com/justunsix/selenium-java-tests
- Best-README-Template: A README template to jumpstart projects
- Java code samples from Web Browser Automation with Selenium and Java by Shadab Ansari for demonstration code in
AppExample.java
- Test automation site https://the-internet.herokuapp.com/
Initialize project in this repository's folder using a Maven quickstart template
mvn archetype:generate -DgroupId=training -DartifactId=training -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
Add Selenium's dependency to Maven's pom. Added Chrome driver dependency from MVN for testing. Other WebDrivers can be added.
- IntelliJ or VS Code with Java Extensions per VS Code Java Tutorial and VS Code Java Linting - formatter, problem and vulnerability analysis
- Install JDK (in VS Code quickstrart or separate installation like JDK 64 Portable (Windows).
- Install Chrome and Chrome driver for Version 91.0.4472.77 (Official Build) (32-bit).
See more options at Maven download sources javadoc
mvn dependency:sources
mvn dependency:sources dependency:resolve -Dclassifier=javadoc
If using VS Code, ctrl+click into a class and then right click to attach the source for the dependency.
"java.configuration.runtimes": [
{
"name": "JavaSE-1.8",
"path": "C:\\usr\\bin\\JDK64",
"sources": "C:\\usr\\bin\\JDK64\\lib\\src.zip",
"javadoc": "https://docs.oracle.com/javase/8/docs/api/",
"default": true
},
{
// Installed with VS Code Java quickstart
"name": "JavaSE-11",
"path": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.11.9-hotspot",
"sources": "C:\\Program Files\\AdoptOpenJDK\\jdk-11.0.11.9-hotspot\\lib\\src.zip",
"javadoc": "https://docs.oracle.com/en/java/javase/11/docs/api/",
"default": true
}
// Sonarlint: selenium-java-tests project binding, make sure to configure the connection in your workspace settings or global settings
"sonarlint.connectedMode.project": {
"projectKey": "selenium-java-tests"
}
# Run a single class
java -cp target/base-1.0-SNAPSHOT.jar base.BaseTests
# Execute a single method called testSuccessfulCloseAndSeeModalLink in ModalEntryAdTests class in src/test
mvn -Dtest=HorizontalSliderTests#testSliding test
# Run main testing class, cleanupDaemonThreads is required otherwise
# there is a java.lang.IllegalThreadStateException or use System.exit(0);
# at the end of the program
mvn exec:java -Dexec.mainClass="training.AppExample" -Dexec.cleanupDaemonThreads=false
# Build, test and scan code for upload to sonarcloud.io
# Requires access to the project in Sonar Cloud and
# connect in environment variable / project settings > Analysis method > Manual
mvn verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar