Basic Selenium

Effective and best way to write testng script in Selenium

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.

 

author-avatar

About Mukesh Otwani

I am Mukesh Otwani working professional in a beautiful city Bangalore India. I completed by BE from RGPV university Bhopal. I have passion towards automation testing since couple of years I started with Selenium then I got chance to work with other tools like Maven, Ant, Git, GitHub, Jenkins, Sikuli, Selenium Builder etc.

34 thoughts on “Effective and best way to write testng script in Selenium

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

    1. Which testng version u r using?

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

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

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

        1. 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 https://vistasadprojects.com/mukeshotwani-blogs-v2/parallel-execution-in-selenium/

  3. bhaskar says:

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

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

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

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

    1. Hi Gaurav,

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

  5. Vaithi says:

    Much Helpful.. pls carry on your service..

  6. 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;

    1. Hi Shalini,

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

  7. sudhr says:

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

    1. Hey,

      Below is the command

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

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

  9. sudhr says:

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

    1. Hi Sudhr,

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

  10. 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?

    1. Hey Sudhr,

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

      1. sudhr says:

        it is not possible in one xml file

        1. yes it is possible.

          1. sudhr says:

            please tell me how to configure in one xml file?

          2. 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:

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

    1. Hi Rajesh,

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

  12. tini says:

    share ur script

    1. Hi Tini,

      import org.testng.annotations.Test;

      public class TestScript1 {

      @Test
      public void appTest() {

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

      }

      }

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

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

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

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

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.