How to run data driven testing using testNG in selenium



  • I am using testNG framework and tried to import data from excel sheet in my local. If I run the code as simple java application then its running but with testNG its not

    public class NewTest {
       public String strFirstName ="";
       public String strLastName="";
       public String strEmail="";
       @Laycee    public static void main (String args[]) throws InterruptedException{
          try {
                 // Open the Excel file
                 // Access the required test data sheet
                 FileInputStream fi = new FileInputStream("E:/som/Book2.xlsx");
                 XSSFWorkbook workbook = new XSSFWorkbook(fi);
                 XSSFSheet sheet = workbook.getSheetAt(0);
    
                 // Loop through all rows in the sheet
                 // Start at row 1 as row 0 is our header row
                 for(int count = 0;count192.168.1.33:8081/serosoft-academia-testing/");
                 System.out.println("Page title is: " + driver.getTitle());
                 driver.findElement(By.id("button-1055-btnIconEl")).click();
                 driver.findElement(By.id("ext-comp-1079-btnInnerEl")).click();
                 driver.findElement(By.id("button-1089-btnIconEl")).click();
                 driver.findElement(By.id("ext-gen1804")).click(); // Enquiry Type
                 Thread.sleep(1000);
                 driver.findElement(By.xpath(".//*[@id='boundlist-1534- 
                    listEl']/ul/li[1]")).click();
                 Thread.sleep(2000);
                 driver.findElement(By.id("ext-gen1812")).click(); // Referer type
                 Thread.sleep(2000);
                 driver.findElement(By.xpath(".//*[@id='boundlist- 
                    1536']/div/ul/li[1]")).click();
                 Thread.sleep(2000);
                 driver.findElement(By.id("ext-gen1822")).click(); // Enquiry Registered
                 // by
                 Thread.sleep(2000);
                 driver.findElement(By.xpath(".//*[@id='boundlist- 
                    1538']/div/ul/li[1]")).click();
                 Thread.sleep(2000);
                 driver.findElement(By.id("textfield-1482-inputEl")).sendKeys(strFirstName); // Enquirer
    
                 // first
                 // name
                 driver.findElement(By.id("textfield-1485-inputEl")).sendKeys(strLastName); // enquirer
    
                 // last
                 // name
                 driver.findElement(By.id("ext-gen1825")).click(); // country
                 Thread.sleep(2000);
                 driver.findElement(By.xpath(".//*[@id='boundlist-1540']/div/ul/li[1]")).click();
                 Thread.sleep(2000);
                 driver.findElement(By.id("ext-gen1827")).click(); // city
                 Thread.sleep(2000);
                 driver.findElement(By.xpath(".//*[@id='boundlist-1542']/div/ul/li[1]")).click();
                 driver.findElement(By.id("textfield-1497-inputEl")).sendKeys(strEmail); // email
    

    And here is the error I am getting: error pic



  • I can see several problems in your code.

    Paramaterized @BeforeSuite method with no data provider.Your @BeforeSuite method (main()) takes a parameter (args), but you have not told TestNG how to provide a value for that. That's what that exception message means.

    Calling test methods. When you run under TestNG, your @BeforeSuite method calls your test method. That seems like a problem. TestNG wants to call your test on your behalf. You don't have to call it yourself. And you almost certainly should not call it yourself.

    Even if it works, when you call your test method in the @BeforeSuite method, any failure in the test would look like a failed configuration method. Probably not what you want. And certainly not what TestNG wants.

    Static @BeforeSuite method. Also, you've put a @BeforeSuite annotation on a static method. I don't know for sure, but I suspect that TestNG does not like that.

    How to parameterize a test in TestNG. The proper way to provide data for tests is to use a @DataProvider method. See Section 5.6 Parameters in the TestNG documentation, especially section 5.6.2 about @DataProvider methods.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2