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.
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
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
Mukesh Otwani says
Hi Ankit,
Please check this link http://learn-automation.com/selenium-maven-build-and-usage/
OR you can visit my framework video playlist here for more understanding https://www.youtube.com/watch?v=vFXL4nMWvXI&list=PL6flErFppaj0WwNOMFeXPVlNCDuJyPYFi
vasanthan R says
Very interesting to learn Automation with help of you Mukesh.
Do you have any post regarding TOSCA?
Mukesh Otwani says
Hi Vasanthan,
Sorry, I don’t have any posts for TOSCA…
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.
Mukesh Otwani says
Hi Raja,
Kindly use *Convert TestNG* instead of *Create testNG class*, if you were trying to run your script through testng.xml
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
Mukesh Otwani says
Hi Hema,
Either create an object base class or use inheritance concept.
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.
Mukesh Otwani says
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.
sudheer says
Hi, I am not getting xml file location in the console. What should I do?
Mukesh Otwani says
Hi Sudheer,
Have you triggered your script through testng.xml file?
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.
Mukesh Otwani says
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
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.
Mukesh Otwani says
Hi Sudha,
You have to edit testng.xml file in run time. Then may be this post will help you https://stackoverflow.com/questions/35693359/how-to-skip-a-class-which-is-mentioned-in-testng-xml-through-programs
Sonali says
Superb Article.Very easy way to learn.
Thanks for sharing such a great knowledge
Mukesh Otwani says
Hi Sonali,
Thanks for your appreciation…:) I’ll always try my level best to serve my subscribers with best content
ashish says
Hi Mukesh,
Can you please explain in details how to use annotations efficiently
Mukesh Otwani says
Hi Ashish,
You can refer this link http://learn-automation.com/testng-tutorials-for-beginners/ for your initial learning then refer this link also https://www.tutorialspoint.com/testng/testng_basic_annotations.htm
PRAMOD says
why don’t we use main method in testng whats the reason
Mukesh Otwani says
Hi Pramod,
TestNG library already handles this logic itself. That is why, TestNG makes framework creation and script execution easy.
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
Mukesh Otwani says
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.
jyoti says
nice explaination. Thankyou.
Mukesh Otwani says
Hi Jyoti,
Thanks for your appreciation…:)
Sree says
Hi Mukesh could you please provide code for executing the failed testcases automatically whenever it fails by using testng framework
Mukesh Otwani says
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.
Babu says
How to continue running test case execution in TestNG even a test case in TestNG.xml fails
Mukesh Otwani says
Hi Babu,
If you mention multiple test cases in TestNg.xml, by default it executes all test cases even though something fails in between.
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
Mukesh Otwani says
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.
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
Mukesh Otwani says
Hi Saikumar,
I hope you are using same driver instance everywhere.
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.
Mukesh Otwani says
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.
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.
Mukesh Otwani says
Hi Atul,
Extent report is far better option than XSLT report http://learn-automation.com/advance-selenium-reporting-with-screenshots-2/
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.
Mukesh Otwani says
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.
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?
Mukesh Otwani says
Hi Deepa,
It creates automatically.
Deepa says
ok Thank you
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
Mukesh Otwani says
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
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.
Mukesh Otwani says
Hi Lalit,
Please use this links for eclipse download (based on your system architecture)
32 Bit -> https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/oxygen/1a/eclipse-jee-oxygen-1a-win32.zip
64 Bit -> https://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/oxygen/1a/eclipse-jee-oxygen-1a-win32-x86_64.zip
After extracting eclipse, follow this link http://learn-automation.com/how-to-install-testng-in-eclipse/
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
Mukesh Otwani says
Hi Ajit,
Please go through this link http://learn-automation.com/read-and-write-excel-files-in-selenium/
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,
Mukesh Otwani says
Hi Rabbu,
Your comment made my day. Very very thanks for your blessings…:)
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”);”
Mukesh Otwani says
Hi Aditya,
Please check this link http://learn-automation.com/how-to-capture-screenshot-in-selenium-webdriver/
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.
Mukesh Otwani says
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.
Mohini says
Hi Mukesh,
How to handle Dropdown with TestNG and Page Factory concept.
Mukesh Otwani says
Hi Mohini,
Handling drop down is nowhere related to TestNG and Page Factory. Create a method/function in library and use wherever you want.
Sachith Palihawadana says
Your tutorials are really helpful . Thanks in advance 🙂
Mukesh Otwani says
Hi Sachith,
Thanks…:)
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!!
Mukesh Otwani says
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.
RANJAN V says
Your Tutorials are amazing.
Mukesh Otwani says
Hi Ranjan,
Thanks for your comments…Keep learning…:)
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
Mukesh Otwani says
Hi Ajith,
Please mention which version of testNG and eclipse you’re using?
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.
Mukesh Otwani says
Hi Gautam,
Please refer this link http://learn-automation.com/selenium-grid-for-remote-execution/
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.
Mukesh Otwani says
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.
Sumanta bhattacharjee says
How to handle Drop down and Date picker in Selenium?
Mukesh Otwani says
Hi Sumatha,
Please check these link…
1. http://learn-automation.com/handle-calender-in-selenium-webdriver/
2. http://learn-automation.com/handle-dropdown-in-selenium-webdriver/
3. http://learn-automation.com/handle-bootstrap-dropdown-in-selenium-webdriver/
sesidhar says
Hi Mukesh
Can we find the bugs in web application through selenium scripts??
Mukesh Otwani says
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.
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 .
Mukesh Otwani says
Hi Sesidhar,
It depends on your client requirements. Ask them, what they want?
sesidhar says
Hi Mukesh
can we use test suite in data provider annotation ??
please tell me the example in real time .
Mukesh Otwani says
Hi Sesidhar,
I think, dataprovider works with @Test annotations only
Saikrishna says
Hi mukesh,
Please guide how to parameterize the scripts using Excel.
Mukesh Otwani says
Hi Saikrishna,
Feed the required data which you are reading from excel directly into your script. For more details, check this link
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.
Mukesh Otwani says
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.
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
Mukesh Otwani says
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.
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
Mukesh Otwani says
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?
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 ..
Mukesh Otwani says
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.
satya narayana says
hi mukesh
is there any emulator online which does the job of selenium webdriver
Mukesh Otwani says
Hi Satya,
Sorry, I didn’t get your question.
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.
Mukesh Otwani says
Yes I will try to post soon.
Gaurav Khurana says
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
Mukesh Otwani says
Yes Gaurav there is lot more to explore about testng http://testng.org/doc/documentation-main.html
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.
Mukesh Otwani says
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.
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
Mukesh Otwani says
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.
manish says
Hi Mukesh,
Can we set priority of Test in testng.xml file itself ?
Thanks!!
Mukesh Otwani says
Hi Manish you can assign groups and sequence in testng.xml file.
suraj says
Hi Mukesh,
Can you share the sample framework of hybrid and keyword driven using TestNG.
That will be really helpful
Mukesh Otwani says
http://learn-automation.usefedora.com/p/selenium-frameworks-and-selenium-question-answers
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
Mukesh Otwani says
Hi Neha,
I am not sure on this. I have mainly worked in test automation so not good in api testing.
Neha says
How can we execute http request using selenium webdriver which requires http headers?
And that url will open browser having UI
Mukesh Otwani says
Hi Neha,
This is what exactly you are looking http://learn-automation.com/find-broken-links-using-selenium/
If not then please explain requirement in brief.