Software Testing Learning Hub

Selenium Lesson 6 – Write Selenium Script from Scratch (Junit)

Lesson 6 - run script from scratch

In this Lesson we will create selenium script from scratch in eclipse. many times it found that automation engineer struggles with writing script from scratch. it is mainly happen because either you are new to selenium or you are using existing automation framework in which initiation and closure part of script is already defined.

Automation Script mainly has Three Basic blocks

  1. Initiation : In Initiation, we usually write code which we need to execute before tests, mostly prerequisite code such as defining the webdriver, initiating the webdriver, opening the web browser, environmental setups, reading files etc. without these prerequisite we cannot able to run our test
  2. Test : In Test case, we write the test code
  3. Cleanup : In clean up  code, we usually perform reset activities, such, closing the webdriver, closing the browser, closing the files/connection where we are writing the results. Apart from it, we write the reset the test data condition for test case, so that we can use same data again for next execution, it includes, modification in DB, or by calling other functions, reset the condition of the application under test.

Before we jump into writing code, we should know about the execution framework. in this lesson we are going to use JUNIT.

why we should know the Junit?

this is because, style in which we will write the code is tightly bound with execution framework. as per the execution framework we have divide the script in different parts also we have to use different annotations of the execution framework while writing our script.

 

Important points about Junit.

we will have separate lesson on Basis of Junit. for this lesson we need to know about following important concept of Junit.

1) we will use Junit to structure and execute our selenium scripts

2) we will use Junit’s Annotations to structured our script ( Annotations such as, @before @test, @after etc)

 

Lets starts!

before we jump on the code, we have to create the Junit Test Case:

Step 1: Open Eclipse >  Load the work-space in which you want to run your script

Step 2: Open ( created in Lesson no 2) or create new Java project ( if you you are creating new project, you have import the selenium Jar file as external Jar in your project. see the lesson no 2 last section for it.

Step 3: Go to File Menu >  New > Other > Wizard will open, Type “Junit Test Case” >  select Junit Test Case

File will look like this.

package selenium;  // this is my package name where i have created this test case
import static org.junit.Assert.*;
import org.junit.Test;
public class test {
@Test
 public void test() {
 fail("Not yet implemented");
 }
}

Step 4:  Remove the @Test code, now your file will look like this

package selenium;  // this is my package name where i have created this test case
import static org.junit.Assert.*;
import org.junit.Test;
public class test {
}

Step5: Add Selenium References and define Variables under class so that it will be access in all methods inside the class

package Selenium;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class test {
private WebDriver driver;
private String baseUrl;
}

Now our base structure is ready. we will write the script. We will divide the script in 3 different sections

  1. Initiation: Selenium Driver initiation code
  2. Test:  Test Case Code
  3. Cleanup: Clean up code

Section 1:  Initiation: Selenium Driver initiation code

Considering you are beginner, I will keep this section simple. In this section, we will define the Selenium webdriver and setup the default implicit wait.

@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://qeworks.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

 

Section 2:  Test:  Test Case Code

In this section, we will write a code. here we are opening the url and doing search with keyword “Software Testing”

@Test
public void testRecording() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.linkText("Software Testing")).click();
}

Section 3:  Cleanup: Clean up code . here we are closing the webdriver

@After
public void tearDown() throws Exception {
driver.quit();
}

Now put the above sections one below other in our Script. and code will look like this.

package Selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class test {
private WebDriver driver;
private String baseUrl;
@Before   // Section 1
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://qeworks.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test  // Section 2
public void testRecording() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.linkText("Software Testing")).click();
}
@After  // Section 3
public void tearDown() throws Exception {
driver.quit();
}
}

Execute the Test case!

You are Done!!!

i hope this lesson will help, you write the code from scratch. if you face any difficulty, do not hesitate to contact us.

Happy Selenium Learning!