Você está na página 1de 13

12/13/2018 TestNG @Test Priority in Selenium

(https://www.guru99.com/)

Home (/) Testing

SAP Web Must Learn! Big Data

Live Projects AI Blog (/blog/)

TestNG @Test Priority in Selenium


TestNG is a Testing (/software-testing.html)framework, that covers different types of test
designs like a unit test, functional test, end to end test, UI test and integration test.

You can run a single or multiple test cases in your Testng (/all-about-testng-and-
selenium.html)code.

If test priority is not defined while, running multiple test cases, TestNG assigns all @Test a
priority as zero(0).

Now, while running; lower priorities will be scheduled first.

In this tutorial, you will learn -

Demo of TestNG code without Priority


Demo of TestNG code without Priority in Alphabetical Order
How to set Priority in TestNG
Methods with Same Priority
Combining both prioritized(having same priority) and non-prioritized methods

Demo of TestNG code without Priority


Let's take a scenario where sequencing will be required in order to pass all test cases:

Scenario: Generate a code where you are required to perform a Google search with a
specific keyword say "Facebook". Now, verify that Browser title is changed to "Facebook -
Google Search".

Note: Each step which you code should be in separate methods

Method 1: Open Browser say Firefox (openBrowser())

Method 2: Launch Google.com (launchGoogle())

Method 3: Perform a search using "Facebook" (performSearchAndClick1stLink())

Method 4: Verify Google search page title (FaceBookPageTitleVerification())


https://www.guru99.com/test-case-priority-testng.html 1/13
Code
12/13/2018 for our scenario: TestNG @Test Priority in Selenium

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

public class Priority_In_testNG {


WebDriver driver;

// Method 1: Open Brower say Firefox


@Test
public void openBrowser() {
driver = new FirefoxDriver();
}

// Method 2: Launch Google.com


@Test
public void launchGoogle() {
driver.get("http://www.google.co.in");

// Method 3: Perform a search using "Facebook"


@Test
public void peformSeachAndClick1stLink() {
driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("Facebook");

// Method 4: Verify Google search page title.


@Test
public void FaceBookPageTitleVerification() throws Exception {

driver.findElement(By.xpath(".//*[@value='Search']")).click();

Thread.sleep(3000);
Assert.assertEquals(driver.getTitle().contains("Facebook - Google Search"),
true);
}
}

Explanation of Code

As mentioned above we have created 4 test cases for performing each action in an
independent methods.

The first method (openBrowser) states to initialize Firefox browser.


The second method (launchGoogle) states that launch Google.com is in the initialized
browser.
https://www.guru99.com/test-case-priority-testng.html 2/13
The
12/13/2018 third method (peformSeachAndClick1stLink)states that perform a search in the
TestNG @Test Priority in Selenium

search box (with xpath (".//*[@title='Search']") with a search term as Facebook and
The fourth and last method (FaceBookPageTitleVerification) states that click on search
icon of Google and verify that browser title has been changed to Facebook - Google
Search.

Now run this code using testNG as shown in the video you will find all the Test Case (/test-
case.html)are failing. The reason for failure: as there is a dependency of previous test case
to pass, only than current running test case will be passed.

In this case,

First method which is executed is openBrowser(). It got passed because it does not
have any dependency.
Second method executed is FaceBookPageTitleVerification(); it is failing because we
are trying to click search button and verifying browser title.
You can see that if search activity is not process then how any other step can get passed.
Hence, this is the reason my test cases are failing.

PASSED: openBrowser
FAILED: FaceBookPageTitleVerification
FAILED: launchGoogle
FAILED: peformSeachAndClick1stLink

TestNG Without Priority

Demo of TestNG code without Priority in Alphabe cal Order


If we don’t mention any priority, testng will execute the @Test methods based on
alphabetical order of their method names irrespective of their place of implementation in the
code.

https://www.guru99.com/test-case-priority-testng.html 3/13
12/13/2018 TestNG @Test Priority in Selenium
package com.guru.testngannotations;

import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

@Test
public void c_method(){
System.out.println("I'm in method C");
}
@Test
public void b_method(){
System.out.println("I'm in method B");
}
@Test
public void a_method(){
System.out.println("I'm in method A");
}
@Test
public void e_method(){
System.out.println("I'm in method E");
}
@Test
public void d_method(){
System.out.println("I'm in method D");
}

Output

I'm in method A
I'm in method B
I'm in method C
I'm in method D
I'm in method E

Though we defined the methods in a random manner (c, b, a, e, d), testng executed the
methods based on their method names by considering alphabetical order and the same was
reflected in the output as well.

How to set Priority in TestNG


As you have seen in the previous example that sequencing required in order to pass this
scenario, so we will be modifying the previous piece of code with Priority Parameter so that
each test should run against to the priority assigned to them.

https://www.guru99.com/test-case-priority-testng.html 4/13
Now as
12/13/2018 you can see we have assigned the Priority
TestNG @Test to each
Priority test case means test case will
in Selenium

the lower priority value will be executed first.

Priority in testNG in action

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

public class Priority_In_testNG {


WebDriver driver;

// Method 1: Open Browser say Firefox


@Test (priority=1)
public void openBrowser() {
driver = new FirefoxDriver();
}

// Method 2: Launch Google.com


@Test (priority=2)
public void launchGoogle() {
driver.get("http://www.google.co.in");
}

// Method 3: Perform a search using "Facebook"


@Test (priority=3)
public void peformSeachAndClick1stLink() {
driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("Facebook");

// Method 4: Verify Google search page title.


@Test (priority=4)
public void FaceBookPageTitleVerification() throws Exception {

driver.findElement(By.xpath(".//*[@value='Search']")).click();

Thread.sleep(3000);
Assert.assertEquals(driver.getTitle().contains("Facebook - Google Search"), true);

}
}

Explanation of Code

After assigning priority to each testcases, run the above code using testNG as shown in
Video-2 mentioned below.

https://www.guru99.com/test-case-priority-testng.html 5/13
Here,you can see that test cases are prioritized.
12/13/2018 Test
TestNG @Test case
Priority having lower priority are
in Selenium

executed first i.e. now there is a sequential execution according to priority in the test cases.
Hence, all test cases are passing now.

Note the console of eclipse:

Output :

PASSED: openBrowser
PASSED: launchGoogle
PASSED: peformSearchAndClick1stLink
PASSED: FaceBookPageTitleVerification

TestNG With Priority

Number 0 has the highest priority(it’ll be executed first) and the priority goes on based on the
given number i.e., 0 has the highest priority than 1. 1 has the highest priority than 2 and so
on.

https://www.guru99.com/test-case-priority-testng.html 6/13
12/13/2018 TestNG @Test Priority in Selenium
package com.guru.testngannotations;
import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

@Test(priority=6)
public void c_method(){
System.out.println("I'm in method C");
}
@Test(priority=9)
public void b_method(){
System.out.println("I'm in method B");
}
@Test(priority=1)
public void a_method(){
System.out.println("I'm in method A");
}
@Test(priority=0)
public void e_method(){
System.out.println("I'm in method E");
}
@Test(priority=3)
public void d_method(){
System.out.println("I'm in method D");
}

Output

I'm in method E
I'm in method A
I'm in method D
I'm in method C
I'm in method B

Here we have provided the priorities as 0,1,3,6,9. So, method having 0 as priority is
executed first and then method having priority-1 and so on. Here alphabetical order method
name won’t be considered as we provided the priorities

Methods with Same Priority:


There may be a chance that methods may contain same priority. In those cases, testng
considers the alphabetical order of the method names whose priority is same.

https://www.guru99.com/test-case-priority-testng.html 7/13
12/13/2018 TestNG @Test Priority in Selenium
package com.guru.testngannotations;
import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

@Test(priority=6)
public void c_method(){
System.out.println("I'm in method C");
}
@Test(priority=9)
public void b_method(){
System.out.println("I'm in method B");
}
@Test(priority=6)
public void a_method(){
System.out.println("I'm in method A");
}
@Test(priority=0)
public void e_method(){
System.out.println("I'm in method E");
}
@Test(priority=3)
public void d_method(){
System.out.println("I'm in method D");
}

Output

I'm in method E
I'm in method D
I'm in method A
I'm in method C
I'm in method B

Here ‘e’ and ‘d’ are executed based on their priority values. But the methods ‘a’ and ‘c’
contains the same priority value(6). So, here testng considers the alphabetical order of ‘a’
and ’c’ and executes them accordingly.

Combining both priori zed(having same priority) and non-


priori zed methods:
In this case, we’ll cover two cases in one testng class.

1. Methods having same priority value.


https://www.guru99.com/test-case-priority-testng.html 8/13
2. More
12/13/2018 than one non-prioritized methods.TestNG @Test Priority in Selenium

package com.guru.testngannotations;

import org.testng.annotations.Test;

public class TestNG_Priority_Annotations {

@Test()
public void c_method(){
System.out.println("I'm in method C");
}
@Test()
public void b_method(){
System.out.println("I'm in method B");
}
@Test(priority=6)
public void a_method(){
System.out.println("I'm in method A");
}
@Test(priority=0)
public void e_method(){
System.out.println("I'm in method E");
}
@Test(priority=6)
public void d_method(){
System.out.println("I'm in method D");
}
}

Output:

I'm in method B
I'm in method C
I'm in method E
I'm in method A
I'm in method D

PASSED: b_method
PASSED: c_method
PASSED: e_method
PASSED: a_method
PASSED: d_method

Explanation:

https://www.guru99.com/test-case-priority-testng.html 9/13
Firstpreference: Non-prioritized methods:
12/13/2018 ‘c’ and
TestNG @Test‘b’: Based
Priority on alphabetical order ‘b’ was
in Selenium

executed first and then ‘c’.

Second preference: Prioritized methods: ‘a’, ‘e’ and ‘d’: ‘e’ was executed first as it was
having highest priority(0). As the priority of ‘a’ and ‘d’ methods were same, testng considered
the alphabetical order of their methods names. So, between them, ‘a’ was executed first and
then ‘d’.

Case-sensitive in TestNG

Just for your information there is a standard syntax for defining priority in testNG i.e. @Test
(priority=4), suppose you are defining it in some other syntax say @Test (PRIORITY=1)
then your IDE will show it as a compilation error. Refer image below:

(/images/Sap-QM/Test_case_priority_TestNG.jpg)

Conclusion:

As you have seen that if there is a requirement to run a set of test-case in specific sequence
then it can be easily done using Priority using testNG as a run tool.

This tutorial is made possible due to contributions of Ramandeep Singh and Rama
Krishna Gadde

 Prev (/intellij-selenium-webdriver.html) Report a Bug

Next  (/testng-execute-multiple-test-suites.html)

YOU MIGHT LIKE:

SELENIUM
https://www.guru99.com/test-case-priority-testng.htmlSELENIUM SELENIUM 10/13
12/13/2018 TestNG @Test Priority in Selenium
(/selenium-python.html) (/xslt-report- (/sikuli-tutorial.html)
(/selenium- selenium.html) (/sikuli-tutorial.html)
python.html) (/xslt-report-
File Upload using Sikuli in
selenium.html) Selenium Webdriver
How to Use Selenium with
Python: Complete Tutorial XSLT Report in Selenium (/sikuli-tutorial.html)
(/selenium-python.html) (/xslt-report-selenium.html)

SELENIUM SELENIUM SELENIUM

(/drag-drop-selenium.html) (/using-soapui- (/desired-capabilities-


(/drag-drop- selenium.html) selenium.html)
selenium.html) (/using-soapui- (/desired-
selenium.html) capabilities-
Drag and Drop ac on in
Selenium: dragAndDrop, Using SoapUI with Selenium selenium.html)
dragAndDropBy (/using-soapui- Desired Capabili es in
(/drag-drop-selenium.html) selenium.html) Selenium WebDriver
(/desired-capabilities-
selenium.html)

Selenium Tutorials
42) SSL Certificate Error Handling (/ssl-certificate-error-handling-selenium.html)

43) Handling Ajax call (/handling-ajax-call-selenium-webdriver.html)

45) Execute JavaScript based code (/execute-javascript-selenium-webdriver.html)

46) Using Selenium with Python (/selenium-python.html)

47) Use intelliJ & Selenium (/intellij-selenium-webdriver.html)

