0

I am new to SELENIUM TESTNG, i need to create a method contains two parameters
one i am sending the parameter form testng xml and the other i am passing from where the method is called.

I want to have a reusable code to check which DB connection i want to use and the same method uses the query to be executed.

@Parameters({"env"})

@Test(dataProvider = "query")

public static int DataBase(String env,String query) throws SQLException, InstantiationException, 
        IllegalAccessException, ClassNotFoundException {

    Class.forName("net.sourceforge.jtds.jdbc.Driver");

    switch (env) {
     case "ENV1":
         con = DriverManager.getConnection("ENV1 CON");
         break;
     case "ENV2":
         con = DriverManager.getConnection("ENV2 Con");
         break;
    default:
            break;
    }   
    Statement statement = con.createStatement();

    // We use Sybase specific select getdate() query to return date
    ResultSet rs = statement.executeQuery(query);


    if (rs.next()) {
        S1 = rs.getInt(1);
        S2 = rs.getString(2);
        S3 = rs.getString(3); 
        System.out.println(S1+" "+S2+" "+S3);
    }
    rs.close();
    statement.close();
    con.close();

    return S1;
}

}

Could you please let know how to achieve this.

  • What error you are getting? – Pratik Jan 28 '20 at 19:15
  • i am trying to call it in a different test when i try to call the function it is asking to pass both the parameters where one of the parameter is coming from the testng xml file and the calling method i am sending the query – Sudheendra Jan 28 '20 at 19:26
  • Can you show which function is it? – Pratik Jan 28 '20 at 19:30
  • public class Test { public void t1(){ DB.DataBase(env, query) } } – Sudheendra Jan 28 '20 at 20:02
  • @Test() public void flightNum_DBvalidate() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{ String data1 = DB.DataBase("Select stmt"); – Sudheendra Jan 28 '20 at 21:07
  • @Sudheendra can you let me know what is exactly your requirement? You want to reuse DataBase() function in another test but it asking for another parameter env. If you can attach testng.xml would be helpful. – Muzzamil Jan 28 '20 at 21:22
  • You shouldn't need to pass the value from the XML into the method as a parameter, you should be able to use the method in the link above to retrieve that value from within the method. – JeffC Jan 28 '20 at 21:43

1 Answers1

0

i used the below method to call the test and it worked for me

@Test(priority=2)

@Parameters({"env",query"})
public void DBvalidate(String env, String query) 

{

 query = "select * from emp";

 String data1 = DB.DataBase(env, query);

}

and it worked fine for me