Setting Priority of Testcases in TestNG

The testcases execution is never ordered in TestNG unless the priority is set in the code. TestNG has it’s own mechanism to set the priority of the test. There is no guarantee that the testcase on the top will be executed will be executed first and the one at bottom will be executed at last.

The following example explains it-

public class PriorityTest {

@Test

public void one()
{
System.out.println(“Inside method one”);
}

@Test

public void two()
{
System.out.println(“Inside method two”);
}

@Test

public void three()
{
System.out.println(“Inside method three”);
}

}

The output after running the above code is –

without priority

As we can see method three gets executed before two.

However TestNG has a inbuilt feature with which we can set the priority of testcases. To set the order of execution we need to set priority for each testcase in @Test annotation.

The following example explains it-


import org.testng.annotations.Test;

public class PriorityTest {

@Test(priority=1)

public void one()
{
System.out.println(“Inside method one”);
}

@Test(priority=2)

public void two()
{
System.out.println(“Inside method two”);
}

@Test(priority=3)

public void three()
{
System.out.println(“Inside method three”);
}

The output of the above code would be –

with priority

Hope it helps ūüôā

Assertions using TestNG

TestNG is a testing framework.It has many different powerful features which gives all important things required to complete the Framework. One such feature is Asserts which helps to take decision in the middle of tests.Using this we can put various checkpoints in the test script. Asserts are very popular and frequently used methods in selenium webdriver scrpits. In this article we will go through basic assert statements available to us.

1) Assert.assertTrue() & Assert.assertFalse()

Both Assert true and Assert false methods check a single variable to find out if it’s value is true/false. Test stops with assert true statement if the actual output is false.¬† Exactly reverse happens in the case of Assert false. Execution continues if the actual output is false. Here in the example if Add() method returns true the execution continues. If it returns false the execution stops.

Assert.assertTrue() Example –
import org.testng.Assert;
import org.testng.annotations.Test;

public class assertTrueTest {
    
    
    @Test
    
    public void testTrue(){
                
        Assert.assertTrue(Add(2,3)==5, "TEST FAILED");
                
                                    
    }
    private int Add(int i, int j) {
        
        return i +j;
    
    }

}
Output –
AssertTrue
Assert.assertFalse() Example –
With Assert False the excecution stops if Add() returns true.

import org.testng.Assert;
import org.testng.annotations.Test;

public class assertFalseTest {    
    
    @Test
    
    public void testFalse(){
                
        Assert.assertFalse(Add(2,3)==5, "TEST FAILED");
            
                                    
    }
    private int Add(int i, int j) {
        
        return i +j;
    
    }

}

Output –

AssertFalse

2- Assert.assertEquals() and Assert.assertNotEquals()

This assertion is used for comparing expected and actual values. In case of assert true the execution continues if expected and actual values match. But if the values mismatch it will mark specific test method as fail and exit from that test method.

Assert.assertEquals() Example –
import org.testng.Assert;
import org.testng.annotations.Test;

public class equalTest {
    
    
    @Test
    
    public void testEquals(){
        
        String Name = "Pranaya";
        
        Assert.assertEquals("Pranaya", Name);
        
                
    }
}
Output –
AssertEqual
Assert.assertNotEquals() Example –
Assert.assertNotEquals() works exactly in the opposite manner. Test execution stops if the values match.
import org.testng.Assert;
import org.testng.annotations.Test;

public class equalTest {
    
    
    @Test
    
    public void testNotEquals(){
        
        String Name = "Pranaya";
        
        Assert.assertNotEquals("Pranaya", Name);
        
                
    }
}
Output –
AssertNotEqual

3-Assert.assertNull() and Assert.assertNotNull()

The assertNull() and assertNotNull() check a single variable to find out if it’s null or not null. Assert.assertNull() methods checks that the object is null or not. The test passes if the value is null.
Assert.assertNull() Example –

import org.testng.Assert;
import org.testng.annotations.Test;

public class testNull {

    public String getPropValue(int key){
        
         HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
        
            hashMap.put(0,"value1");  
            hashMap.put(1,"value2");  
            hashMap.put(2,"value3");
            
            return hashMap.get(key);
        
    }
    
    @Test
    
    public void test(){
        
        testNull NT = new testNull();
        Assert.assertNull(NT.getPropValue(3));
        
    }   
}

Output –

TestNull

