Selenium is an open-source test automation framework for web apps. It provides a test domain-specific language allowing developers and testers to write tests in many languages such as Java and Python. Selenium relies on WebDriver, making it possible to run the same tests (with some modification) interchangeably in many web browsers.
Important note: Selenium tests run on a web browser. Whenever you update your web browser, please verify that your Selenium's browser driver supports the version of your web browser. If it does not support, you need to download and set up the browser driver. Remember to verify that their versions are compatible.
A Java Development Kit (JDK) includes the Java Runtime Environment (JRE) that is required to write and run Java program. If you have already installed JDK on your machine, please skip this step.
Note: this introduction uses JUnit 5 to verify the behavior of the program under test; JUnit 5 requires Java 8 or higher.
java -version
.
You should see a screen showing the Java version on your machine.
This introduction uses Eclipse. You may use any Java IDE of your choice and configure Selenium as appropriate. If you have already installed Eclipse (or any Java IDE), please skip this step.
Eclipse.app
for macOS, Eclipse.exe
for Windows)
Note: Eclipse comes with JUnit 5. Thus, no additional set up is needed for JUnit 5.
If you use other Java IDEs, please verify if JUnit 5 is included. Many Java IDEs come with JUnit 5 (e.g., Eclipse, NetBeans, IntelliJ, and Visual Studio Code).
If you do not already have JUnit 5 on your machine, you may download JUnit 5 (https://junit.org/junit5/), use any Java IDE or command line to set up. Depending on your machine's configuration, you may need to set the environment path.
This introduction uses Selenium 4. WebDriver, the core of Selenium, is an interface to write instruction sets that can be run interchangeably in many web browsers. Later, you will download and set up a web browser driver. You will then configure the browser driver through WebDriver. If you have already installed Selenium 4, please skip this step.
.jar
files
to configure Selenium WebDriver with Eclipse.
The browser driver interacts with the real web browser and instructs it to execute the actions as specified in a Selenium test. Each browser requires a browser driver specific to it.
In practice, testers would run tests on multiple web browsers to ensure the program under test behaves appropriately across browsers.
Depending on which web browser(s) you use, download the appropriate browser driver(s). Please be sure to verify the version of your web browser.
Alternatively, you may download the browser driver from the following locations directly:Later, you will configure web browser driver through Selenium's WebDriver.
It appears that web browsers update more often than the drivers. For simplicity, we recommend that you try not to update your web browser too often. Otherwise, you may need to re-configure your Selenium test environment or sometimes need to downgrade your web browser. Please be sure to verify that the driver for the specific version is available before updating your web browser.
Eclipse.app
for macOS, Eclipse.exe
for Windows)
File > New > Project..
,
select Java Project
,
then enter your project name
Depending on your Eclipse version and configuration, Java Project
may appear by default,
and thus you may not need to click on the Project..
option.
Eclipse works in project. It is important that you first create a Java project. You will later create a Selenium test file in this project.
src
folder,
select New > Package
, then enter your package name (assume the package is named "test")
Note: a package is optional but recommended. This is to help you organize your files.
New > JUnit Test Case
, enter your file name
Be sure to select New JUnit Jupiter Test
to create JUnit 5 test
so that Eclipse will automatically add JUnit 5 library to Java Build Path of the project.
Alternatively, you may manually add JUnit 5 library to the project.
Select your project, then click the Project > Properties
menu.
Select Java Build Path > Libraries > Classpath > Add Library...
,
then select JUnit
Depending on your Eclipse version, if there is no Classpath
to select, simply click the
Add Library...
button.
Project > Properties
menu,
then select Java Build Path > Libraries > Classpath > Add External JARs...
Depending on your Eclipse version, if there is no Classpath
to select, simply click the
Add External JARs...
button.
.jar
files and add them to the classpath.
(note: make sure that they are added to the classpath, not the modulepath)
You classpath should show all the .jar
files.
Be sure to click Apply and Close
button.
Be sure to add all .jar
files.
lib
folder, select all .jar
files and add them to the classpath.
You classpath should show all the .jar
files.
Be sure to click Apply and Close
button.
Be sure to add all .jar
files.
WebDriver
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver; // for chrome import org.openqa.selenium.firefox.FirefoxDriver; // for Firefox
Be sure to replace path/to/chromedriver
with the actual path to your chromedriver
.
class FirstSeleniumTest { private WebDriver driver; private String url = "http://www.google.com"; @BeforeEach void setUp() throws Exception { // configure path to the driver System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // create an instance of the web browser and open it driver = new ChromeDriver(); // open the given url driver.get(url); } @AfterEach void tearDown() throws Exception { // close the browser driver.close(); } @Test public void test_openURL() { // check if we are on the right page assertEquals(driver.getTitle(), "Google"); } }
If you use Firefox, use the following setUp()
and replace path/to/geckodriver
with
the actual path to your geckodriver
.
@BeforeEach void setUp() throws Exception { // configure path to the driver System.setProperty("webdriver.gecko.driver", "path/to/geckodriver"); // create an instance of the web browser and open it driver = new FirefoxDriver(); // open the given url driver.get(url); }
Click the file name, then select Run as > JUnit Test
Alternatively, you may use the Toolbar, run icon
()
If everything is set up properly,
you should see a web browser open and close. The test should pass.
Java Build Path
and the path to your web browser driver.
Java Build Path
are set properly,
try to locate module-info.java
in your src
folder and delete it.
Java Build Path
.
Be sure to include all .jar
files provided by Selenium 4.
Try to allow it through System Preferences > Security & Security
.
For more information, please refer to
https://support.apple.com/en-us/HT202491
SessionNotCreatedException
,
it is most likely that your Selenium's browser driver does not support the version of your web browser.
Please check the version of your web browser and then re-download the Selenium's browser driver that supports the version of your web browser.
"C:\\HumptyDumpty\\uva-classes\\cs3250\\jar\\chromedriver.exe"
"/Users/HumptyDumpty/uva-classes/cs3250/jar/chromedriver"