Hello Welcome to TestNG tutorial, today we will see how to write testng script in Selenium. Before moving forward make Sure TestNG and Eclipse is ready is Installed.
If you are new to Eclipse the check Eclipse tips and tricks also.
Q1-Difference between Java Program and TestNg Script?
Ans- When we execute Java program and TestNG script then functionality wise nothing will change because our script is performing the same functionality but using TestNG you will get some additional functionality.
Some benefit of TestNG Script
1- For even single test case you will get 3 reports. These reports generated by TestNG
2-You can check execution time i.e. How much time test case has taken
3-Parallel execution etc
Let’s write 1 simple java program and 1 TestNG script then will compare
Basic Java Program
public class TestJava1 { public static void main(String[] args) { System.out.println("Hello java"); } }
Steps to Write testng script
import org.testng.annotations.Test; public class TestScript1 { @Test public void appTest() { System.out.println("Hello Selenium"); } }
Q2- How we can write TestNG Script?
Ans- To execute TestNG script we don’t have to write the separate class. We can use simple java class but here we will not write public static void main(String []args) because we are not going to execute this from JVM.
TestNG works with Annotations and annotation can be represented by @ symbol
@Test- is this the main annotation from where TestRunner will start execution.
In other words, you can say @Test in entry point
- Step 1- Select your project
- Step 2- Go to src and create a new package and give any name (I gave demo)
- Step 3- Now select above created package and create a new class and give any name (I gave TestScript1)
- Step 4- create a simple java method and before that method use @ Test
Complete program write testng script
import org.testng.annotations.Test; public class TestScript1 { @Test public void appTest() { System.out.println("Hello Selenium"); } }
Step 5- Once you completed the script simply right click on above created program and select run as> TestNG Test.
Check Official documentation of TestNG for more info 🙂
Thanks for visiting my Blog. Keep reading and visiting.
Have a nice day.
Sravani Batta says
Hi Mukesh,
I facing an issue in Testng.In Testng.xml i have two classes under Tag ,When i run Testng.xml only one class is execute and second class is skipped.
i am starting application in @BeforeMethod and quitting browser in @AfterMethod
Webdriver refrence variable is Static .
Can you please help me to resolve this issue
Mukesh Otwani says
Which testng version u r using?
SATVEER BAJWA says
Hi Mukesh,
I have been using Test NG from many days and I had run successfully all my programs but now I am trying to run new testcase with TestNG it caused many problems. I have some questions related to TestNG:
1. Do we need to add testng.xml file every time while using TestNG?
2. When I run my script it shows nothing to run or value 0 for test run, failures and skips. I am running through testng.xml file after right click on it and select run as TestNG suite.
Let me know if you have any questions .
Thanks in advance.
Thank you
Satveer
Mukesh Otwani says
Hi Satveer,
There is no need to create separate testng.xml file. If you want to trigger your testng based test script through testng.xml file then you have add it into required testng.xml file. Always check package is mentioned correctly
SATVEER BAJWA says
Okay. Thanks Mukesh for your reply and one more question we need xml file only in case of parallel execution right? otherwise if we are running single test case then no need to call xml c, we can directly run test case through right click in the project and run as TestNG
Thank you so much for your quick response.
Mukesh Otwani says
Hi Satveer,
You can execute any number of test cases in either way. May this post will make you understand it more and here is the link http://learn-automation.com/parallel-execution-in-selenium/
bhaskar says
“”org.testng.TestNGException:
Cannot find class in classpath:””
I am getting above error while running the testcase…help me with this..
Mukesh Otwani says
Hi Bhaskar,
Please create a fresh project and try again. Please follow below video as well for complete details https://www.youtube.com/watch?v=OTtFSnZY4f8
Gaurav Khurana says
Can you tell about this , where the Test NG Program will run if not in JVM
“we will not write public static void main(String []args) because we are not going to execute this from JVM.”
Mukesh Otwani says
Hi Gaurav,
Annotations of TestNG are itself a class which contains the definition of main() method due to which annotations drives compilation forward.
Vaithi says
Much Helpful.. pls carry on your service..
Mukesh Otwani says
Thanks Vaithi
shalini says
Hi Mukesh,
can you please let me know the software versions you are using.
I am using TestNG- 6.9.12, ide- 4.6.0, selenium-2.25, eclipse java-3.12.0
when i execute my TestNG programming, i am getting following error:
Exception in thread “main” java.lang.NoSuchMethodError: org.testng.internal.Utils.defaultIfStringEmpty(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
Mukesh Otwani says
Hi Shalini,
I am using Selenium 2.53.1, Eclipse Mars 4.6, TestNG 6.9.8 and Java 8
sudhr says
.what is the command line to run Testng batch file in command prompt? not xml file ?
Mukesh Otwani says
Hey,
Below is the command
java -cp bin;libs/* org.testng.TestNG testng.xml
sudhr says
hi,
i want to take one testcase report,from all the run test cases.How can i get it without using testNG?
and
.Scenario:in a page we have Apply button 3 times,one on top,2nd in middle,3rd in below.I want to click middle Apply button. how?
got question from an interview
Mukesh Otwani says
Hey Sudhher,
you can use findElements method in this case.http://learn-automation.com/difference-between-findelement-and-findelements-in-selenium-webdriver/
sudhr says
how to run only skipped test cases in selenium web driver?
Mukesh Otwani says
Hi Sudhr,
rerun test cases also can be run by testng-failed.xml file.
sudhr says
hi,
i want to run some method in sequential order and some in parallel in testng.how would u configure the testng.xml?
Mukesh Otwani says
Hey Sudhr,
In this case you can create 2 xml file. One will run in parallel and one in sequence.
sudhr says
it is not possible in one xml file
Mukesh Otwani says
yes it is possible.
sudhr says
please tell me how to configure in one xml file?
Mukesh Otwani says
Hey,
You can convert all script in testng.xml file and you can trigger direct from eclipse or ant or pom.
Rajesh Kumar says
I encountered the same problem.. how to resolve it.. please tell me
Mukesh Otwani says
Hi Rajesh,
I did not get. Can you please explain what problem you are facing with TestNG?
tini says
share ur script
Mukesh Otwani says
Hi Tini,
import org.testng.annotations.Test;
public class TestScript1 {
@Test
public void appTest() {
System.out.println(“Hello Selenium”);
}
}
Sugu says
My Eclipse Version: Luna Service Release 2 (4.4.2)
I installed TestNG and created a TestNG class file. I imported (org.testng.annotations.Test) and added TestNG library. When I run the test case as TestNG Test, I am getting the following error.
An internal error occurred during: “Launching TestScript1”.
java.lang.NullPointerException
Mukesh Otwani says
Hi Sugu,
Can you please share the script ? Because it is very difficult to guess what is the issue.
There can be so many reason for NullPointerException
Sugu says
The script not even started to run. Immediately after I select to run the test case as TestNG Test, it shows the error.
‘Launching Gmail’ has encountered a problem.
An internal error occurred during: “Launching Gmail”.
java.lang.NullPointerException
Mukesh Otwani says
Hi Sugu,
Your script is fine its eclipse issue only can you please check that you are using Latest Version Eclipse Mars?
Upgrade Eclipse and issue should be resolved.
Thanks
Mukesh