Check if data is committed to the database only when the operation is successfully completed.
Language: Java
Framework: JUnit using JDBC
1//Assuming a database named "testdb" is created and JDBC driver is added to the project classpath.23import java.sql.*;45import org.junit.Test;6import org.junit.Assert;78public class DatabaseTest {910 @Test11 public void checkDataCommit() {12 13 String url = "jdbc:mysql://localhost/testdb"; //Assuming the database is running on local machine14 String user = "username"; //Assuming a valid username for the database15 String password = "password"; //Assuming the password for the database16 Connection conn = null;17 Statement stmt = null;18 ResultSet rs = null;19 20 try {21 //Connect to the database22 conn = DriverManager.getConnection(url, user, password);23 24 //Create a test table for the scenario25 stmt = conn.createStatement();26 stmt.execute("CREATE TABLE test_table (id INTEGER NOT NULL, name VARCHAR(20))");27 28 //Insert data into the table29 stmt.executeUpdate("INSERT INTO test_table (id, name) VALUES (1, 'John')");30 31 //Commit the transaction32 conn.commit();33 34 //Verify if the data is committed to the database35 rs = stmt.executeQuery("SELECT * FROM test_table WHERE id = 1");36 Assert.assertTrue(rs.next());37 38 } catch (SQLException e) {39 e.printStackTrace();40 } finally {41 //Close the resources42 try {43 if (rs != null) rs.close();44 if (stmt != null) stmt.close();45 if (conn != null) conn.close();46 } catch (SQLException e) {47 e.printStackTrace();48 }49 }50 }51 52}535455//To connect to remote client with desired capabilities56//Assuming the driver location and the capabilities are provided5758import org.openqa.selenium.WebDriver;59import org.openqa.selenium.remote.DesiredCapabilities;60import org.openqa.selenium.chrome.ChromeDriver;61import org.openqa.selenium.remote.RemoteWebDriver;62import java.net.URL;6364public class RemoteDriverTest {6566 public static void main(String[] args) throws Exception {67 68 //Create desired capabilities69 DesiredCapabilities capabilities = DesiredCapabilities.chrome();70 71 //Create remote webdriver72 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);73 74 //Navigate to a web page75 driver.get("https://www.google.com/");76 77 //Perform some actions78 //...79 80 //Close the webdriver81 driver.quit();82 }83 84}
Language: Python
Framework: Pytest
1# Assumption: A database connection and a successful operation has been established.23import pytest4import mysql.connector56def test_data_commit():7 # Connecting to database8 db = mysql.connector.connect(9 host="localhost",10 user="yourusername",11 password="yourpassword",12 database="yourdatabase"13 )14 15 # Creating cursor16 cursor = db.cursor()17 18 # Inserting data19 try:20 query = "INSERT INTO yourtable (id, name, age) VALUES (%s, %s, %s)"21 values = (1, 'John', 25)22 cursor.execute(query, values)23 db.commit()24 except:25 db.rollback()26 27 # Checking if data is committed28 cursor.execute("SELECT * FROM yourtable WHERE id=1")29 data = cursor.fetchone()30 31 assert data[1] == 'John' and data[2] == 2532 33 # Closing database connection34 db.close()3536# Code to connect to remote client with desired capabilities37# from selenium import webdriver38# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities3940# capabilities = DesiredCapabilities.CHROME.copy()41# capabilities['platform'] = "WINDOWS"42# capabilities['version'] = "10"43# capabilities['goog:chromeOptions'] = {'args': ['--disable-gpu']}4445# driver = webdriver.Remote(46# command_executor='http://127.0.0.1:4444/wd/hub',47# desired_capabilities=capabilities48# )
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