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?
Jshell
can be used with Selenium Java API for debugging tests, finding elements, getting to know selenium and Web automation via the Java language.- The tool provides the user with the ability to run selenium commands through the command line, which gives us the ability to run our commands line by line instead of running the entire test suite or using break point for debugging our testing scripts.
- You can navigate manually to the desired places in the browser and then run the code lines that we want. In this way, we save a lot of navigation and access that requires navigation code to reach the desired state that we want to test or inspect.
- When we receive an error from the server (for example, No such element exception / Timeout, etc) as opposed to running in a test environment where the error would cause the program to crash - using this tool, the error is printed and we can continue to run commands (which greatly optimizes the work of locating elements).
- We can also import any java library we want and use it via the CLI.
Tool installation
- Install JDK 10 or higher - for Windows users I recommend usingChocolatey.
- Install Maven.
- Create a new Java Maven project via your favorite IDE.
- Add Selenium & TestNG dependencies to the project pom.xml file.
- Add jshell-maven-plugin to the pom in the build section - this plugin gives us the ability to easily run the shell in our projects folder and import our needed dependencies from the pom file.
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();
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.
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"));
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!