In this tutorial, you will learn-
- Create a Selenium Project
- Convert and Execute Selenium Project to TestNG
- Execute TestNG via Command Line
- How to run only failed test cases
- Running testng-failed.xml file from Command line
- Report Generation in Selenium web driver
Prepare the Project
Step 1) In Eclipse, Create Java project by clicking on new Java Project
Step 2) 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 3) 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 4) 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 5) In this step,
- Now, Right click on newly created package and
- Select option "class."
A new window will open.
Step 6) 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, When you click on "Finish" Button class will look like below-
Step 7) If you have installed TestNG library just click on Java Project--->Properties.
Step 8) Now in properties window,
- Click on Java Build Path
- Click on Libraries
- Click on Add Library.
- Click on "TestNg" and then
- Click on "Next".
Then Click on "finish" button.
After this, write the following code.
For Class DemoA
This line will going to maximize the Browser window.
This line will enter the specified URL in the URL field.
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,
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 8) 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 9) 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 10) 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
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
After creating a folder, next step will be converting our programs that are DemoA and DemoB into Testng.xml file.
Step 1) In this step,
- Select two java files present under the package and right click.
- Select the option called "testNG."
- Click on convert to "testNG".
Step 2) 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. )
Step 3) 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:
Step 4) 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.
You need to find the location of the Project Space
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 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