Check if the correct data is getting saved in the database upon a successful page submit.
Language: Java
Framework: JUnit using JDBC
1import org.junit.Test;2import java.sql.Connection;3import java.sql.DriverManager;4import java.sql.PreparedStatement;5import java.sql.ResultSet;6import java.sql.SQLException;78public class DatabaseTest {910 //Assuming the following database details11 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";12 static final String DB_URL = "jdbc:mysql://localhost/test";1314 //Assuming database credentials15 static final String USER = "username";16 static final String PASS = "password";1718 @Test19 public void testDataSaveInDatabase() {2021 Connection conn = null;22 PreparedStatement pstmt = null;23 ResultSet rs = null;2425 try {26 // Register JDBC driver27 Class.forName(JDBC_DRIVER);2829 // Open a connection30 conn = DriverManager.getConnection(DB_URL, USER, PASS);3132 // create query33 String sql = "INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)";34 pstmt = conn.prepareStatement(sql);3536 // add data to the query 37 pstmt.setString(1, "data1");38 pstmt.setString(2, "data2");39 pstmt.setString(3, "data3");4041 // execute query42 pstmt.executeUpdate();4344 // read data back from the database45 sql = "SELECT column1, column2, column3 FROM table_name";46 pstmt = conn.prepareStatement(sql);47 rs = pstmt.executeQuery();4849 // check if correct data is saved in the database50 while(rs.next()){51 String column1 = rs.getString("column1");52 String column2 = rs.getString("column2");53 String column3 = rs.getString("column3");5455 //assuming data1, data2, data3 was saved in the database56 if(!column1.equals("data1") && !column2.equals("data2") && !column3.equals("data3")){57 throw new AssertionError("Data is not saved correctly in the database");58 }59 } 60 } catch (SQLException se) {61 se.printStackTrace();62 throw new AssertionError("SQL Exception");63 } catch (Exception e) {64 e.printStackTrace();65 throw new AssertionError("Exception");66 } finally {67 // Close connections68 try {69 if (rs != null) rs.close();70 if (pstmt != null) pstmt.close();71 if (conn != null) conn.close();72 } catch (SQLException se) {73 se.printStackTrace();74 throw new AssertionError("SQL Exception");75 }76 }77 }78} 7980//Assuming code to connect to a remote client with desired capabilities - 81//This code uses a Selenium grid with Chrome browser8283import java.net.MalformedURLException;84import java.net.URL;85import org.openqa.selenium.Platform;86import org.openqa.selenium.remote.DesiredCapabilities;87import org.openqa.selenium.remote.RemoteWebDriver;88import org.junit.Test;8990public class RemoteTest {9192 @Test93 public void test() throws MalformedURLException {9495 //Assuming the following desired capabilities96 DesiredCapabilities capabilities = DesiredCapabilities.chrome();97 capabilities.setPlatform(Platform.WINDOWS);98 capabilities.setVersion("87.0");99100 //Assuming the remote web driver URL101 URL url = new URL("http://localhost:4444/wd/hub");102103 RemoteWebDriver driver = new RemoteWebDriver(url, capabilities);104105 // Add your selenium code here106 driver.get("https://www.google.com");107 driver.quit();108 }109}
Language: Python
Framework: Pytest
1# Assuming the database is MySQL and the submit form is located at http://localhost:8000/submit_form2# Using local driver 3import mysql.connector4from selenium import webdriver5import pytest67@pytest.fixture(scope="module")8def driver():9 driver = webdriver.Chrome()10 yield driver11 driver.quit()1213def test_database_save(driver):14 # Navigate to form page15 driver.get("http://localhost:8000/submit_form")1617 # Inputting test data18 fullname_input = driver.find_element_by_name("fullname")19 fullname_input.send_keys("John Doe")20 email_input = driver.find_element_by_name("email")21 email_input.send_keys("johndoe@gmail.com")22 23 # Submitting form24 submit_button = driver.find_element_by_xpath("//button[text()='Submit']")25 submit_button.click()2627 # Establishing database connection28 db = mysql.connector.connect(29 host="localhost",30 user="root",31 password="password",32 database="test_database"33 )3435 # Retrieving and comparing submitted data from database36 cursor = db.cursor()37 cursor.execute("SELECT fullname, email FROM submissions ORDER BY id DESC LIMIT 1")38 result = cursor.fetchone()39 assert result[0] == "John Doe"40 assert result[1] == "johndoe@gmail.com"4142 # Closing database connection43 db.close()44 45 # Remote client with desired capabilities code46 # Assuming the remote webdriver URL is http://localhost:4444/wd/hub47 # and the browser used is Firefox with Windows 10 as OS4849from selenium import webdriver50from selenium.webdriver.remote.webdriver import WebDriver51from selenium.webdriver.common.desired_capabilities import DesiredCapabilities5253@pytest.fixture(scope="module")54def driver(request):55 desired_caps = DesiredCapabilities.FIREFOX.copy()56 desired_caps["platform"] = "WINDOWS"57 driver = webdriver.Remote(58 command_executor="http://localhost:4444/wd/hub",59 desired_capabilities=desired_caps60 )61 def fin():62 driver.quit()63 request.addfinalizer(fin)64 return driver65 66def test_database_save(driver):67 # Same test case code as before68 # ...
Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing