A Basic Test Script In Java

We pick up right where we left off which is now to incorporate our first basic Selenium test script. This script will be a very simple script that will open a webpage. As we get further on into developing our tests, we will adopt best practices such as using an execution framework (J-Unit) assertions libraries (HamCrest) with the addition of using the modelling technique (Page Object Modelling) this will incorporate typically good programming practices such as DRY and SOLID.

In future blog posts we will also start to perform actions on the website, then put together an end to end framework. Let’s open our IDE IntelliJ and click on new project you should see the following window screen displaying below.

Ensure to select Maven as the option and click on the next button. There will be a further screen which will allow us to enter our project name. I’m going to call this project automationodyssey. You are free to call the project a meaningful name.

Once done click on the finish button you will see a project screen that will have the following folder structure .idea, src \main\java, src\main\resources, src\test\java. This is a typical Maven folder structure, this has been created for us by Maven. I will be going into further detail about Maven in a future blog post.

In order for us to start using Selenium we need to add Selenium-Java as a dependency to our project. Adding a dependency allows us to use Selenium-Java library. We do this by adding the Selenium XML to the pom.xml file. The pom.xml file will handle all other project dependencies we may need in future.

Handling our dependencies in this manner allows us to update versions and fixes to dependencies should they require change. When I first started out in test automation I downloaded .jar files and added these to the external libs folder.

This became an inefficient way as you downloaded a new .jar file for every version update, then delete your old .jar file. Rather now the site https://mvnrepository.com/ hosts the files and XML codings, it’s a small matter of making the changes direct to the pom.xml file and updating information such as the version number.

In the pom.xml file place the following line of code under the version tag as show below.

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>

Now under the src\test\java folder right click under the new option select add Java class and name that class as BasicTest. A Java class is where we write and house our code. Typically in a Java class we house things such as fields, constructors, methods and nested classes. As we progress with our tests we will see these building blocks being housed in our Java classes.

Now in the Java file we are going to create our first field which will be the WebDriver field and assign it to a variable called ‘driver’ as shown below. *Note* if you see the class name WebDriver highlighted in red this is a warning to us to import the WebDriver class into our project file.

You can fix this issue simply by right clicking on the project name then in the Maven option reimporting the project. You will then need to head back over to the Java file and under the field placing the cursor over the declared WebDriver field and selecting the class to import as shown below.

In Java we need explicitly state the entry point for our program.We do this by adding the following line of code to our Java class file, public static void main (String [] args) { } . I won’t go into detail regarding that line of code, we can further explore this in another post. Inside of the curly braces of public static void main (String [] args) { } add the following lines of code as below.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BasicTest {

   static WebDriver driver;

   public static void main (String[] args){
     
System.setProperty("webdriver.chrome.driver","src/main/resources/chrome-driver");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
      driver.navigate().to("https://the-internet.herokuapp.com/login");
   }


}

Looking at the lines of code what we have done is to set the system property this allows Selenium to use ChromeDriver a standalone server which will let our code run against a Chrome browser. I will also add the ChromeDriver standalone server in the resources folder you will need to add the Chrome.exe file.

This file can be downloaded at the following site https://chromedriver.chromium.org/home. Ensure that you are downloading the version that matches your current Chrome browser version. The version can be located under the Chrome about option.

Now in the test file right click and select Run BasicTest() main to see Selenium in action.

If you have followed all of the steps correctly you would have seen a browser spin up and the following page displayed below. Congratulations you have successfully ran your first Selenium test script!

In the next post will further build on interacting with the page and using the runner framework J-Unit. Unit then take care and thanks for reading this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s