Tuesday, 23 July 2019

Database testing in selenium - DB validation


Database Testing In Selenium


So Database connectivity or Database testing is the most important and frequently used task that we all need to perform in selenium - Data base connectivity.

DB connection is always required to make automation script cover maximum of the task such as validation and verification of data entered or stored in DB.

In Java we can use JDBC driver which help to connect to other db and execute queries and fetch the required data. In Selenium we will take the same approach with JDBC.

Lets see how can we connect to DB in selenium with below code. Before starting if want to know more about selenium click here -> Learn Selenium


JDBC connection ->


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import org.apache.log4j.Logger;

public class DBr {

static Logger dbLogger= Logger.getLogger(DBHelper.class);

public static Connection getConnection() throws AccountException
{

Connection conn=null;

try {
FileInputStream fin=new FileInputStream("database.properties");
Properties prop= new Properties();
prop.load(fin);
dbLogger.info("Property file loaded..");
System.out.println("properties loaded..");
String driver= prop.getProperty("driver");
String url= prop.getProperty("url");
String user= prop.getProperty("username");
String passwd= prop.getProperty("password");
Class.forName(driver);
conn=DriverManager.getConnection(url,user,passwd);
dbLogger.info("database connected...");
return conn;
} catch (SQLException e) {
dbLogger.error(e.getMessage());
throw new AccountException(e.getMessage());
} catch (FileNotFoundException e) {
dbLogger.error(e.getMessage());
throw new AccountException(e.getMessage());
} catch (IOException e) {
dbLogger.error(e.getMessage());
throw new AccountException(e.getMessage());
} catch (ClassNotFoundException e) {
dbLogger.error(e.getMessage());
throw new AccountException(e.getMessage());
}
}
public long insertAccountHolderDetails(AccountHolder acc) 
throws AccountException{
try {
Connection conn= getConnection();
String sql= "insert into AccountHolder values(custId_seq.nextval,?,?,?,?)";
PreparedStatement ps= conn.prepareStatement(sql);
ps.setString(1,acc.getFirstName());
    ps.setString(2,acc.getLastName());
    ps.setLong(3,acc.getMobilenumber());
    ps.setString(4, acc.getAddress());
    int rec=ps.executeUpdate();
    if(rec==1){
    sql= "select custId_seq.currval from abc
    ps= conn.prepareStatement(sql);
    ResultSet rs= ps.executeQuery();
    if(rs.next())
    {
    long custId= rs.getLong(1);
    dbLogger.info("record inserted :" +custId);
    return custId;
    }
    }
    
} catch (SQLException e) {
dbLogger.error(e.getMessage());
throw new AccountException(e.getMessage());
}
return 0;
}

}







Validate table in selenium,database testing in selenium,database testing,selenium,selenium database testing,selenium webdriver,database,selenium,selenium database connection using jdbc,database connection,database testing selenium,java database connection,database connection in java 1.8


Sunday, 23 December 2018

How to Identify Table in a webpage in a easiest way using Xpath

Identify Table in a webpage using Xpath and click a required Cell in a table


Hello Everyone , Today we will be learning about how we can identify a table or many table present in a webpage easiest way just using XPATH.


First lets Start with Basics.

//table : It will identify all the table present in a webpage.
//thead : It will identify header of a particular table.
//tbody : It will identify body of a particular table.
//tr : Selects the row.
//td: Selects the cell.
//th : Selects the header cell. 


 For any query how wewrite a Xpath please look into my previous post : 
How to get Xpath of elements in webdriver


 Now lets suppose we have to identify a table in a website.if only one table is present in the website the our task is easy as we don't have to uniquily identify the table as only one table is present in the current webpage. In this case following Xpath will work if we want to click or get text of a particular cell.

                                               Xpath : //table/tbody/tr[2]/td[2]




The above xpath will get the cell value of 2nd row and 2nd column in table body. Similarly we can identify any cell by giving the index value of row and column as per requirement.

Now , Task really starts when we have more than one table in a web page and we really struggle to identify the table which we are looking for. So for that index help us to get it done very easily.  


Suppose we are having two table in a webpage, so for identifying them we have to take the index of each table. Generally we have index of 1st table in webpage as [1] and index of second table as [2]. See below example:


                                                  Xpath: //table[index]


Here we have used index as 2 to identify table 2 in a webpage.once we have selected the table all other operations will remain same to identify cell, column or row in a table.

Hope you understand how we can identify different table in a webpage using this simple Xpath trick.