Quality Testing

Quality is delighting customers

Selenium - JUnit

About Junit: 
Junit is different and Ant is different. Junit is used for different purpose and Ant is used for compiling and executing the test scripts from command prompt.

What is Junit?
Junit is an already inbuilt framework that can be used for testautomation with selenium.This junit can also be used for WhiteBox testing.Junit contains functions that are written in Java

How to build Junit Testcases(Testscripts):
First open a new java project in eclipse.
Now create a new Package in the already created new project
Now create a new java class inside the newly created Package.Each class file is a collection of testcases or a single testcase.
Associate the Junit Library with the Package.
Now create individual test case actions inside different functions inside the Java class.While creating this function use the annotation @test before the beginning of each function.
Now inside the function you can write selenium scripts (for performing actions like click,object identification with Xpath) that perform different business scenarioes of the aplication.Remember it is not mandatory for the functions to be static in nature.

Note: It is better to use the word "Test" with the name of each function inside the class because it controls the order of execution of the tests inside the class file.But this is not mandatory.

Note: You can write any number of tests(test cases) inside the Class file and when you want to ask Junit to ignore any testcase for execution you have to use the @ignore annotation to tell junit to ignore the test case. When you do so the test case will not be commented but Junit simply skips the control to the immediate next testcase in eclipse.Junit generates the end report of execution within the Junit console inside Eclipse. It is also possible to generate html reports with Junit.

How to execute the Junit testcases in Eclipse?
Since you have downloaded Eclipse for JavaEE developers you will get the Run as option as Junit test in eclipse.

It is possible to run individual test batches (single java class files) from the Junit or else you can put all the testcases(@test) into another suite(container) and then execute this suite as a Junit test inside eclipse. Always remember to execute the Suite java file if you want to do a batch execution(execution of batch of batches) using Junit.

For example: Mysuite.class is the batch of batches of test cases. if you want to execute both the batches of testcases at a time then you can create a separate container for its execution and keep this container in the same package where the individual batches (class files) are created.

