Previously you were executing testng.xml file through eclipse only. Here, you will execute the same testng.xml file using the command line and storing logging information using Reporter class present in org.testng.
In this tutorial, you will learn-
TestNG is an automation testing framework in which NG stands for "Next Generation". TestNG is inspired from JUnit which uses the annotations (@).
- Using TestNG you can generate a proper report, and you can easily come to know how many test cases are passed, failed and skipped.
- You can execute failed test case separately. For example.
- Suppose, you have five test cases, one method is written for each test case (Assume that the program is written using the main method without using testNG). When you run this program first, three methods are executed successfully, and the fourth method is failed. Then correct the errors present in the fourth method, now you want to run only fourth method because first three methods are anyway executed successfully. This is not possible without using TestNG.
- The TestNG provides an option, i.e., testing-failed.xml file in test-output folder. If you want to run only failed test cases means you run this XML file. It will execute only failed test cases.
Beside above concept, you will learn more on TestNG, like what are the Advantages of TestNG, how to create test methods using @test annotations, how to convert these classes into testing suite file and execute through the eclipse as well as from the command line.
- Generate the report in a proper format including a number of test cases runs, the number of test cases passed, the number of test cases failed, and the number of test cases skipped.
- Multiple test cases can be grouped more easily by converting them into testng.xml file. In which you can make priorities which test case should be executed first.
- The same test case can be executed multiple times without loops just by using keyword called 'invocation count.'
- Using testng, you can execute multiple test cases on multiple browsers, i.e., cross browser testing.
- The testing framework can be easily integrated with tools like Maven, Jenkins, etc.
- Annotations used in the testing are very easy to understand ex: @BeforeMethod, @AfterMethod, @BeforeTest, @AfterTest
Open your Eclipse IDE and follow the below-mentioned steps:
Step 2) Create Java project by clicking on new àJava Project
Step 3) In this step,
- Give project Name
- Choose execution environment
- Select Project layout option
- Click on "Finish" button.
When you click on finish button. The "TestProject" java project is created. The "TestProject" will look like this.
Step 4) Open that newly created project. You will able to see the "src" folder in Package Explorer.
- Right click project and select "New"
- Select option package
Step 5) In this step,
- Browse the src folder and select package
- Give the package name and
- Finally, click on finish button. (package Name->com.test)
After clicking on "finish" the project structure will look like this:
Step 6) In this step,
- Now, Right click on newly created package and
- Select option "class."
A new window will open.
Step 7) In this step,
- Give the "class" name
- Select modifiers
- Browse and select superclass java.lang.object
- Click on finish button.
Here, you are creating two classes ex: DemoA, DemoB.
First Create Class DemoA.
When you click on "Finish" Button. Then it will going to create class like this:
Similarly, create class DemoB, It will look like this:
Name class for Demo B as you did for Demo A.
When you click on "Finish" Button. Then it will going to create class like this:
Write the following code within these classes by adding TESTNG Library. (If you don't add now means you can add it later)
(For Installation of TestNG refer previous Classes)
Before Adding TestNG Library, the project structure will look like this:
In the above project structure, you are not able to see the TestNG library.
In this section, you will learn how to add TestNG Library.
Step 1) If you have installed TestNG library just click on Java Project--->Properties.
Step 2) Now in properties window,
- Click on Java Build Path
- Click on Libraries
- Click on Add Library.
Then you will get another window like this : In this window,
- Click on "TestNg" and then
- Click on "Next".
Then Click on "finish" button.
After this, write the following code.
For Class DemoA
- Code line: The first line i.e WebDriver driver = new FirefoxDriver();
This line will going to open the firefox browser. Here, WebDriver is an Interface which implements all the classes such as FirefoxDriver, ChromeDriver, etc. Here you are giving parent reference to the child class known as upcasting in java. You can also write like this.
FirefoxDriver driver = new FirefoxDriver();
- Code line: Driver.manage().window().maximize();
This line will going to maximize the Browser window.
- Code line: driver.get(https://www.google.co.in);
This line will enter the specified URL in the URL field. The alternative method you can use is navigate();
- Code line: This line will going to identify the "Google" search box and enter the data you sent using sendKeys method.
Output: The above program contains the errors, so it is not possible to execute.
Similarly for Class DemoB,
Step 3) errors means- The red color underlined words are the errors here. Because you have not yet added the jar file that contains these classes and interfaces. In order to remove the errors present in the above code add the corresponding jar file. Right click on the Java Project and Select "Properties".
Step 4) In the property window,
- Click on Java Build path on the left navigation pane.
- Click on the libraries tab,
- Click on the Add External JARs and Select the Selenium-standalone jar file
- Click on "OK" button.
After this step, all the errors will be removed automatically. If not then place the mouse over the code which is showing errors and import all necessary classes and interfaces.
For @test if it is still showing errors then place the mouse over there. It will show the possible options. Then click on add TestNG Library option. Similarly do it for other 'class' also.
After writing your code inside both the classes i.e. DemoA, DemoB go to the next step.
Step 5) In this step,
- Right-click on the Project and
- Select option folder to create a folder called 'lib' and paste testNG jar file and selenium jar file (selenium-server-standalone<version>).
(Testng and Selenium-server-standalone jar file need to be downloaded from web and store it inside your system. You need to manually go to that folder and copy these two jars and right click on 'lib' folder present in eclipse and click on paste)
Step 6) In this step,
1. Select the parent folder
2. Give the Folder name as 'lib' and (The primary purpose of adding these two jar files in the lib folder is that, while executing from the command prompt you can tell the compiler that the required jar files for the execution of the program are present in this location. If you want to execute testng.xml from eclipse then this lib folder is not at all required)
3. Click on 'finish' button
After creating a folder, next step will be converting our programs that are DemoA and DemoB into Testng.xml file.
Step 7) In this step,
- Select two java files present under the package and right click.
- Select the option called "testNG."
- Click on convert to "testNG".
A new window will open. In this window, enter details like
- Suite Name
- Test Name
- Class Selection
- Parallel Mode
- Click on "Finish" button
Then the testng.xml file created under the java project, and it will look like this.
(if you want to run that testng.xml file then Right click on that testng.xml file present on the left navigation pane in Package Explorer and click on run as TestNG Suite. )
The testng.xml suite file will look like this:
If you want to execute DemoA class first, then remove that complete line and add it before DemoB class like this:
After executing the testng.xml file, it will display the result in the following manner. (Since you have not written any executable statements using System.out.println() so it is not printing anything on the console window).
This is one of the ways you will execute test through the eclipse, if you want to execute the same testng.xml suite file that contains two class file such as DemoA, DemoB from the command prompt you need to follow the below steps.
Now open the command prompt and go to the Project workspace.
Step 1) First right click on the Java Project and Select the properties option. In properties window select option resource.
Step 2) Now when you click on "resource" link, present in the left navigation pane. It will show where exactly the project is stored
The project workspace is C:\Users\User\Desktop\Guru99\TestProject. So you are changing directory to that particular location.
Step 3) Then type the following command. (This command is generic command)
java –cp "path of lib folder present in workspace\*; path of bin folder present in project workspace; path of testng.jar file present in lib folder of project workspace" org.testng.TestNG testing.xml
but for our project you need to write the following command.
Java –cp "C:\Users\User\Desktop\Guru99\TestProject\lib\*; C:\Users\User\Desktop\Guru99\TestProject\bin" org.testng.TestNG testing.xml
Step 4) Then press the Enter. Both the classes DemoA and DemoB will start their execution. Finally, it will display the result on the command prompt.
If you want to execute only failed test cases through the Eclipse, then first refresh the project.
Step 1) Right click on the java project (Demo A and B). Select the refresh option or simply select the java project and press F5.
Step 2) Then you will able to see the test-output folder. In that folder, you have a file with name testng-failed.xml.
Step 3) Right-click on this file and click on run as and select the option called "testNG suite".
Suppose if you have three test cases if all the test cases are executed successfully means you are not able to see this folder under the test-output folder. This folder will appear only when one of the test case is failed. Then run this file, it will going to run only failed test cases.
Step 1) To run failed test cases in command prompt. Open the command prompt and go to the Project workspace.
My project workspace is C:\Users\User\Desktop\Guru99\TestProject. So you will be changing directory to that particular location.
Step 2) Type the following command. (This command is generic command)
java –cp "path of lib folder present in workspace\*; path of bin folder present in project workspace; path of testing.jar file present in lib folder of project workspace" org.testng.TestNG test-output/testing-failed.xml
For our project, you need to write the following command.
Java –cp "C:\Users\User\Desktop\Guru99\TestProject\lib\*; C:\Users\User\Desktop\Guru99\TestProject\bin" org.testng.TestNG test-output/testing-failed.xml
Step 3) Then press the Enter. It will going to run only failed classes and display corresponds to that class only.
In the above class i.e. DemoB just change the code like this.
The above code will not going to find any element. So, it will throw the exception. But in our program, you are not handling the exception so our program will stop executing the remaining code. Because of this reason, the ClassB will going to fail.
Report Generation in Selenium web driver
Report generation is very important when you are doing the Automation Testing as well as for Manual Testing.
- By looking at the result, you can easily identify how many test cases are passed, failed and skipped.
- By looking at the report, you will come to know what the status of the project is.
Selenium web driver is used for automating the web-application, but it won't generate any reports.
- The TestNG will generate the default report.
- When you execute testng.xml file, and refresh the project. You will get test-output folder in that folder.
- Right click on the emailable-report.html and select the option. Open with the web browser.
- Click on option "emailable-report.html"
- Click on option web browser
The output report of testng will look like below if both the classes are passed:
Consider the scenario in where you are intentionally failing the test case i.e. DemoB class. Then convert both the classes into testing.xml suite file and run it. Then the result will look like this. It will show the failed test cases.
This is result for DemoB class:
Similarly, result for the Class DemoA will look like this:
- Right click on the index.html from the project directory.
- Select option open with web browser option. It will display the result in the following order.
The result will look like this:
Along with these report generated methods, you can use object.properties file to store the system generated logs as well as user generated logs. But one of the simplest ways to store log information in testing is using Reporter Class.
Reporter is a class present in TestNG. It provides 4 different methods to store log information they are:
- Reporter.log(String s);
- Reporter.log(String s, Boolean logToStandardOut);
- Reporter.log(String s, int level);
- Reporter.log(String s, int level, Boolean logToStandardOut);
Create Two classes such as DemoA and DemoB and write the following code inside the classes.
For Class DemoA;
- The Code for DemoA is already explained above. Here you are using log method of Reporter class. (For implementing a reporting class, the class has to implement an org.testng.IReporter interface).
- The log method is a static method of Reporter class. So you are accessing that method through the Reporter class.
- The log method is used to store log information that is written inside the program. By looking at the log information, you will easily come to know where exactly the execution of the program is stopped.
For Class DemoB:
- Now, Create testng.xml file by selecting these two classes and
- Select run as and
- Click on the convert to testng.
- Then run this testng.xml file by selecting run as and select testng suite.
- Then refresh the project open the test-output folder.
Similarly, you will have an Output for Demo B project as well.
2. In test-output folder open the index.html. It will look like:
Click on reporter output. It will open logging info whatever written in the test methods.
Click on the Times. It will going to show how much time it took to run the test method present in class.
- TestNG is Automation Testing Framework which is inspired from the Junit and contains different annotations.
- TestNG generates the reports in its standard report,which contains the following things:
- How many test cases are there ?
- How many test cases are passed ?
- How many test cases are failed ?
- How many test cases are skipped ?
- Multiple test cases can be grouped easily and executed them by converting test classes into testing suite file.
- TestNG can be easily integrated with other third-party tools. It provides different features like assigning the priority to the test cases, execution of same test case multiple times using invocation count.
- If any of the test case fails while executing multiple test cases, you can execute that specific test case separately.
- The static method log of Reporter class can be used to store logging information which presents in or.testng.
This article is contributed by Sandeep Batageri