DEV Community

Tony Colston
Tony Colston

Posted on

Single file Java and Selenium

One reason scripting languages are appealing to me is the feedback loop.

You can type in code and quickly execute that code without a separate compilation step. This quick movement of write-save-run is one of the many reasons I like Python.

Java now has a feature (starting Java 11) called single file execution that takes Java from a write-compile-run to a write-save-run language.

Single file execution lets you use Java as you would a scripting language! That is exciting stuff.

This post is about using this new feature to run Selenium tests with Java. To be clear Java is more complex to setup and use but if you in a shop that requires you to use Java or in an enterprise setting this is a great way to write Selenium scripts quickly.

You need at least Java 11 or later for this to work. Earlier versions of Java do not have single file execution. You will also need the Selenium Java jar file. See my links below.

import org.openqa.selenium.WebDriver; // Step 1
import org.openqa.selenium.chrome.ChromeDriver;

public class Testme {
  public static void main(String[] args) {
    // Step 2
    System.setProperty("webdriver.chrome.driver",
      "/projects/singlefilejava/chromedriver"); // chromedriver location
    WebDriver driver = null; // Step 3
    try {
      driver = new ChromeDriver();
      driver.get("https://p-p.me");
    } finally {
      if (driver!=null) {
        driver.quit();
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 1 - We have to import Selenium code. Both WebDriver and ChromeDriver are provided by Selenium. This is similar to Python so the syntax should not surprise you.

Step 2 - We have to tell Java/Selenium where chromedriver executable is located. That is what this line does.

Step 3 - Here we are declaring a variable that has a type of WebDriver. We have to declare the variable here so that the try/catch/finally statement can access the variable. If we declared the variable inside of the try statement, that variable would not be available later in the finally (search for Java scoping rules if you want to read more about this).

The rest of the program looks a lot like the Python examples I have already written. See here: https://dev.to/tonetheman/how-to-get-started-with-selenium-and-python-7p

If your file is named Testme.java and you have Java 11 you can run this example with this command:

java -cp /projects/singlefilejava/selenium-server-standalone-3.141.59.jar Testme.java
Enter fullscreen mode Exit fullscreen mode

The command line parameter -cp tells Java where to find supporting classes that are needed for your program.

Notice here you will need to have the path to the Selenium jar that is listed in the links at the bottom of this article. You can use relative or absolute pathing.

links

Article I found that started me down this path (good stuff):
https://www.infoq.com/articles/single-file-execution-java11/

Selenium jar for Java: https://www.seleniumhq.org/download/
You will need the big jar (Selenium Standalone Server). I tried this with just the driver and it felt harder/more painful.

JDKs here: https://adoptopenjdk.net/

Top comments (0)