Writing a basic Selenium test

In this post we will continue to build on our Maven project to create a simple test in Selenium.

You don’t have to have a Maven project in order to write a selenium test. You can create a normal Java project and import the necessary jar files. You can read the post “Setting up Selenium WebDriver” to see how to do this or you can set up the project using Maven – “Setting up Selenium WebDriver using Maven“.

Open your IDE and expand the all the folders for your project. You should have a src folder; this is where your code goes. As we used the Maven quickstart the folder structure is also set up for us to an extent.

FolderStructure
Maven folder structure

Delete the AppTest class which is inside the test/java/uk.co.khaleghi folder, then right click on the uk.co.khaleghi folder and select New > Java class. I have created a new class called KhaleghiWebsite.

NewClass
Create a new class

Open the class which was just created and paste the following code into your class. Please note if you chose a different GroupId than the one I entered (uk.co.khaleghi), you will need to change the “package” to your own relevant one. Also depending on what you named your class (I named mine KhaleghiWebsite), you will need to change the line which says “public class KhaleghiWebsite” to “public class xyz”, where xyz is the name of your class.

package uk.co.khaleghi;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

//Start of class
public class KhaleghiWebsite { 

    //This is a junit annotation which tells Selenium what is a test and what needs to be run
    @Test

    //Start of test which is called testContactPage
    public void testContactPage() {

        //Open a Firefox browser
        WebDriver driver = new FirefoxDriver();

        //Enters http://www.khaleghi.co.uk into address bar.
        driver.get("http://www.khaleghi.co.uk");

        //Finds the element on the screen which has the link Contact and assigns contact_button to it. Within the test if we can refer to this element by calling contact_button.
        WebElement contact_button = driver.findElement(By.linkText("Contact"));

        //Gets contact_button from the step above and click on it.
        contact_button.click();

        //Finds the element on the screen which has the id "name" and assigns name_field to it. Within the test if we can refer to this element by calling name_field.
        WebElement name_field = driver.findElement(By.id("name"));

        //Gets name_field from the step above and enter the text Hadi.
        name_field.sendKeys("Hadi");
    
    //End of test    
    }

//End of class
}

I have commented out the code so you can see what is happening line by line. If you have followed everything correctly you should not be getting any errors as we have imported all the relevant classes. If you do see red text on the screen hover over it and it will give you suggestions as to what the error is.

Right click on the class and click on Run ‘KhaleghiWebsite’, or use the shortcut keys Ctrl + Shift + F10. The test should now start to run. After a few seconds you will notice a Firefox window opening up and everything we have written above being performed.

RunTest
Running the selenium test

 

Note: If you are having trouble running the test this may be due to the Firefox browser being updated. You will need to update the selenium files either in the projects pom.xml (if using Maven) or the jar file if you are importing the files manually.

One comment:

Leave a Reply