Skip to content
Go back

Test Automation - Selenium WebDriver Java CLI via JShell

Published:

Introduction

In this article, I will review a new Java ability introduced from JDK 9 to use interactive shell called JShell. According to oracle official documentation:

The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line.

Tool advantages - Why should we use a CLI for testing?

Tool installation

Our minimal pom.xml for running the CLI should look something like this:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>jshell</groupId>
  <artifactId>jshell</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
    <dependency>
      <groupId>org.seleniumhq.selenium</groupId>
      <artifactId>selenium-java</artifactId>
      <version>3.141.59</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.testng/testng -->
    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.14.3</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>com.github.johnpoth</groupId>
        <artifactId>jshell-maven-plugin</artifactId>
        <version>1.1</version>
      </plugin>
    </plugins>
  </build>
</project>

In the project directory, run the following command:

mvn jshell:run

We will see the shell opening and awaiting our commands, exciting!

Exploring JShell - Writing basic selenium test cases

Now, let’s import the necessary dependencies:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.openqa.selenium.By;

In this article, I have chosen to run ChromeDriver, but you can choose any driver you prefer. Let’s open our driver:

WebDriver driver = new ChromeDriver();

No alt text provided for this image

Now, let’s run two simple test cases.

Test case #1

driver.navigate().to("http://google.com");
var title = driver.getTitle();
Assert.assertEquals(title, "test");

As we can see, we started navigating to the Google site, storing the page title as a variable and then running an assert that checks that the page title is equal to the text I expect - in this case there is no match and therefore an exception is thrown. Notice that the shell does not close although the unhandled exception, we can continue typing our commands.

No alt text provided for this image

the var keyword which used to store the title is supported from JDK 10 - it means Local Variable Type Inference, for further reading click here.

Test case #2

We will use the “getAttribute” function to extract the “src” of the Google image. This function accepts 2 parameters, the Selector and the Attribute that we want to extract. The System.out.println() statement is very useful for us when we want to print the terminal, as in the following case:

System.out.println(driver.findElement(By.cssSelector("#hplogo")).getAttribute("src"));

No alt text provided for this image

In conclusion

In this article, we reviewed the JShell REPL (Read-Eval-Print-Loop) tool, for further reading on the REPL concept here. We examined JShell advantages, installation and implemented Selenium Java API & TestNG than written two simple test cases.

REPL also exists in other development languages. Last year I published an article on WebDriverIO JavaScript REPL Interface, stay tuned for more articles :)

Happy testing!


Suggest Changes

Have a challenge? Let's Talk


Previous Post
Test Automation - Networking with Java and PowerShell
Next Post
Test Automation - Reducing The Number Of Test Classes Using The Adapter Pattern