While step is capable of much more than just automating browsers, this is a popular use of the platform and an good way to get your feet in the water. We’ll use Selenium, Chrome and ChromeDriver as our toolkit of choice here, but keep in mind that you could choose to do the exact same thing using other browsers or even a different stack such as step’s Node.JS agent along with Pupeteer to interact with Chrome.
Eclipse, IntelliJ or any other Maven-enabled Java IDE installed
For interacting with Chrome, a compatible version of ChromeDriver must be installed. Please check your local version of Chrome and refer to the following page in order to download the right driver version. As you can see from the following figure, since v73, a new version of the driver is provided with each major release:
Make sure to unzip the chromedriver binary into a proper installation folder and add this folder to the PATH. If you cannot add it to the PATH, remember the path to the chromedriver binary as we’ll need to set it in our project explicitly.
Project initialization with Git
If you’re already familiar with tools such as Git, Maven, JUnit and Eclipse, you can go ahead and clone our samples project right away:
The samples discussed in this guide are contained in the maven project demo-selenium-keyword located in the folder step-samples\keywords\java.
Creating a maven project
If you’re interesting in a step-by-step initialization of a step project with Eclipse and Maven, this section will be relevant, otherwise just skip to the description of the keyword’s code.
You can download Eclipse here which should be shipped by default with Maven. If you’re already familiar with Maven, just move on to the next sub-section.
Click File -> New -> Other… -> Choose Maven Project from the list
Check the Create a simple artefact checkbox then click Next
Enter ch.exense.step.examples as a Group Id and demo-selenium-keyword as Artefact Id then click Next
A project will appear in your Project Explorer, along with several sub-folders and a pom.xml file in which dependencies and other project properties will be managed.
Make sure that JDK 8 is installed as your default Java environment (Windows -> Preferences -> Java -> Installed JREs) and that your project is configured to use it (Right click on project -> Properties -> Java Build Path -> Libraries).
For convenience purposes, we’ve combined both step’s dependencies as well as Selenium into a single dependency which you can use in your project. Just add the following lines to the maven project’s pom.xml:
Step supports different packaging alternatives. Here, we will opt to package our keyword code as an uber jar via maven’s share plugin by adding the following snippet to our maven project’s pom.xml file:
Let’s now create a class called ChromeKeywords in the src/main/java folder of our project. This class will contain our automation code. In order to comply with step’s API and benefit from its services and features, in Java, Keyword classes will need to satisfy a couple of requirements:
In order to communicate with step, the class will extend the AbstractKeyword superclass.
Each method defining a keyword will have to be annotated with the annotation Keyword, will use no arguments and will be of return type void
Let’s look at a simple Selenium code snippet for starting chrome and navigating to a URL, and the result once integrated as a step Keyword:
Note: if you haven’t add the chromedriver to your PATH you have to uncomment the first line and set the path to the chromedriver binary explicitly using the System property webdriver.chrome.driver.
In a separate class called ChromeKeywordsTest, we will use JUnit to invoke our Keyword via step’s context, run it locally and build a basic test case. But first, let’s add the following dependency to our pom.xml in order to have access to JUnit’s functionality:
If you run the unit test mapped to the method createAndNavigateTest (by right clicking the method’s name and clicking Run As -> JUnit Test ), you should see an instance of Chrome start, take you to the exense.ch front page and quickly navigate to our Consulting page.
The final state of the spawned browser instance should look approximately like the screenshot above.
Building the uber jar
You can now build your keyword into a jar file containing all of its dependencies (Right click the pom.xml file -> Run As -> Maven build … -> Entering package as a Goal -> Checking the box Skip Tests -> Run)
The output will contain the explicit path to your uber jar file and should look like this:
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing C:\git\step-samples\keywords\java\demo-selenium-keyword\target\demo-selenium-keyword-0.0.1-SNAPSHOT.jar with
[INFO] Dependency-reduced POM written at: C:\git\step-samples\keywords\java\demo-selenium-keyword\dependency-reduced-pom.xml
[INFO] BUILD SUCCESS
[INFO] Total time: 26.724 s
[INFO] Finished at: 2019-05-16T13:05:12+02:00
Registering the keyword
In the step UI go to Keywords
Click New keyword
Fill in the form as follow
Drag & drop the uber jar demo-selenium-keyword-0.0.1-SNAPSHOT.jar generated by maven to the field Jar file
Make sure to use the exact same spelling in the name of the keyword as the one used when naming the annotated method in your keyword class.
Running the keyword on an agent
If you now click the play button of the corresponding entry in the Keyword list view,
which takes you to an interactive execution lab, and then click play,
choose the default TEST environment and click play one last time,
your keyword will be executed, but this time, instead of being invoked directly from eclipse, its code will be shipped dynamically to the agent and started from there. As a result of the execution, you’ll automatically be taken to the execution view in which important information such as functional outputs or performance data will be reported.
Let’s move on to the next section to see how to make this type of information available and start getting move value out of the platform. We will run a distributed plan involving our keyword, perform functional validation against the results of our automation scenario as well as measure the latency of our web page.