Skip to main content

Database connection and selenium with examples

This illustrates how to connect MySQL database with Selenium using Java bindings.  Make sure mysql-connector-java-5.x.x-bin.jar in your buildpath.

Configure MySQL

1| Download an install MySQL.
2| Create a database and table. e.g.,
create database one;
create table pets (name VARCHAR(20), owner VARCHAR(20), species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
3| Follow up whatever queries intended
4| Now, try to connect the schema with java via selenium webdriver for retrieving the table values.


private String dbvalue;
private String dbUrl = "jdbc:mysql://localhost:3306/one"; //Here, one is the schema/db
private String username="root";
private String password="yourpassword";
private String dbClass = "com.mysql.jdbc.Driver";

@Test
public void connectdb() throws Exception {  
driver.get("www.xyz.com");
      
try {
Class.forName(dbClass);
Connection con = DriverManager.getConnection (dbUrl,username,password);
Statement stmt = con.createStatement();

//insert values into the table | Get values from the already created schema/db
String insertvalues1 = "Insert into pets values('dashund', 'Sams', 'DOG', 'M' ,'2012-05-12', '2012-05-12');";
String insertvalues2 = "Insert into pets values('brownie', 'Sams', 'CAT', 'F' ,'2013-07-22', '0000-00-00');";
String insertvalues3 = "Insert into pets values('parrot', 'Sams', 'BIRD', 'F' ,'2011-12-02', '0000-00-00');";
stmt.executeUpdate(insertvalues1);
stmt.executeUpdate(insertvalues2);
stmt.executeUpdate(insertvalues3);

String query = "select * from pets";
System.out.println(query);
ResultSet rst = stmt.executeQuery(query);

//In a loop
while (rst.next()) {
dbvalue =rst.getString(1);
System.out.println(dbvalue);
driver.findElement(By.id(Value)).sendKeys(dbvalue);
driver.findElement(By.id(Value)).submit();
        driver.get("www.xyz.com");
}

con.close();

} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
}

Comments

Popular posts from this blog

Rational Functional Tester: Calling RFT scripts from using xml tags

Hello, This will help to call RFT scripts in xml, if anyone using 'ant' this will help them to embed code to invoke RFT scripts. <java classname="com.rational.test.ft.rational_ft" fork="true" maxmemory="1024m">         <classpath>         <fileset dir="C:\Program Files\IBM\SDP\FunctionalTester\bin\">         <include name="rational_ft.jar" />         </fileset>         </classpath>         <jvmarg line="-Drational_ft.install.dir=&quot;C:\Program Files\IBM\SDP\jdk_\jre\bin&quot;" />         <arg line="-rt.bring_up_logviewer false -datastore &quot;D:\RFT-Dev\Project&quot; -playback RFTTestSet5.testcase4.TestScript1" />         <arg line="-args -scriptArg ${scriptValue}" />         </java>

Rational Functional Tester: Creating and Reading Datapool cell value in RFT

Hello, Hope this will help for my friends who is trying to use data pool for dynamic values.If you want to use any run time values in between scripts then you will be looking for the functions to read/write/create datapool cells dynamically. /************ Dp row count *************************/ IDatapoolIterator  ite; ite.dpCurrent().getEquivalenceClass().getRecordCount(); /***************** to set cell value **************/ IDatapoolIterator it; ((DatapoolCell) it.dpCurrent().getCell("RunTimeSheetForContainers")).setCellValue("Y"); /*********** to load CSV files (csv to Datapool)*******************************/ public IDatapoolIterator LoadCSV(String Sname)       {             File fname=getFileName(Sname);             IDatapool dp=DatapoolUtilities. loadCSV (fname, "," , true );             IDatapoolIterator ii=dpFactory().open(dp, null );             ii.dpInitialize(dp);             return

Questions on Selenium Grid

What is Selenium Grid? Selenium Grid is a part of the Selenium Suite that specializes on running multiple tests across different browsers, operating systems, and machines in parallel . Selenium Grid has 2 versions – the older Grid 1 and the newer Grid 2. We will only focus on Grid 2 because Grid 1 is gradually being deprecated by the Selenium Team. Selenium Grid uses a hub-node concept where you only run the test on a single machine called a   hub , but the execution will be done by different machines called   nodes .  When to Use Selenium Grid? You should use Selenium Grid when you want to do either one or both of following : ·          Run your tests against different browsers, operating systems, and machines all at the same time. This will ensure that the application you are testing is fully compatible with a wide range of browser-OS combinations. ·          Save time in execution of your t