• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / Effective and best way to write testng script in Selenium

Effective and best way to write testng script in Selenium

March 26, 2015 by Mukesh Otwani 34 Comments

TestNG tutorials

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.

 

write testng script

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.

 

Filed Under: Basic Selenium

Reader Interactions

Comments

  1. Sravani Batta says

    February 3, 2022 at 7:58 PM

    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

    Reply
    • Mukesh Otwani says

      February 4, 2022 at 1:36 PM

      Which testng version u r using?

      Reply
  2. SATVEER BAJWA says

    March 25, 2019 at 9:02 PM

    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

    Reply
    • Mukesh Otwani says

      March 25, 2019 at 10:38 PM

      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

      Reply
      • SATVEER BAJWA says

        March 26, 2019 at 2:30 AM

        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.

        Reply
        • Mukesh Otwani says

          March 26, 2019 at 10:25 AM

          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/

          Reply
  3. bhaskar says

    January 24, 2017 at 2:56 PM

    “”org.testng.TestNGException:
    Cannot find class in classpath:””

    I am getting above error while running the testcase…help me with this..

    Reply
    • Mukesh Otwani says

      January 24, 2017 at 4:03 PM

      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

      Reply
  4. Gaurav Khurana says

    January 14, 2017 at 10:39 AM

    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.”

    Reply
    • Mukesh Otwani says

      January 14, 2017 at 12:36 PM

      Hi Gaurav,

      Annotations of TestNG are itself a class which contains the definition of main() method due to which annotations drives compilation forward.

      Reply
  5. Vaithi says

    October 12, 2016 at 2:43 PM

    Much Helpful.. pls carry on your service..

    Reply
    • Mukesh Otwani says

      October 13, 2016 at 8:52 AM

      Thanks Vaithi

      Reply
  6. shalini says

    October 5, 2016 at 4:32 AM

    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;

    Reply
    • Mukesh Otwani says

      October 5, 2016 at 11:09 PM

      Hi Shalini,

      I am using Selenium 2.53.1, Eclipse Mars 4.6, TestNG 6.9.8 and Java 8

      Reply
  7. sudhr says

    August 23, 2016 at 6:52 PM

    .what is the command line to run Testng batch file in command prompt? not xml file ?

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:14 PM

      Hey,

      Below is the command

      java -cp bin;libs/* org.testng.TestNG testng.xml

      Reply
  8. sudhr says

    August 23, 2016 at 6:43 PM

    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

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:15 PM

      Hey Sudhher,

      you can use findElements method in this case.http://learn-automation.com/difference-between-findelement-and-findelements-in-selenium-webdriver/

      Reply
  9. sudhr says

    July 24, 2016 at 9:52 PM

    how to run only skipped test cases in selenium web driver?

    Reply
    • Mukesh Otwani says

      July 25, 2016 at 12:05 PM

      Hi Sudhr,

      rerun test cases also can be run by testng-failed.xml file.

      Reply
  10. sudhr says

    July 2, 2016 at 11:49 AM

    hi,

    i want to run some method in sequential order and some in parallel in testng.how would u configure the testng.xml?

    Reply
    • Mukesh Otwani says

      July 2, 2016 at 3:02 PM

      Hey Sudhr,

      In this case you can create 2 xml file. One will run in parallel and one in sequence.

      Reply
      • sudhr says

        July 3, 2016 at 2:24 AM

        it is not possible in one xml file

        Reply
        • Mukesh Otwani says

          July 7, 2016 at 9:14 AM

          yes it is possible.

          Reply
          • sudhr says

            July 11, 2016 at 2:42 PM

            please tell me how to configure in one xml file?

          • Mukesh Otwani says

            July 12, 2016 at 4:54 PM

            Hey,

            You can convert all script in testng.xml file and you can trigger direct from eclipse or ant or pom.

  11. Rajesh Kumar says

    December 10, 2015 at 5:54 PM

    I encountered the same problem.. how to resolve it.. please tell me

    Reply
    • Mukesh Otwani says

      December 12, 2015 at 3:53 PM

      Hi Rajesh,

      I did not get. Can you please explain what problem you are facing with TestNG?

      Reply
  12. tini says

    December 9, 2015 at 12:29 PM

    share ur script

    Reply
    • Mukesh Otwani says

      December 12, 2015 at 4:00 PM

      Hi Tini,

      import org.testng.annotations.Test;

      public class TestScript1 {

      @Test
      public void appTest() {

      System.out.println(“Hello Selenium”);

      }

      }

      Reply
  13. Sugu says

    December 3, 2015 at 2:22 PM

    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

    Reply
    • Mukesh Otwani says

      December 3, 2015 at 9:54 PM

      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

      Reply
      • Sugu says

        December 7, 2015 at 8:18 PM

        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

        Reply
        • Mukesh Otwani says

          December 7, 2015 at 9:43 PM

          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

          Reply

Leave a Reply Cancel 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.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?