{"id":4586,"date":"2020-08-27T18:06:17","date_gmt":"2020-08-27T12:36:17","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=4586"},"modified":"2025-04-02T07:18:19","modified_gmt":"2025-04-02T11:18:19","slug":"database-testing-using-selenium","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/database-testing-using-selenium\/","title":{"rendered":"Database Testing using Selenium"},"content":{"rendered":"\n<p>Testing your web applications using Browser is limited in Selenium Webdriver. For Database Verification, you need to use JDBC (Java Database Connectivity) in Selenium Webdriver.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-is-spring-jdbc\/\">JDBC <\/a>is a Java Level API that manages joining to a database and allows us to execute SQL statements. It provides a connection between the Java Programming language and a wide range of databases. The JDBC API will provide the below following interfaces and classes.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Driver Manager<\/li>\n\n\n\n<li>Driver<\/li>\n\n\n\n<li>Connection<\/li>\n\n\n\n<li>Statement<\/li>\n\n\n\n<li>ResultSet<\/li>\n\n\n\n<li>SQLException<\/li>\n<\/ul>\n\n\n\n<p>You need to observe the following 3 steps while your Database testing using Selenium.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/pQLDToQ5we64BKMsdtorah4jCpOLo8-1hyYcIao7sUc7k92zl1mQakWEyxyPXpYRGPyjP--I6TxRgs3GBARe3VMqM03R7COJAFOmHb8iglkviqQxl4LErlfyjurRGQd3pQqR66Rd2m1k4XEiqA\" alt=\"SQL.PNG\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong> Making a connection to the Database<\/strong><\/h2>\n\n\n\n<p>The following is the syntax to make a connection to the database<\/p>\n\n\n\n<p>DriverManager.getConnection(URL, \u201cuserid\u201d, \u201cpassword\u201d)<\/p>\n\n\n\n<p>Here,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Userid is the username which is configured in the database<\/li>\n\n\n\n<li>The Password of the configured user<\/li>\n\n\n\n<li>URL is in the format jdbc:&lt;dbtype&gt;:\/\/ipaddress:portnumber\/db_name\u201d<\/li>\n\n\n\n<li>&lt;dbtype&gt;- The driver to the database which you are trying to connect. To connect to the SQL database this value will be \u201cSQL\u201d<\/li>\n<\/ul>\n\n\n\n<p>To connect for the database with name \u201cemployee\u201d in MYSQL the URL will be<\/p>\n\n\n\n<p><strong>bejdbc:mysql:\/\/localhost:3036\/employee<\/strong><\/p>\n\n\n\n<p>And to create a connection the code looks like<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/kdiCXdF1SQT0fJ1DQjJ_MHDLAcATgWivu6Vesw9-IsNbHITiWmxo5mQVZwxUW_atuV-nu8wqdbdII-2I54VJPtDe_Pv4ucHDjsh2QISGWadwZ4vAk85HP9hOGbhit12GgPUwaPBBCqkxOYWTJg\" alt=\"database.PNG\" title=\"\"><\/figure>\n\n\n\n<p>Using the code you need to load the JDBC Driver<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/M5sZdurPQBi6cNg2PUxaBN1ms97axEA8KHooVxl5aFUnTNQXKBJTyBAy0N4pfuae7BzS5QlU92WvgABNIEo7uQ1TtJSivS2g4e8v5HEMMxdd3TvUluztG1Y6CKMbtpA7gPqTq8QVYSlyfnxF7w\" alt=\"Class.PNG\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sending Queries to the Database<\/strong><\/h2>\n\n\n\n<p>You need to execute the queries once the connection is made. We use the Statement Object to send queries.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/FXtSctavUNfR_RVXbmYLxHi8iv84iqY1AD3ZMEzbhw_z375EedI6gh3mNutvVqAxCaoZJly4IVJIBNgXSeBeWsgVdpUqoCZVz9oHN74mtCJew-B_7CpQ_jPpkr5vEZDTwNTfBVRjoYiSqabppQ\" alt=\"Statement.PNG\" title=\"\"><\/figure>\n\n\n\n<p>To execute the SQL queries we use the executeQuery method once the statement object is created<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/iE7UPvXLyjwW9blqhIjePmZSm89DGKSaiRXHLrKoxgNMrE1ifAOeBQszQ8X4Cx3F8wq9bK65G7ATA9Sni-g13JTT1s545yiDhb7uX00dk4wPClBjdS4vf0qCy6socs0KVL9wmrXxYftld5qCpw\" alt=\"stmt.PNG\" title=\"\"><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong> Processing the results<\/strong><\/h2>\n\n\n\n<p>Results got from the executed query are stored in the ResultSet object.<\/p>\n\n\n\n<p>Java provides advanced methods to process the results. Below are the few methods that are listed.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Method Name<\/strong><\/td><td><strong>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Description<\/strong><\/td><\/tr><tr><td>int getInt()&nbsp;<\/td><td>This method will fetch the integer type data from the result set<\/td><\/tr><tr><td>String getString()<\/td><td>This method will fetch the string type data from the result set<\/td><\/tr><tr><td>double getDouble()<\/td><td>This method will fetch the double type data from the result set<\/td><\/tr><tr><td>Date getDate()<\/td><td>This method will fetch the Date type object from the result set<\/td><\/tr><tr><td>boolean next()<\/td><td>This method will move to the next record in the result set<\/td><\/tr><tr><td>boolean previous()<\/td><td>This method will move to the previous record in the result set<\/td><\/tr><tr><td>boolean first()<\/td><td>This method will move to the first record in the result set<\/td><\/tr><tr><td>boolean last()<\/td><td>This method will move to the last record in the result set<\/td><\/tr><tr><td>Boolean absolute(int rowNumber)<\/td><td>This method will move to the specific record in the result set<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Database Testing Example Using Selenium WebDriver and JDBC<\/strong><\/h2>\n\n\n\n<p><strong>Step 1: <\/strong>Install MYSQL Server and MYSQL Workbench from below links<\/p>\n\n\n\n<p>MYSQL Server: <a href=\"https:\/\/dev.mysql.com\/downloads\/mysql\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/dev.mysql.com\/downloads\/mysql\/<\/a><\/p>\n\n\n\n<p>MYSQL Workbench: <a href=\"https:\/\/dev.mysql.com\/downloads\/workbench\/\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/dev.mysql.com\/downloads\/workbench\/<\/a><\/p>\n\n\n\n<p><strong>JDBC Connection, Eclipse Setup, and Selenium WebDriver:<\/strong><\/p>\n\n\n\n<p>To create a JDBC connection, we need a driver class. The SQL connector driver class can be downloaded from the link<\/p>\n\n\n\n<p><em><span class=\"has-inline-color has-cyan-bluish-gray-color\">https:\/\/dev.mysql.com\/downloads\/connector\/j\/<\/span><\/em><\/p>\n\n\n\n<p>Once you download the zip file \u2018mysql-connector-java-5.1.49.zip\u2019, unzip the file to see the jar inside which is the actual driver file.<\/p>\n\n\n\n<p><strong>Follow below steps to setup eclipse for JDBC Connection:<\/strong><\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Right-click on the selenium project and navigate as \u2018Build Path\u2019 &gt; \u2018Configure Build Path\u2019<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Click on the \u2018Libraries\u2019 tab and click on the \u2018Add External JARs\u2026\u2019 button and select the path \u2018mysql-connector-java-5.1.39.jar\u2019 file as shown below and click on the Open button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/2EHZiLKrFSWGwO--Ahr3Nk4yOdFNSNoHg4_BI0xUsTNs04nysEAZhEkfKwPzG2c_IonmuDrOEnuBonLlr11r-IAaY3IsMOH3ZYs_zqlhYBF0WGNOfsjcgNwsLa1L8fuo4-Cd2xN_w5nM1382Xw\" alt=\"sqljar.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 3:<\/strong> Click on the Apply and Close button to finish the build path set up as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/uB35il-tJFxhcbTSA3cCfzmiiA11rcVhpsxpLZ4bivfvnn-QhZwfTHuQsVmplHvrybFERyltclInz7HFWuvSeMQrczEoZO9yZhi7ciGyz5SV-_k7hyAYhI6Ccl8b9SMaR-VByjqGJGuX9Tflvg\" alt=\"sqlapply.PNG\" title=\"\"><\/figure>\n\n\n\n<p><strong>Step 4:<\/strong> Execute the test script as shown below once the driver class is set up.<\/p>\n\n\n\n<p><strong>i): Create a Package (seleniumpackage)<\/strong><\/p>\n\n\n\n<p><strong>ii): Create a Class (SeleniumJDBCConnection)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>package<\/strong> seleniumpackage;\n\n<strong>import<\/strong> java.sql.Connection;\n<strong>import<\/strong> java.sql.Statement;\n<strong>import<\/strong> org.openqa.selenium.WebDriver;\n<strong>import<\/strong> org.openqa.selenium.firefox.FirefoxDriver;\n<strong>import<\/strong> java.sql.ResultSet;\n<strong>import<\/strong> java.sql.DriverManager;\n<strong>import<\/strong> java.sql.SQLException;\n\n<strong>public<\/strong> <strong>class<\/strong> SeleniumJDBCConnection {\n<strong>public<\/strong> <strong>static<\/strong> <strong>final<\/strong> String <strong><em>QUERY<\/em><\/strong> = \"select * from TESTING;\";\n<strong>public<\/strong> <strong>static<\/strong> <strong>void<\/strong> main(String&#91;] args) <strong>throws<\/strong> ClassNotFoundException, SQLException {\nString baseWebUrl = \"\";\nString expectedWebsiteTitle = \"\";\nClass.<em>forName<\/em>(\"com.mysql.jdbc.Driver\");\/\/Load MySQL JDBC driver\n\/\/Create Connection to DB\nConnection con = DriverManager.<em>getConnection<\/em>(\"jdbc:mysql:\/\/localhost:3306\/webtesting\", \"MySQLDatabase\",\"softwaretestingclass\");\nStatement stmt = con.createStatement(); \/\/Create Statement Object.\nResultSet rs= stmt.executeQuery(<strong><em>QUERY<\/em><\/strong>); \/\/ Execute the SQL Query. Store results in ResultSet.\n<strong>while<\/strong> (rs.next()){ \/\/ While Loop iterates through all data present in a table.\nbaseWebUrl = rs.getString(1);\nexpectedWebsiteTitle = rs.getString(2);\n}\ncon.close();\nSystem.<strong><em>out<\/em><\/strong>.println(\"baseWebUrl: \"+baseWebUrl);\nSystem.<strong><em>out<\/em><\/strong>.println(\"expectedWebsiteTitle: \"+expectedWebsiteTitle);\nSystem.<em>setProperty<\/em>(\"webdriver.gecko.driver\",\"F:\\\\drivers\\\\geckodriver.exe\");&nbsp;&nbsp;\nWebDriver driver = <strong>new<\/strong> FirefoxDriver();\n\nString actualWebsiteTitle = \"\";\ndriver.get(baseWebUrl);\nactualWebsiteTitle = driver.getTitle();\n<strong>if<\/strong> (actualWebsiteTitle.contentEquals(expectedWebsiteTitle)){\nSystem.<strong><em>out<\/em><\/strong>.println(\"Test has Passed!\");\n} <strong>else<\/strong> {\nSystem.<strong><em>out<\/em><\/strong>.println(\"Test has Failed!\");\n}\ndriver.close();\n}\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Make a Database connection<\/strong><\/h2>\n\n\n\n<p>After loading the JDBC driver and with the help of ConnectionManager we are making the MySQL DB connection. This class requires a URL, username, and password as shown in the above program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Execute SQL queries in Database via JDBC connection<\/strong><\/h3>\n\n\n\n<p>We have created a SQL query to select a record (here URL and title) from the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Database\" data-type=\"link\" data-id=\"https:\/\/en.wikipedia.org\/wiki\/Database\" rel=\"nofollow noopener\" target=\"_blank\">database<\/a> testing. We are getting this SQL in the pre-compiled state with the help of the Statement interface and the \u2018createStatement\u2019 method of the Connection interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Process the return result set from the database<\/strong><\/h3>\n\n\n\n<p>Here we execute the pre-compiled SQL with the help of \u2018executeQuery\u2019 method which returns a result set that contains URL and title.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>WebDriver Web Testing<\/strong><\/h3>\n\n\n\n<p>By using the URL fetched from the database to load the website using Firefox WebDriver. The driver is fetching the title of the page that URL loads. This title is compared with the title retrieved from the database.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Output<\/strong><\/h2>\n\n\n\n<p>Since the title retrieved from the database matches with the title fetched from the website test script will print the test has passed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In conclusion, database testing using Selenium, enhanced with JDBC, enables testers to validate data integrity directly alongside UI testing. This combined approach ensures the front-end interactions reflect accurately in the backend, making it crucial for applications that rely on precise data consistency. <\/p>\n\n\n\n<p>Enrolling in a <a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium certification<\/a> course can help you master these skills, equipping you with the knowledge to perform comprehensive database and UI testing, essential for delivering high-quality, reliable applications.<\/p>\n\n\n\n<p>Ready to enhance your testing skills with database integration in Selenium? Join H2K Infosys Selenium automation testing course and master the essential techniques for combining UI and database testing using JDBC. Gain hands-on experience with industry experts and take your testing expertise to the next level. Enroll today to ensure your applications meet the highest standards of quality and data integrity!<\/p>\n\n\n\n<p>Join H2K Infosys today and gain practical experience with expert-led guidance and real-world projects. Whether new to Selenium or looking to expand your testing abilities, our course provides everything you need to excel in database testing, from foundational knowledge to advanced techniques. Don\u2019t miss out on the opportunity to elevate your testing career enroll now!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Testing your web applications using Browser is limited in Selenium Webdriver. For Database Verification, you need to use JDBC (Java Database Connectivity) in Selenium Webdriver. JDBC is a Java Level API that manages joining to a database and allows us to execute SQL statements. It provides a connection between the Java Programming language and a [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":6878,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[156,128,541,45,448],"class_list":["post-4586","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-automation-testing","tag-database-testing","tag-jdbc","tag-selenium","tag-selenium-webdriver"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4586","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=4586"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/4586\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/6878"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=4586"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=4586"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=4586"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}