52) Flash Testing with Selenium (/flash-testing-selenium.html)

54) Core Extensions (/selenium-core-extensions.html)

55) Using Apache Ant with Selenium (/using-apache-ant-with-selenium.html)

56) Using Selenium with Github (/selenium-github.html)

57) Handling Cookies (/handling-cookies-selenium-webdriver.html)

58) Using SoapUI with Selenium (/using-soapui-selenium.html)

59) XSLT Report in Selenium (/xslt-report-selenium.html)

60) Firefox Profile (/firefox-profile-selenium-webdriver.html)

61) Breakpoints and Startpoints (/breakpoints-startpoints-selenium.html)

62) Selenium Interview Questions (/top-100-selenium-interview-questions-answers.html)

63) Cucumber Selenium (/using-cucumber-selenium.html)


https://www.guru99.com/test-case-priority-testng.html 11/13
12/13/2018 TestNG @Test Priority in Selenium
64) Drag & Drop Selenium (/drag-drop-selenium.html)

65) Selenium C# Webdriver (/selenium-csharp-tutorial.html)

66) Creating Object Repository (/object-repository-selenium.html)

67) Scroll UP or Down a page (/scroll-up-down-selenium-webdriver.html)

68) Sikuli Tutorial (/sikuli-tutorial.html)

