Author - StudySection Post Views - 1,405 views
Selenium Database

Steps to Connect a Database to Selenium Test Cases

Introduction

Every application requires a database like My SQL, Oracle, or any other database to store all its data. Selenium Webdriver is used for examining web applications. We can perform many operations like submitting information and some times retrieving information and validate them. Here we will discuss the steps to connect a database to a selenium test case.

How to connect a database to selenium test cases

Here are the following steps to connect your database with selenium:

  1. Download MYSQL and create a database
  2. JDBC connection with Java programming
  3. Selenium code to integrate with JDBC and retrieving data from the database.

Step1:
Firstly, download the MYSQL from this link: https://dev.mysql.com/downloads/windows/installer/

sql-installer

After installing the Mysql Workbench and made a connection by filling credentials to create Workbench access. Once we successfully log in to the workbench then we create a database and table to insert the data.

Step2:

In this step, we integrate the database with JDBC API and for that, we need to download the Jar File.We can download this jar file directly from this link: “www.java2s.com/Code/Jar/m/Downloadmysqlconnectorjava5121jar.htm

Then we need to add this Jar file in eclipse.
Steps: project->properties->add external jars.

Step3:
Make connection with database from your project with this code:
On running this whole code we retrieve data from the database and automate our test case which is entering username and password on the web:


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class jdbcconection {

public static void main(String[] args) throws SQLException, ClassNotFoundException {

//Initialization of variables
String host="localhost";
String port= "3306";

//Loading the required MYSQL JDBC Driver class
Class.forName("com.mysql.jdbc.Driver");

//creating connection to a database
Connection con=DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/demo", "root", "root");

//Executing SQL query and fetching the result
Statement s=con.createStatement();

ResultSet rs=s.executeQuery("select * from credentials where scenario ='demo'");
while(rs.next())
{
WebDriver driver= new FirefoxDriver();
driver.get("https://login.salesforce.com");
//retrieving username and password from database
driver.findElement(By.xpath(".//*[@id='username']")).sendKeys(rs.getString("username"));
driver.findElement(By.xpath(".//*[@id='password']")).sendKeys(rs.getString("password"));

}
}
}

Now, This code executes the query and gets the username and password to login to the “https://login.salesforce.com” account.

As we have a username and password in the credential table:

demo

Result:

salesforce

Being the most extensively used JavaScript library, a jQuery certification will add enormous value to your skill-set. jQuery provides various functionalities to the developer in order to develop complex applications with ease and efficiency.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri