Program without main()


						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.firefox.FirefoxDriver;

						public class TestNG1 
						{
							public static void mainsBrother() 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
							}
						}

					

You will get error because Java programs always starts at main() method but in above programme we do not have any main() method.

Let us modify the above program like below one

Program with TestNG Annotation


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

						public class TestNG1 
						{
							@Test
							public  void mainsBrother() 
							{
								WebDriver driver=new FirefoxDriver();
								driver.get("https://chercher.tech");
							}	 
						}

					

Now it will open the Firefox and then it open chercher.tech

In this case also we donot have any main method but how it works.there is main method present in TestNG class but it is not visible, the execution starts the from the @Test Method.

TestNG Annotations

Annotations were formally added to the Java language in JDK 5, and TestNG made the choice to use annotations to annotate test classes and test methods.

    @BeforeSuite
    @AfterSuite
    @BeforeTest
    @AfterTest
    @BeforeGroups
    @AfterGroups
    @BeforeClass
    @AfterClass
    @BeforeMethod
    @AfterMethod

Program with n number of TestNG Annotation

Let us look at the following program.

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

					public class TestNG1 
					{
						WebDriver driver=new FirefoxDriver();
						@Test
						public  void mainsBrother() 
						{
							driver.get("https://chercher.tech");
						}
						
						@Test
						public void mainsSister()
						{
							driver.quit();
						}
					}

					

In the above program there are 2 methods,but both executed successfully,so program can have n-number of @Test methods.

Now in the above program itself make little modification so that program executes mainSister() first.

TestNG program without Priority


						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)

So execute the following program

TestNg with Priority


						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 yahoo.com
								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 “yahoo.com᾿ 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

 
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
 
Selenium-Webdriver.Com [Group]
Facebook Group · 801 members
Join Group
Learn Selenium Webdriver Concepts and Interview Questions Learn and code
 
Copyright © CherCher Tech