71) Selenium vs HP UFT (QTP) (/alm-qtp-selenium-difference.html)

72) Selenium Alternatives (/selenium-alternatives.html)

 (https://www.facebook.com/guru99com/) 
(https://twitter.com/guru99com) 
(https://www.youtube.com/channel/UC19i1XD6k88KqHlET8atqFQ)

(https://forms.aweber.com/form/46/724807646.htm)

About
About US (/about-us.html)
Advertise with Us (/advertise-us.html)
Write For Us (/become-an-instructor.html)
Contact US (/contact-us.html)

Career Sugges on
SAP Career Suggestion Tool (/best-sap-module.html)
Software Testing as a Career (/software-testing-career-
complete-guide.html)
Certificates (/certificate-it-professional.html)

Interes ng
Books to Read! (/books.html)
Suggest a Tutorial
Blog (/blog/)
Quiz (/tests.html)
Review (/best-ergonomic-mouse.html)

Execute online
Execute Java Online (/try-java-editor.html)
Execute Javascript (/execute-javascript-online.html)
https://www.guru99.com/test-case-priority-testng.html 12/13
12/13/2018 Execute HTML (/execute-html-online.html)
TestNG @Test Priority in Selenium

Execute Python (/execute-python-online.html)

© Copyright - Guru99 2018


Privacy Policy (/privacy-policy.html)

https://www.guru99.com/test-case-priority-testng.html 13/13

Você também pode gostar