[Selenium+java] TestNG priority in Test Cases

Source: Internet
Author: User
Tags assert xpath testng guru99

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

TestNG priority in Test Cases

ESTNG is a testing framework, which covers different types of test designs like a unit test, functional test, end to end Te St, UI test and integration test.

You can run a single or multiple test cases in your Testng code.

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

Now, while running; Lower priorities would be scheduled first.

Demo of TestNG code without priority

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

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

Note:each step which you code should is 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 ())

Codefor our scenario:

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 = ' S        Earch ']). 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 are in the initialized browser.
    • The third method (Peformseachandclick1stlink)states that perform a search in the search box (with XPath (".// *[@title = ' search '] " with a Search term as Facebook and
    • The fourth and last method (facebookpagetitleverification) states that click on the search icon of Google and verify That browser title had been changed to Facebook-google Search.

Now run this code using TestNG as shown in the video you'll find all the Test case is failing. The reason for Failure:as there are a dependency of previous test case to pass, only than current running test case would b E passed.

In the case,

    • First method which is executed is openbrowser (). It got passed because it does not has any dependency.
    • Second method executed is facebookpagetitleverification (); It is failing because we be trying to click Search button and verifying browser title.
    • You can see the IF search activity is not process and how to any other step can get passed. Hence, the reason my test cases is failing.

Passed:openbrowser

Failed:facebookpagetitleverification

Failed:launchgoogle

Failed:peformseachandclick1stlink

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

Package Com.guru.testngannotations;import Org.testng.annotations.test;public class Testng_priority_annotations {@ testpublic void C_method () {System.out.println ("I ' m in Method C");} @Testpublic void B_method () {System.out.println ("I ' m in Method B");} @Testpublic void A_method () {System.out.println ("I ' m in Method a");} @Testpublic void E_method () {System.out.println ("I ' m in Method E");} @Testpublic void D_method () {System.out.println ("I ' m in Method D");}}

Output

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 were reflected in the output as well.

Importance of priority in running TestNG methods

As you had seen in the previous example that sequencing required in order to pass this scenario, so we'll be modifying The previous piece of code with priority Parameter So, each test should run against to the priority assigned To them.

Now as you can see we had assigned the priority to each test case means test case would the lower priority value would be E xecuted 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 Firefoxdriv    ER (); }//Method 2:launch google.com @Test (priority=2) public void Launchgoogle () {Driver.get ("Http://www.go    Ogle.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-to-each-testcases, run the above code using TestNG as shown in Video-2 mentioned below.

Here, you can see that test cases is prioritized. Test case had lower priority was executed first i.e. now there was a sequential execution according to priority in the T EST cases. Hence, all test cases is passing now.

Note the console of Eclipse:

Output :

Passed:openbrowser

Passed:launchgoogle

Passed:peformsearchandclick1stlink

Passed:facebookpagetitleverification

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

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

Here we have provided the priorities as 0,1,3,6,9. So, method has 0 as priority is executed first and then method has 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 is a chance that methods could contain same priority. In those cases, TESTNG considers the alphabetical order of the method names whose priority is same.

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

Here ' e ' and ' d ' is 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 prioritized (having same priority) and Non-prioritized methods:

In this case, we'll cover the cases in one testng class.

    1. Methods has same priority value.
    2. More than one non-prioritized methods.
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 the method B I ' m in method C I ' m in method E I ' m in method A I ' m ' method D Passed:b_method passed:c_method PASSED: E_method Passed:a_method Passed:d_method

Explanation:

First preference: Non-prioritized methods: ' C ' and ' B ': Based on alphabetical order ' B ' is executed first and then ' C '.

Second Preference: Prioritized methods: ' A ', ' e ' and ' d ': ' E ' was executed first as it was had 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 ' is executed first and then ' d '.

case-sensitive in TestNG

Just for your information there are a standard syntax for defining priority in TestNG i.e. @Test (priority=4), Suppose you is defining it in some other syntax say @Test (priority=1) then your IDE would show it as a COM Pilation error. Refer image below:

Conclusion:

As you had seen that if there was a requirement to run a set of test-case in specific sequence so it can be easily done The using priority using TestNG as a run tool.

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

[Selenium+java] TestNG priority in Test Cases

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.