Assert.assertNotNull() Example –
Assert.assertNotNull() methods checks that the object is null or not. If it is null then it throws an AssertionError or else the test passes.
import java.util.HashMap;

import org.testng.Assert;
import org.testng.annotations.Test;

public class testNotNull {

    public String getPropValue(int key){
        
         HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
        
            hashMap.put(0,"value1");  
            hashMap.put(1,"value2");  
            hashMap.put(2,"value3");
            
            return hashMap.get(key);
        
    }
    
    @Test
    
    public void test(){
        
        testNotNull NT = new testNotNull();
        Assert.assertNotNull(NT.getPropValue(1));
        
    }
Output –
assertNotNull

Getting Started with TestNG

TestNG is a testing framework, which was inspired from JUnit and Nunit with some additional features like Annotations, support for data-driven testing, support for parameters, support for wide variety of tools, dependent methods for application server testing.

TestNG is designed to cover all kinds of tests like unit, functional, end-to-end, integration etc.

How it works?

TestNG1 (2)

The above image shows general architecture of a framework.

The green circles indicate selenium scripts which have many number of test-cases to be executed.

The yellow circles indicate the excel files from which data is read for executing the test.

The red circle indicate the test report to keep track of testcase execution results and the error messages

of failed test cases.

The purple circle indicate logs to keep track of what happened at which point of time.

To collaborate everything, so that the test cases can get data from excel sheets, a report gets generated and a log gets generated we use TestNG. Basically it works as a controlling unit to co-ordinate between testcase, data, report and log.

Both Junit and TestNG do the same job although there is a potential difference. Selenium with TestNG is more famous than selenium with Junit

  • ¬† because we get very good reports in TestNG.
  • ¬† It is easier to execute parallel testcases with TestNG.

How to Install?

TestNG2 (2)

A window as shown in image below opens

.TestNG3 (2)

Put URL http://beust.com/eclipse and click on Add button.

TestNG3 (2)

Check the TestNG checkbox in search results and click on next button.

TestNG4 (2)

This completes the installation process of TestNG.

How to check if TestNG has got properly installed?

Go to Window ‚Üí Show View ‚Üí Other

TestNG 5

On clicking other an window shown in image below opens.

TestNG6 (2)

After that expand the Java option and check if TestNG appears as a option under it.

TestNG 7

X-path functions

X-path is the most commonly used locator in selenium. since X-path is a programming Language it has many built in functions which we can use in X-path statements.

Automation using selenium is a great experience as it provides many ways to identifying an object or element on the web page. But sometime we face the problems of identifying the objects on a page which have same attributes and we can no where distinguish them.

I have listed few X-path functions below.

1-  position() РX-path function

position( ) is one of the predefined methods of X-path Language which is used in X-path Statement to locate the element     node at the specified position of the specified node type.

steps-
1- Open http://www.mindfiresolutions.com/development.htm page.
2- Open firepath and put //p[position()=1] in the input field.
3- In the image below it can be seen the first paragraph gets highlighted.

positions

2- last() – X-path function

It is one of the predefined methods of X-path Language which is used in X-path Statement to locate the last element node of the specified node type.

Steps-
1- Open http://www.mindfiresolutions.com/development.htm page.
2- Open firepath and put //p[last()] in the input field.
3- In the image below it can be seen the last paragraph gets highlighted.

last

Similarly we can modify above X-path to locate 1st/2nd/3rd element of paragraph type.

For e.g on changing the X-path to //p[last()-2] the 3rd last element gets highlighted.
last -2

3-   contains() РX-path function

contains( ) is one of the predefined methods of X-path Language which is used in X-path Statement to locate the element   node containing specified text on the page or to locate the element node     containing specific text in its attribute value.

Steps-
1- Open http://www.mindfiresolutions.com/development.htm page.
2- Open firepath
3- type //p[contains(.,’Development services are’)] in the input field.
4- It can be seen that the paragraph containing “Development services are” is getting highlighted.

contains

4- starts-with() – X-path function

starts-with( ) is one of the predefined methods of X-path Language which is used in X-path Statements to locate the element  starting with the specified text  or to locate the element node    containing an attribute value which is starting with the specified text.

Steps-
1- Open http://www.mindfiresolutions.com/development.htm page.
2- Open firepath
3- type //p[starts-with(.,’Development services are’)] in the input field.
4- It can be seen that the paragraph starting with “Development services are” is getting highlighted.

starts