Steps to follow when writing the script in TestNG:

As this is a very first TestNG program, I would like to go step by step, so it would be easy for you to learn the steps and understand easily. From next example onwards I will give the complete code.

  • Create a class called OpenGoogle in seleniumWithTestNG package.
package seleniumWithTestNG;

public class OpenGoogle {

}
  • Now create a method called openGoogle with the below contents.
package seleniumWithTestNG;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class OpenGoogle {

       public void openGoogle(){

        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");  
        // Launch the FireFox browser.
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.google.com");

           }
}
  • Import the import org.testng.annotations.Test; and give @Test annotation at the top of the openGoogle method.
package seleniumWithTestNG;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class OpenGoogle {

       @Test
       public void openGoogle(){

        System.setProperty("webdriver.gecko.driver", "C:\\Users\\Elcot\\Desktop\\drivers\\gecko\\geckodriver.exe");  
        // Launch the FireFox browser.
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://www.google.com");

           }
}
  • Now the program is ready to run. Right click-> Run As-> TestNG Test. As expected, the program will start executing and it will open Google.com.

NOTE: Here we have not used main method, as we did in the previous examples. We are not going to run as JAVA applications here after, we will run it as TestNG Test. @Test annotation will be acting like main method.

I said, we will use TestNG because, we will get reports on the test runs. Where is the report then?. Look at your console tab in Eclipse.

The report above says that, openGoogle is PASSED, Failures: 0 and skips:0. We did not write any logic to get the Pass counts or other data. But still we got the data. Thansk to TestNG. This is why we are using TestNG. The applications of TestNG are enormous and we will look at those things one by one in the upcoming chapters.

results matching ""

    No results matching ""