Home » Enterprise Java » Selenium » Selenium Automation Testing Tutorial

About Petr Arsentev

Petr Arsentev has over 8 years of experience in java development. He participated in the development a few startup projects, which run successfully. He finished Moscow Power Engineering Institute (National Research University) at 2009. After he started to work in a local company as java developer and still keeps improving the knowledge about software developments. He focused on JVM languages like Java, Scala and related technologies and frameworks. He has developed the few courses about Java in Russian. He teaches students Java language too. This is his personal website http://parsentev.ru/

Selenium Automation Testing Tutorial

1. Introduction

In this example, we shall show you how to write the automation tests by Selenium. Selenium is collections of tools to test the web applications. We are going to cover user’s cases for TrackStudio system.

Automation tests has few benefits:

  1. It is easy for supporting.
  2. It is faster than manual tests.
  3. It has possibility to repeat tests.
  4. It has the lower cost with compare to manual tests.
  5. It has ability to use in Continue Integration.
  6. It has ability to get exhausted reports.

Selenium consists from three main parts:

  1. Selenium IDE – add-ons for Firefox.
  2. Selenium WebDriver – library, which use native browser API for testing.
  3. Selenium Server – server for execute tests in different environments.

2. Create a tests plan

Before we start to write the tests code, we should write the tests plan. The plan should describe what we should do and what we will expect after those actions. Let’s tell few words about TrackStudio system. TrackStudio is issues/tasks tracker in the simple words.

It supports: workflow processing, documents processing, access rules control and so on. It fits great for our demonstration purpose, because it is the web app with rich user interfaces and it has many functional features.

Let’s imagine that we want to test all cases for creating the library (books store) configuration in TrackStudio.

Short explanationDetail explanationExpected result
Log in as administrator1. Go to index page.
2. Fill the login form.
3. Submit the form.
Redirect to main page.
Create the new rule – Library manager.4. Go to manage user tab
5. Open the rules tab.
6. Click on the create button
7. Fill the form.
8. Submit the form.
The rule should appear in rules list
Create the new rule – Reader.1. Go to manage user tab
2. Open the rules tab.
3. Click on the create button
4. Fill the form.
5. Submit the form.
The rule should appear in rules list.
Create the new User with Manager rule – Bread.1. Go to manage user tab
2. Open the users tab.
3. Click on the create button
4. Fill the form.
5. Submit the form.
The new user should appear in users list.
Create the new User with Reader rule – Smith.1. Go to manage user tab
2. Open the users tab.
3. Click on the create button
4. Fill the form.
5. Submit the form.
The new user should appear in users list.
Create the new workflow – Book1. Go to manage task tab.
2. Open the workflow tab.
3. Click on the create button.
4. Fill the form.
5. Submit the form.
The new workflow should appear in workflows list.
Create the two workflow states – in, out1. Go to manage task tab.
2. Open the workflow tab.
3. Open the status tab.
4. Click on the create button.
5. Fill the form.
6. Submit the form.
The new status should appear in workflows status list.
Create the two workflow transaction – out to in, in to out1. Go to manage task tab.
2. Open the workflow tab.
3. Open the transaction tab.
4. Selected the necessary status and click on the create button.
The new transactions should appear in the workflow.
Create the new category for Book workflow.1. Go to manage task tab.
2. Open the category tab.
3. Click on the create button.
4. Fill the form.
5. Submit the form.
The new category should appear in the categories list.
Create the new project for library.1. Go to manage task tab.
2. Click on the new project button.
3. Fill the form.
4. Submit the form.
The new project should appear in the task list.
Add the access for rule Manager and Reader to the library project1. Go to manage task tab.
2. Open the access rule tab.
3. Selected the rules.
4. Submit the form.
The new access rule should appear in the access rules list.
Login as the Bread user and create the new book.1. Log out.
2. Log in as Bread.
3. Go to the library project.
4. Click the new book button.
5. Fill the form.
6. Submit the form.
The new book should appear in the book list.
Login as the Smith user and take the new book.1. Log out.
2. Log in as Smith.
3. Go to the library project.
4. Click the new book link.
5. Click the out operation button.
6. Fill form and submit it.
The book should change the status to out.

Tests cases

Now we can go to write your tests.

3. Install the Selenium IDE

First of all we should install the Selenium IDE. You should open the Firefox.Then you should go to official Selenium web site link.

Download Selenium IDE

Download Selenium IDE

After you restart the firefox browser you can see the button in the right top corner. It is the Selenium IDE.

Selenium IDE Button

Selenium IDE Button

Then we should click on this button and Selenium IDE opens.

Selenium IDE

Selenium IDE

Now we are ready to record out test cases.

4. Record the user activities in Selenium IDE

Firstly, we should run the TrackStudio Server. You should open the FireFox and open the URL http://localhost:8080/TrackStudio/ It will look similar:

TrackStudio Login Page

TrackStudio Login Page

Now we need to record our user’s cases. Open the Selenium IDE. You have to check that Base URL is the same as TrackStudio Login page and the button record turns on.

Start to record the user activities

Start to record the user activities

Then you start to make the actions in browser as general users: click on link, fill the form and so on. Selenium IDE will records all this activities. After you finished your actions, you can see that Selenium IDE filled the table.

Login As Admin

Login As Admin

We do other tests similar actions. After that, we should get the list with our tests cases.

TrackStudio Selenium IDE Test Cases

TrackStudio Selenium IDE Test Cases

When you need to assert the text in the web page, you should select the necessary element and open the popup menu, like this:

Assert Link

Assert Link

Selenium IDE inserts there assertions in the tests code. So now we are ready to export the code to our favorite programming language from Selenium IDE. You should go to File->Export->Select language

Export

Export

5. Refactoring exported code

Now we have the source code on your tests cases. Then we should create the maven project with selenium dependency and put there your test code.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>ru</groupId>
    <artifactId>parsentev</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>
    </dependencies>
</project>

Now we can open our new project in Eclipse and make the refactoring. How you can see below the exported code does look good. It has duplicates codes.

Eclipse

Eclipse

First of all, we should extract all use data to specific file. It is the design approach – Map UI.

TrackStudioData.java

package ru.parsentev;

/**
 * File contains the trackstudio user data.
 * @author parsentev
 * @since 14.12.2015
 */
public interface TrackStudioData {
	String ROOT_LOGIN = "root";
	String ROOT_PWD = "root";
	String BREAD_LOGIN = "root";
	String BREAD_PWD = "root";
	String SMITH_LOGIN = "root";
	String SMITH_PWD = "root";
	String MANAGER_ROLE = "manager";
	String READER_ROLE = "reader";
	String CATEGORY_NAME = "book";
	String WORKFLOW_NAME = "book";
	String TASK_NAME = "the catcher in the rye";
	String TR_IN = "in";
	String TR_OUT = "out";
}

The next step is to split out main method on the few little as we described in table tests ceases.

TrackStudioCreateLibraryConfiguration.java

	@Test
	public void cases() throws Exception {
		this.loginAsAdmin();
		this.createManagerRole();
		this.createReaderRole();
		this.createUserManager();
		this.createUserReader();
		this.createWorkflow();
		this.createCategory();
		this.createTask();
		this.takeTaskByReader();
	}

We have the automation tests, which you can run the follow command mvn clean test. You can integrate it to CI easily.

6. Download the Maven project

Download

You can download the full source code of this example here: SeleniumAutomationTests.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments