[renewed at 5 Dec 2018]
IMHO Java is the most popular programming language in the Selenium project, which means, that whenever you face a Selenium problem, the probability of finding the solution in Java is the highest. So start using Selenium in Java!
There are generelly 3 approches to get started with Selenium in Java:
Maven
The official one: Setting Up a Selenium-WebDriver Project
The sophisticated one (by Mark Collin, aka Ardesco): Selenium-Maven-Template
IDE
The official one: The 5 Minute Getting Started Guide Note: Here you find an example with HtmlUnitDriver. I experienced too often, that HtmlUnitDriver wasn’t working even with correct Selenium code, so I recommand not using it at all.
Without IDE, without Maven
This approach takes the most time but is the most back-to-the-roots one and shows best how Java works under the hood.
Approach without IDE, without Maven
Some backgrounds to Java
This paragraph is only a little refreshment for your aged Java knowledge. If you never had Java knowledge, you should go for a tutorial.
If you haven’t done yet, download the Java JDK from the download page (take the most actual Java Platform (JDK)). Execute it and follow the wizard. After that, set PATH.
The Java JDK not only comprises the JRE (jre.exe) – the JRE is the environment to run java-programs – but also the Java Compiler (javac.exe). The Compiler is necessary, because you have to compile the file of your source-code (yourname.java) to become an executable file. The result of that compilation is yourname.class. The program then can be executed by java yourname
in your Command Prompt (after you navigated in the directory, where the yourname.class resides).
Selenium Setup
Download Selenium Standalone Server and put it e.g. on your desktop.
Selenium on Chrome works pretty out of the box: Download ChromeDriver, unzip it to e.g. the desktop and just run it.
If you want to run your tests on Internet Explorer, you need more time for configuration: look
Your first Selenium program
1.) Copy the following code in an editor and save it as Example.java e.g. on your Desktop:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Example { public static void main(String[] args) { // Create a new instance of the Chrome driver // Notice that the remainder of the code relies on the interface, // not the implementation. WebDriver driver = new ChromeDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); } }
2.) As every Java program, it needs to be compiled. For the Java Compiler to find your Selenium library, you have to provide it in the so-called classpath. Execute the following in your Command Prompt (of course with the correct version number):
javac -classpath "%UserProfile%\Desktop\selenium-server-standalone-3.141.59.jar" %UserProfile%\Desktop\Example.java
3.) If everything is ok, you find Example.class on your desktop. Execute the following in your Command Prompt to run your first Selenium program (of course with the correct version number):
cd "%UserProfile%\Desktop
java -classpath .;"%UserProfile%\Desktop\selenium-server-standalone-3.141.59.jar" Example
… for historical reasons the JRE needs to point also to the actual folder, that is .;
Now Chrome should write „Cheese!“ in google search and finally you should find in the Command Prompt: „Page title is: Google“. The browser-window is not closing, as you might admire the results for some time!