Steps to Run TestNG

1.Right Click on the class

right click on class options selenium webdriver

2.Select Run As

3.Select TestNG Test

testng tests selenium webdriver

4.Program starts running, while running you can see below line like "testng-customize.xml" running

testng running selenium webdriver

5.After execution on the console you see the details like PASSED and Failed, Skipped and time taken to execute.

testng options selenium webdriver

6.On the right to console tab you can also see "Result of Running Class" on that tab you can see details like what are passed,failed or skipped tests

testng options selenium webdriver

TestNG Dependency

In some cases you might have conditions like if ""A" pass then only execute "B" using depends on methods we can execute methods in order.

Let us consider the below program when you execute this program it does the following thing
1.opens the bing.com
2.opens the google.com
3.opens the chercher.tech

TestNG without Dependency Program


					import org.openqa.selenium.WebDriver;
					import org.openqa.selenium.firefox.FirefoxDriver;
					import org.testng.annotations.Test;

					public class Dependency 
					{
						WebDriver driver=new FirefoxDriver();
						@Test
						public void google()
						{
							driver.get("http://google.com");
						}
						
						@Test
						public void seleniumWebdriver()
						{
							driver.navigate().to("https://chercher.tech");
						}
						@Test
						public void bing()
						{
							driver.navigate().to("http://bing.com");
						}
					}

					

But I want to execute the methods like below order.
1. Open the chercher.tech
2. Open the bing.com
3. Open the google.com

TestNG program with Dependency


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.Test;

						public class Dependency 
						{
							WebDriver driver=new FirefoxDriver();
							@Test(dependsOnMethods="bing")
							public void google()
							{
								driver.get("http://google.com");
							}
							
							@Test
							public void seleniumWebdriver()
							{
								driver.navigate().to("https://chercher.tech");
							}
							@Test(dependsOnMethods="seleniumWebdriver")
							public void bing()
							{
								driver.navigate().to("http://bing.com");
							}
						}
					

By executing below scenario you can understand the concept ,In the below Program second (bing) method fails let see whether 3rd (google)method executes or not.To fail second method we wantedly given driver.get(“bing.com᾿)

TestNG with Dependency


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.Test;

						public class Dependency 
						{
							WebDriver driver=new FirefoxDriver();
							@Test(dependsOnMethods="bing")
							public void google()
							{
								driver.get("http://google.com");
							}
							
							@Test
							public void seleniumWebdriver()
							{
								driver.navigate().to("https://chercher.tech");
							}
							@Test(dependsOnMethods="seleniumWebdriver")
							public void bing()
							{
								driver.navigate().to("bing.com");
							}
						}
					

Dependency on Multiple methods
In below program the bing methods will be executed only if yahoo and google methods are executed and passed.

TestNG with Dependency


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.Test;

						public class Dependency 
						{
							WebDriver driver=new FirefoxDriver();
							@Test
							public void google()
							{
								driver.get("http://google.com");
							}
							
							@Test
							public void seleniumWebdriver()
							{
								driver.navigate().to("https://chercher.tech");
							}
							@Test(dependsOnMethods={"seleniumWebdriver","google")
							public void bing()
							{
								driver.navigate().to("bing.com");
							}
						}
					

TestNG Execution without Order


							public class TestNG1 
							{
								//open the firefox
								WebDriver driver=new FirefoxDriver();
								
								@Test
								public void mainsSister()
								{
									//opens the chercher.tech
									driver.get("https://chercher.tech");
									
								}
								
								@Test
								public  void mainsBrother() 
								{
									//opens the google.com
									driver.get("http://google.com");
								}	
							}
					

Look at the result: program opens the firefox and goes to Google.com first then only it navigates to “chercher.tech᾿ website but we have written the in correct order but why it is not following the order.

It is simple TestNG executes the methods as per alphabets only not as per the sequence you write.because of this reason only the mainsBrother() got executed first even though it is in last.

Let us consider this if a scenario has more than 100 methods .Is all the methods in scenario follows alphabet order,No,not at all.

Let’s see how to overcome this problem.
Priority is numeric integer(negative also) constant value,we cannot use a variables in place of priority

Syntax:@Test(priority=2)
Syntax :@Test(priority=-2)
Wrong :@Test(priority=2.2)


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.Test;

						public class TestNG1 
						{
							//open the firefox
							WebDriver driver=new FirefoxDriver();
							
							@Test(priority=-2)
							public void mainsSister()
							{
								//open chercher.tech
								driver.get("https://chercher.tech");
								
							}
							
							@Test(priority=2)
							public  void mainsBrother() 
							{
								//opens the google.com
								driver.get("http://google.com");
							}
							
							@Test
							public void mainsFather()
							{
								//open chercher.tech
								driver.get("http://yahoo.com");
							}
						}
					

If you are able to notice thing in the above program ,it open “chercher.tech᾿ first and then it opens the “chercher.tech᾿ after that only it open the “google.com᾿ .why so we didnot give any priority to mainsFather() method but how it executed in the second place.

Specifying priority is not mandatory,if we do not assign any priority then TestNG gives default priority as ‘0’(zero).

Below one is the order of priority
-2
-1
0
1
2
3
If the priority is duplicate then methods will be executed in alphabet order

Ordering Execution with TestNG Using Dependency

We can also order an execution by adding dependency between the methods.


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;
						import org.testng.annotations.Test;

						public class Dependency 
						{
							WebDriver driver=new FirefoxDriver();
							@Test
							public void google()
							{
								driver.get("http://google.com");
							}
							
							@Test
							public void seleniumWebdriver()
							{
								driver.navigate().to("https://chercher.tech");
							}
							@Test(dependsOnMethods={"yahoo","google")
							public void bing()
							{
								driver.navigate().to("bing.com");
							}
						}
					

In above scenario "seleniumWebdriver" methods will be executed first the "google" then "bing

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Ram Bhai
    maintain the sequence of topic
    Reply
    • karthiQ [ admin]
      Hi Ram,
      
      thanks for highlighting the issue,we will correct it.
      Reply
  •  
    Selenium-Webdriver.Com [Group]
    Facebook Group · 801 members
    Join Group
    Learn Selenium Webdriver Concepts and Interview Questions Learn and code
     
    Copyright © CherCher Tech