In this tutorial, you will learn-

Create a Selenium Project

Prepare the Project

Step 1) In Eclipse, Create Java project by clicking on new Java Project

Step 2) In this step,

  1. Give project Name
  2. Choose execution environment
  3. Select Project layout option
  4. 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.

  1. Right click project and select "New"
  2. Select option package

Step 4) In this step,

  1. Browse the src folder and select package
  2. Give the package name and
  3. 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,

  1. Now, Right click on newly created package and
  2. Select option "class."

A new window will open.

Step 6) In this step,

  1. Give the "class" name
  2. Select modifiers
  3. Browse and select superclass java.lang.object
  4. 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,

  1. Click on Java Build Path
  2. Click on Libraries
  3. Click on Add Library.

Next

  1. Click on "TestNg" and then
  2. Click on "Next".

Then Click on "finish" button.

After this, write the following code.

For Class DemoA

Code Explanation:

  • Driver.manage().window().maximize();

This line will going to maximize the Browser window.

  • driver.get(https://www.google.co.in);

This line will enter the specified URL in the URL field.

  • driver.findElement(By.name("q")).sendKeys("Hi");

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,

  1. Click on Java Build path on the left navigation pane.
  2. Click on the libraries tab,
  3. Click on the Add External JARs and Select the Selenium-standalone jar file
  4. 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,

  1. Right-click on the Project and
  2. 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.

Convert and Execute Selenium Project to TestNG

Step 1) In this step,

  1. Select two java files present under the package and right click.
  2. Select the option called "testNG."
  3. Click on convert to "testNG".

Step 2) A new window will open. In this window, enter details like

  1. Location
  2. Suite Name
  3. Test Name
  4. Class Selection
  5. Parallel Mode
  6. 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.

Execute TestNG via Command Line

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

How to run only failed test cases

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.

Running testng-failed.xml file from Command line

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/testng-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/testng-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.

driver.findElement(By.name("a")).sendKeys("Bye");		

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.

Summary:

  • 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 is present in org.testng

This article is contributed by Sandeep Batageri

 

YOU MIGHT LIKE: