In this page, I will walk you through TestNG Tutorials which will help you to write efficient scripting in Selenium Webdriver.Most of the guys you have heard TestNG ,Junit,Nunit and you might be having so much curiosity about it.

 

TestNG Tutorials
Just a small introduction about all then we will discuss TestNG in details.

1-TestNG, Junit, Nunit are a separate testing framework which is freely available in the market.

2-TestNG, JUnit mainly we can integrate into Java whereas NUnit works with C# only.

3-Junit comes by default with Eclipse but TestNG you have to install an Add-ons.

4-Junit has fewer annotations as compared to TestNG.

5-Junit automatically does not create HTML report so we have to use ant build tool, but TestNG has rich HTML report generations even for single test case it creates 3 HTML reports

 

we have so much difference please refer their official website you will get a clear picture that how it works.

Note- TestNG is successor of Junit so I will be focusing on TestNG

Official website for Junit is-    http://junit.org/

Official website for TestNg is- http://testng.org/

Official website for NUnit is-   http://www.nunit.org/

What is TestNG and Why we have to use in Selenium

Ans- TestNG is a testing framework which is designed to cover all categories of tests:  unit, functional, end-to-end, integration, etc…

We can combine TestNG with Selenium and we can write Test Cases in Eclipse using Java.

 

TestNG Tutorials for beginner

Some of the TestNG feature as listed below

1- Annotation- It support multiple annotations at a various level so we will discuss in the separate post.

2- Support for data-driven testing (with @DataProvider)

3- Support for parameters.

4- Generate automatic reports

5- We can run failed test case only using testng.xml no need to run the full test suite in case of failure

6- Supported by a variety of tools and plug-ins (Eclipse, IDEA, Maven, etc…).

7- Default JDK functions for runtime and logging (no dependencies).

8- Dependent methods for application server testing.

 

                                 I have very details video on TestNG which will help you to get started 

TestNG Tutorials for Selenium 

Configure Eclipse with Selenium and TestNG

New- Installation of TestNG Using Eclipse MarketPlace

Create First TestNG Program and Execute.

Check reports generated by TestNG

Cross browser testing using TestNG

Create Data driven framework using DataProivider

 How to execute only failed testcases in Selenium Webdriver

How to generate Selenium log using TestNG

What is TestNG Listerner and How to implement TestNG Listener in Selenium Webdriver

How to disable testcase in Selenium using TestNG

How to group Testcases in Selenium using TestNG 

How to create dependency between Test cases in Selenium Webdriver

How to execute testng.xml file through Java Program

Execute Selenium Webdriver Test cases parallel using TestNG

Create custom report with Graph, Pie Chart and Screenshot attached in Report