package testcases
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith (Suite.class)
@SuiteClasses({ //here you can also give these in the same line.
public class Mysuite(){ //it is not mandatory to write anything in the Mysuite class as it is just a //java file and the testcases are added above into the mysuite.

Understanding Before and After annotation:
Every java file can be a single test case(testscript) or a batch of testcases. 

For example:
You take a scneario of a web based banking application where the bank employee wants to create a new sb account for a new customer.
In this case you can create a java file called Customer.java and this can be put inside a package called test cases. 
Now create java functions(@test) as Createcustomer inside the Customer java file.
You can add DeleteCustomer testcase(scenario) into the same Customer Java file.
Now you have 2 testcases inside the same batch called Customer.java file.
Now since this is a web application and the scenario cannot be executed if the browser is unavailable we can go for annotations before and after.

public class firstbatch{
public void openbrowser(){
Launch browser using selenium webdriver
@test //testcase 1 (scenario 1)

@test //testcase 2 (scenario 2)

@test //testcase 3 (scenario 3)

public void closebrowser(){
Close browser using selenium webdriver

public class secondbatch{
public void openbrowser(){
Launch browser using selenium webdriver
@test //testcase 4 (scenario 1)

@test //testcase 5 (scenario 2)

@test //testcase 6 (scenario 3)

public void closebrowser(){
Close browser using selenium webdriver

This means before executing every single test case inside the Java class file it performs launching browser action first and then the scenario gets executed. 

Before annotation and After annotation can be used to execute multiple testcases in different browser sessions separately.

Another use of Before and After annotation in testing: When you prepare the library functions in a library.java file you are bound to make a function that would open the application within a browser. When you trigger the call to this function from the actual testscript you can include the @before annotation at the beginning of the java function. Thus application will be launched in the same browser before every testscript is executed using Junit in Selenium.

How is Junit different from Selenium?
Junit is a predefined set of Java functions Selenium is not.
It is not necessary for Junit to use Selenium jars when configuring inside Eclipse.
Junit is not Java but java is used to build functions in Junit library and eclipse is an editor tool that supports junit, java, selenium jar files.

In Junit generally it is always a batch of batches that is executed as a suite(container).However it is also possible to execute single testcases(@test functions) inside a java class file. These single testcases are written inside single java class files and these tests are called as Junit tests.

Difference between @before and @BeforeClass:
@before annotation will call the function(generaly Non static) before the execution of every testscript that is present in the java class file whereas the @BeforeClass annotation calls a static function only once before the beginning of the execution of the first testscript within the Java file.

@after annotation will call the function(generaly Non static) after the execution of every testscript that is present in the java class file whereas the @AfterClass annotation calls a static function only once after the execution of all the testscripts within the Java file.

Before Class and After Class:
This annotation can be used if you want to include any important message before beginning and after ending the testscript. The condition to be taken care of for this purpose is that the function must be a static function at all times.

Using @before and @after annotation calls the function before execution of each testscript however when you want these function calls only at the beginning and at the end of the testscript when you have a single testscript then we can use the @BeforeClass and @AfterClass functions.

When using the @BeforeClass and @AfterClass the positioning of these annotations does not matter within the Java file.

Remember that every java file can have a test under the @test annotation, in this case the name of the testcase(testscript) must contain the name Test associated with it. This will control the order of execution of the testcases when you have multiple test cases inside a java file.

Annotations are part of Junit and it allows the tester to control the flow of the testscripts execution.Junit is built in Java.Since selenium uses junit and junit being built on Java whenever you use selenium with junit framework you need selenium and Java. Any imported function in junit is a java file. Hence you need to learn java so that apart from using the built in functions you can also write your own functions and use it when needed with Selenium.

The Java functions that we write for the @Before, @After,@BeforeClass,@AfterClass annotations are not testscripts but they are Java functions defined by the tester when using the Junit framework by utilizing the these built in annotations we can provide userfriendly messages when the execution of testscripts are done using a Junit Framework in Selenium.

@ignore is another annotation in junit that can be used within a Java class file when there are multiple testscripts in the Java class file in order to skip execution of that selected testscript.
However it is possible to skip a testscript within a Java class file by the use of a java function called Assume.assumeTrue();. This can contain a function name in brackets or simply as below
Assume.assumeTrue(true); However the positioning of this function must be either within the @Before java function or @BeforeClass function.

Something about Assertions in Junit:
Assertions are built in java class files that help a tester report the errors within the testscript into the Junit Failure trace console. We use assertions of Junit to compare the actual with the expected values in selenium testscripts with Junit.

Instead of writing If statements to make a comparison we can use these assertions from Junit within our Selenium testscript. The assertions indicate the line within the testscript where the error occurs and also gives the expected and actual values. 

These assertions write the errors into the console of Junit.

Disadvantage of Assertions:
Whenever we use assertions in the testscript the script lines after the assertion will execute properly when the assertion passes. However if for some reason the assertion fails then all the script lines after the failed assertion will never be executed untill the assertion becomes true.
Hence it is not so good to use the assertions directly eventhough if it is not restricted.

Solution for the above problem:
Even when an assertion fails if the tester wants to continue with execution of the script then the tester must put a try and catch block that deals with the exception and recovers from the exception automatically and continues to further execute all the testscript lines.

In the above case put the 
Public Class UnderstandingAssertions{
//inside the class but at a global level.Junit will import the necessary java classfile into eclipse.
public ErrorCollector errCollector = new ErrorCollector();
public void friends_list{
int actual=100; //captured using selenium
int expected=100; //value read from excel

// if (actual==expected) {
//} else {

//Using try and catch block for exception handling
//Using assertions instead of if statements
} catch (throwable t) {
System.out.println("error Occurred");
//java code used to report an error. To use this we need to use an annotation called
errCollector.Adderror(t); (java class file from Junit library for error reporting.)
System.out.println("moving forward");

When you execute the testscript in this case the message moving forward gets printed even if the expected value does not match the actual value. Earlier because of assertions this message would never be displayed when the assertion failed.

TTWT Magazine





© 2021   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service