RE: Backend testing using selenium

Answered

How to do backend testing using selenium. how to run SQL queries in selenium

anu17 Doctor Asked on September 2, 2014 in Selenium WebDriver.
Add Comment
2 Answers
Best answer

Hi Anu,

Selenium does not support your backend related tests, All you can do is write a backend interface in your tests using the database APi of the language in which you are writing your test code.

In Java you write something like this to connect to DB
 

 
 public Connection getConnection() throws SQLException {
Connection conn = null;
 Properties connectionProps = new Properties();
 connectionProps.put("user", this.userName);
 connectionProps.put("password", this.password);
if (this.dbms.equals("mysql")) {
 conn = DriverManager.getConnection(
 "jdbc:" + this.dbms + "://" +
 this.serverName +
 ":" + this.portNumber + "/",
 connectionProps);
 } else if (this.dbms.equals("derby")) {
 conn = DriverManager.getConnection(
 "jdbc:" + this.dbms + ":" +
 this.dbName +
 ";create=true",
 connectionProps);
 }
 System.out.println("Connected to database");
 return conn;
 }
 

 

Also to execute your sql query you can use this

 

 public static void viewTable(Connection con, String dbName)
 throws SQLException {
Statement stmt = null;
 String query = "select COF_NAME, SUP_ID, PRICE, " +
 "SALES, TOTAL " +
 "from " + dbName + ".COFFEES";
 try {
 stmt = con.createStatement();
 ResultSet rs = stmt.executeQuery(query);
 while (rs.next()) {
 String coffeeName = rs.getString("COF_NAME");
 int supplierID = rs.getInt("SUP_ID");
 float price = rs.getFloat("PRICE");
 int sales = rs.getInt("SALES");
 int total = rs.getInt("TOTAL");
 System.out.println(coffeeName + "\t" + supplierID +
 "\t" + price + "\t" + sales +
 "\t" + total);
 }
 } catch (SQLException e ) {
 JDBCTutorialUtilities.printSQLException(e);
 } finally {
 if (stmt != null) { stmt.close(); }
 }
 }

 

Thanks
Virender

Professor Answered on September 2, 2014.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.