105 thoughts on “TestNG Tutorials for Selenium Webdriver

  1. ankit says:

    Hi sir can you give me pom.xml dependencies i am unable to find the deoendencies and build please prove me link to

  2. vasanthan R says:

    Very interesting to learn Automation with help of you Mukesh.

    Do you have any post regarding TOSCA?

    1. Hi Vasanthan,

      Sorry, I don’t have any posts for TOSCA…

  3. RajaV says:

    hi Mukesh,
    I have created a java class with just simple code to read a excel file data, and it has been converted as TestNG class. there is no any error while run the code but when i trying to debug the code it says “Source not found” NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available. kindly suggest what i have to do.

    1. Hi Raja,

      Kindly use *Convert TestNG* instead of *Create testNG class*, if you were trying to run your script through testng.xml

  4. Hema.T says:

    Im a fresher for the Testing. And i started creating base class for login using TestNG and the expected page got open.
    The next step i have a bootstrap dropdown buttons containing links to the pages.
    my question is how i call the baseclass to a new class in testNG

    1. Hi Hema,

      Either create an object base class or use inheritance concept.

  5. Vandana says:

    Hi Mukesh,
    I have tried Create Data driven framework using DataProivider scenario 1.
    But getting exception java.lang.RuntimeException: java.lang.ArrayIndexOutOfBoundsException: 2
    Can you help me out please.

    1. Hi Vandana,

      Please debug your code because this exception comes when we try to access index of an array which doesn’t even exist. Check how many data are you passing from dataProvider annotated method.

  6. sudheer says:

    Hi, I am not getting xml file location in the console. What should I do?

    1. Hi Sudheer,

      Have you triggered your script through testng.xml file?

  7. Sudha says:

    Thanks for your reply but have confusion ,where should i insert that code within a class or TestNG.xml file,can u send me example XML file.

    1. Hi Sudha,

      This is actually invoking testng.xml from a java class which is called as testng runner class. Please check this link http://testng.org/doc/documentation-main.html#running-testng-programmatically

  8. Sudha says:

    I got a case.. Running TestNG.XML file suppose i have 10 classes(scripts),in that 10 class first class is filed.Is there any way to skip the 9 classes ,Please provide the solution.

  9. Sonali says:

    Superb Article.Very easy way to learn.
    Thanks for sharing such a great knowledge

    1. Hi Sonali,

      Thanks for your appreciation…:) I’ll always try my level best to serve my subscribers with best content

  10. ashish says:

    Hi Mukesh,

    Can you please explain in details how to use annotations efficiently

  11. PRAMOD says:

    why don’t we use main method in testng whats the reason

    1. Hi Pramod,

      TestNG library already handles this logic itself. That is why, TestNG makes framework creation and script execution easy.

  12. PRAMOD says:

    Hi Mukesh the way of u r explination is fantastic…
    i have small doubt if there are 20 test case i want to execute all together by generating xml file but in case if i want create own xml file what is the approach to create own xml file with out generating by default

    1. Hi Pramod,

      If you executing those test cases using TestNG framework then syntax and content on xml file should be as per what is being defined by testng.

  13. jyoti says:

    nice explaination. Thankyou.

    1. Hi Jyoti,

      Thanks for your appreciation…:)

  14. Sree says:

    Hi Mukesh could you please provide code for executing the failed testcases automatically whenever it fails by using testng framework

    1. Hi Sree,

      There are 2 ways to do this,
      1. TestNG creates test-output folder, which contains xml file viz testng-failed. Run it using testng runner class
      2. Implement TestNG IRetryAnalyzer interface into a class, where you can set no. of times of execution also.

  15. Babu says:

    How to continue running test case execution in TestNG even a test case in TestNG.xml fails

    1. Hi Babu,

      If you mention multiple test cases in TestNg.xml, by default it executes all test cases even though something fails in between.

  16. Ajay says:

    How to compare two xml files via selenium web driver? I searched all your videos but i couldn’t find it.Thanks in advance

    1. Hi Ajay,

      There are two ways to parse xml, SAX Parser and JDOM Parser. Please visit these link –> JDOM parser SAX Parser. Read each node and compare it.

  17. Saikumar says:

    Hi mukesh ,
    I am using TestNG currently , I am declaring a Chrome driver instance in @BeforeTest class but i am not able to use the same instance in @Test please advice

    1. Hi Saikumar,

      I hope you are using same driver instance everywhere.

  18. Ajmal says:

    Hi Mukesh,
    1.Package A
    1.1BaseTest
    2.Package B
    2.1 Pages (contains action and related functions of each page)
    3.package C (Holds my Testscripts)
    1.TestCase 1 login
    2.TestCase 2 Homepage
    My First Testcase contains (3 test methods with priority sequence like 1. 2,3 (working fine)
    My second Testcase (3 test methods with priority sequence like 1. 2,3 but getting failed at test method one itself (priority =1)
    Error :- unable to locate an element
    the same thing working fine if i change the priority of the second test case testmethod priority 1 to priority 4.

    1. Hi Ajmal,
      This is not an issue of testng. Unable to locate element comes from selenium. So debug your code because issue is wrt locator which you have used.

      1. Atul says:

        Hi Mukesh,
        How can we generate XLST report , If we are running our tests using maven integration, what additional configuration required in POM.xml to generate XLST report.

  19. Ajmal says:

    Hi Mukesh,
    i have question ,we can execute failed testcase using(testng-failed.xml) but if my testcase have dependecy on some pass testcase in this case how to can execute.

    1. Hi Ajmal,

      testng-failed.xml will also contains dependent test cases also(either passed or failed). As without these dependencies, failed ones won’t get execute.

      1. Deepa Kulkarni says:

        Hi Mukesh, does testing-failed.xml file will generate automatically for failed TC? or we manually need to create it to run only failed TCs?

        1. Hi Deepa,

          It creates automatically.

          1. Deepa says:

            ok Thank you

  20. Jayasree says:

    Hi Mukesh,
    I have a TestNG project. Don’t have any main class, currently it is running like “Run As TestNG”.
    I want to export it as runnable jar or jar so that any one can just hit a command from command line and test cases start running.
    Thanks in advance

    1. Hi Jayasree,

      Using Eclipse Export Wizard, select ‘Create Runnable Jar’ and select the class which is entry point (TestNG runner class having main method which should read testNG xml file) of your project.
      Now, to create testng runner class, please refer this link http://testng.org/doc/documentation-main.html#running-testng-programmatically

  21. lalit says:

    Hi Mukesh,
    I am quite new to automation and trying to Install TestNG from MyEclipse But I cannot find Market Place from where I can download. Any suggestion?
    MyEclipse Enterprise Workbench version 2017 CI 8 I am using.

  22. Ajit Kumar says:

    Hi Mukesh,
    I always watch your video on you tube and I like your study material bcz your explanation style is nice and it is very easy to understand. I ave one question I want to write data from excel sheet and I also want to make this code as library for use many times. So how can i make this, please share me code,
    Thank you

  23. Rabbu says:

    Hello Sir,
    I am one of your big fan. You are amazing teacher. I love your video specially Automation testing. It is very accurate and easy to understand. God bless.
    Thank you,

    1. Hi Rabbu,

      Your comment made my day. Very very thanks for your blessings…:)

  24. Aditya Gour says:

    Hi Sir,
    i want to capture screenshot but i am facing error.
    please see below code.
    i am facing error in this line “FileUtils.copyFile(scrFile, “C:\\Aditya”);”

  25. Ravi Gavkare says:

    Hello Mukesh,
    I am getting below error while running multiple testcases.

    org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?

    It works when only one test executed at a time but when I try to run more than one test case then the second onward testcase failed due to above mentioned reason.
    Please suggest.

    1. Hi Ravi,

      Exception message itself is saying that you are using WebDriver instance after calling quit(). You might be calling quit() method at end of first test script. Call this quit() after execution of last script.

  26. Mohini says:

    Hi Mukesh,
    How to handle Dropdown with TestNG and Page Factory concept.

    1. Hi Mohini,

      Handling drop down is nowhere related to TestNG and Page Factory. Create a method/function in library and use wherever you want.

  27. Sachith Palihawadana says:

    Your tutorials are really helpful . Thanks in advance 🙂

    1. Hi Sachith,

      Thanks…:)

  28. Anant Anurag Yadav says:

    Hi Mukesh,
    I am a big fan of your selenium videos. They are amazing and to the point. I had just recently started learning selenium on my company’s laptop and I have some issues in all the browsers. Please help me out
    Firefox(55.0.3): Selenium WebDriver would open the browser but won’t do anything further. It won’t even enter the URL.
    Chrome(60.0.3112.113): Browser opens and then gets closed abruptly
    IE(11.0.9″): It works extremely slow. For instance if I try to launch FB and enter the name in the First Name field using SendKeys() it takes years to enter it.

    Below are some more details:
    Eclipse: Oxygen Release (4.7.0)
    OS: Windows 7 64 bit
    Selenium: selenium-java-3.5.3
    I can’t upgrade or downgrade any piece of software including the browsers due to admin settings.

    Help me out!!

    1. Hi Anant,

      In your case, i would suggest you to go with latest geckodriver but I am not quite sure about Selenium 3.5.3.

  29. RANJAN V says:

    Your Tutorials are amazing.

    1. Hi Ranjan,

      Thanks for your comments…Keep learning…:)

  30. Ajith says:

    when i trying to run testNg program it show some “A JNI error has occurred, please check it your installation and try it again” how can i solve this problem

    1. Hi Ajith,

      Please mention which version of testNG and eclipse you’re using?

  31. Gautam Pandey says:

    Hi Mukesh,
    Could, I run a testNG suite of 100 test cases by using selenium grid. If it is possible to run, please tell me how to set the all the configuration in all test cases at a time.

  32. Raj says:

    1. Please correct me if my understanding is correct: How we know, that TestNG is installed in the eclipse: I did Rt. Click on the Project- > It shows”TestNG”. That means it is installed. Am i right?
    2. Iam trying to run the “testng.xml” as a TestNG Suit. But iam getting following error :-
    ” Cannot complete the install because one or more required items could not be found.
    Software being installed: TestNG M2E Integration (Optional) 6.11.0.201703011520 (org.testng.eclipse.maven.feature.feature.group 6.11.0.201703011520)
    Missing requirement: TestNG M2E Integration 6.11.0.201703011520 (org.testng.eclipse.maven 6.11.0.201703011520) requires ‘bundle org.eclipse.m2e.core 1.5.0’ but it could not be found
    Cannot satisfy dependency:
    From: TestNG M2E Integration (Optional) 6.11.0.201703011520 (org.testng.eclipse.maven.feature.feature.group 6.11.0.201703011520)
    To: org.testng.eclipse.maven [6.11.0.201703011520]”

    Could you please tell me what is the issue here? Is it a TestNg installation issue? Please guide.

    1. Hi Raj,

      Check in marketplace whether testng is installed completely or not. If it is installed, then uninstall it. Restart eclipse and install it once again. Sometimes due to network connectivity, testng installation failure happens.

  33. Sumanta bhattacharjee says:

    How to handle Drop down and Date picker in Selenium?

  34. sesidhar says:

    Hi Mukesh
    Can we find the bugs in web application through selenium scripts??

    1. Hi Sesidhar,

      Automation is done for regression testing. If your script fails then it means that required data or functionality flow is not working. But you can’t test whole application thoroughly using Selenium.

  35. sesidhar says:

    Hi Mukesh
    After completion of writting entire code ,Is we have to send the code to client?? If we have to send means, how please to tell me (OR) The test ng reports are enough to show to the clients .

    1. Hi Sesidhar,

      It depends on your client requirements. Ask them, what they want?

  36. sesidhar says:

    Hi Mukesh
    can we use test suite in data provider annotation ??
    please tell me the example in real time .

    1. Hi Sesidhar,

      I think, dataprovider works with @Test annotations only

  37. Saikrishna says:

    Hi mukesh,

    Please guide how to parameterize the scripts using Excel.

    1. Hi Saikrishna,

      Feed the required data which you are reading from excel directly into your script. For more details, check this link

  38. Ramya says:

    Hi Mukhesh,

    There could be a situation when we have to have Junits and TestNG files in the same Maven project together. I believe we have to have Maven Profiles in such cases. Could you please share an example if u have implemented anything like that by any chance.

    1. Hi Ramya,

      As per you mentioned, JUnit and TestNG jars can coexist in same profile. Only this is whenever you use any annotation which are common to both JUnit and TestNG, while coding, import only those which you want.
      As of now, I don’t have any post based on profile in Maven, but very soon I’ll post it.

  39. Manish Sharma says:

    Hi Mukesh,

    Please guide be to write the script for below test case.
    Login from user1
    Update user profile details like Age, date of birth, Pan number, Adhara number.
    Logout

    I want to repeat this scenario for 200 users by using testNG data driven framework.

    my concern is this that how many methods i have to creates for this scenario?
    i have created 5 methods two dataprovider methods for login and update.
    open()
    login()
    updateDetails()
    logOut()
    quitBrowser()

    But execution first completed the login loop and after that gets failed

    1. Hi Manish,

      I think these methods are enough, only thing is that your test data(i.e. user credentials are changing). Keep credentials of 200 users in some external files such as excel file. Loop you script execution 200 times in which each it read it from excel one by one.

  40. Prabhakar says:

    Hi Mukesh,

    This is Prabhakar. Am very big fan your articles in Selenium and follow regularly.Have one query ..Can we write and execute Selenium Code with TestNG jars Since don’t have internet access inside my project to download from eclipse.

    Thanks,
    Prabhakar

    1. Hi Prabhakar,

      There are two things for testng wrt query you have asked over here. One is testng plugin for eclipse which is kind of tricky to configure offline and second one is testng jar file which should be available in build path of your project in order to execute scripts.
      Now, tell me which one is your concern?

      1. Prabhakar says:

        Am trying to execute scripts with TestNg by adding TestNG jars to my project..Is it allowed ?

        From Eclipse downloaded TestNg tried(From Home) and is working fine..

        But in my project(Bank) where don’t have access to download any file from internet..So is there any other option to access TestNG like adding jars to project ..

        1. Hi Prabhakar,
          It is better to discuss this requirement with your project manager. Usually organizations implement their proxy settings won’t allow you to download any jar file and if you do this by your own then they can sniff it.

  41. satya narayana says:

    hi mukesh
    is there any emulator online which does the job of selenium webdriver

    1. Hi Satya,

      Sorry, I didn’t get your question.

  42. Manish says:

    Hi Mukesh,
    Could you please post an example for the following
    1. @BeforeClass and @AfterClass for a multiple class test.
    2. @Before Mothod and @AfterMethod for a multiple class test.

    1. Yes I will try to post soon.

  43. Thanks I was able to complete the TestNG tutorial for the above tutorials for beginners. Is there something for advanced level as well or interview questions related to just testNG

    1. Yes Gaurav there is lot more to explore about testng http://testng.org/doc/documentation-main.html

  44. Vinit Kumar says:

    Hi Mukesh
    I want to know that how to write x-path in Chrome browser.Because fire bug is not plugin for chrome .is there any way to write x-path without fire bug.

    1. Hi Vinit,

      Yes Vinit, there is a chrome extension called as ‘XPath Helper’ using which relative xpath can be verified. There is another way but easy for those who can create relative xpath on the fly. Please follow these steps:
      1) Once you load your required webpage, press F12(Developer tool)
      2) Now press Cntrl+F12
      3) Now you can see one text box at bottom of html script.

      Now in this texbox you can check your relative xpath and verify matching nodes.

  45. renuka says:

    hi mukesh
    i have one doubt.i am able to open crome or firefox using maven project ? if yes where did we put the chromedriver.exe or geckodriver.exe

    1. Hi Renuka,

      Maven doesn’t affect your script execution. It gives you Maven structure for your project and help you to meet dependencies as defined in POM.xml. You can use all .exe files as usual.

  46. manish says:

    Hi Mukesh,
    Can we set priority of Test in testng.xml file itself ?
    Thanks!!

    1. Hi Manish you can assign groups and sequence in testng.xml file.

  47. suraj says:

    Hi Mukesh,
    Can you share the sample framework of hybrid and keyword driven using TestNG.
    That will be really helpful

  48. Neha says:

    Hi Mukesh,
    This is not what I am looking for.
    I want to automate one web application where we have to set http request headers with that request.

    Then user will be identified with those http headers and then he will be redirected to the homepage of that application

    Can we do this using selenium?
    Plesae explain this

    1. Hi Neha,

      I am not sure on this. I have mainly worked in test automation so not good in api testing.

  49. Neha says:

    How can we execute http request using selenium webdriver which requires http headers?
    And that url will open browser having UI

    1. Hi Neha,

      This is what exactly you are looking https://vistasadprojects.com/mukeshotwani-blogs-v2/find-broken-links-using-selenium/

      If not then please explain requirement in brief.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.