Check if the radio button and drop-down list options are saved correctly in the database.
Language: Java
Framework: JUnit using JDBC
1//Assuming the following:2//1. Database is already set up and we have access to it3//2. The radio button and drop-down list are already present on the UI and can be interacted with4//3. We already have the JDBC driver in our classpath56import java.sql.Connection;7import java.sql.DriverManager;8import java.sql.PreparedStatement;9import java.sql.ResultSet;10import java.sql.SQLException;1112import org.junit.After;13import org.junit.Before;14import org.junit.Test;15import static org.junit.Assert.*;1617public class DatabaseTest {1819 private Connection connection;2021 private final String RADIO_BUTTON = "Option A";22 private final String DROPDOWN_OPTION = "Option 2";2324 @Before25 public void setUp() {26 //Establish database connection27 String dbURL = "jdbc:mysql://localhost:3306/DatabaseName";28 String username = "username";29 String password = "password";30 try {31 connection = DriverManager.getConnection(dbURL, username, password);32 } catch (SQLException e) {33 e.printStackTrace();34 }35 }3637 @Test38 public void testRadioButtonAndDropDownListOptions() {39 //Assuming we have already navigated to the page with the radio button and drop-down list options40 //Select the radio button41 //Assuming radio button has an id of "radioButton"42 WebElement radioButton = driver.findElement(By.id("radioButton"));43 radioButton.click();4445 //Select an option from the drop-down list46 //Assuming drop-down list has an id of "dropDownList"47 WebElement dropDownList = driver.findElement(By.id("dropDownList"));48 Select select = new Select(dropDownList);49 select.selectByVisibleText("Option 2");5051 //Save the selections in the database52 try {53 PreparedStatement ps = connection.prepareStatement("INSERT INTO options (radio_button, dropdown_list) VALUES (?, ?)");54 ps.setString(1, RADIO_BUTTON);55 ps.setString(2, DROPDOWN_OPTION);56 ps.executeUpdate();57 } catch (SQLException e) {58 e.printStackTrace();59 }6061 //Check if the options were saved correctly in the database62 try {63 PreparedStatement ps = connection.prepareStatement("SELECT * FROM options WHERE radio_button = ? AND dropdown_list = ?");64 ps.setString(1, RADIO_BUTTON);65 ps.setString(2, DROPDOWN_OPTION);66 ResultSet rs = ps.executeQuery();67 assertTrue(rs.next());68 } catch (SQLException e) {69 e.printStackTrace();70 }71 }7273 @After74 public void tearDown() {75 //Close the database connection76 try {77 if (connection != null) {78 connection.close();79 }80 } catch (SQLException e) {81 e.printStackTrace();82 }83 }84} 8586//Connecting to a remote client with desired capabilities87//Assuming the following:88//1. The remote client is already set up and running89//2. We have the address and port number of the remote server90//3. We want to use Chrome browser9192import org.openqa.selenium.WebDriver;93import org.openqa.selenium.chrome.ChromeDriver;94import org.openqa.selenium.remote.DesiredCapabilities;95import org.openqa.selenium.remote.RemoteWebDriver;96import java.net.URL;9798public class RemoteTest {99100 public static void main(String[] args) {101 //Assuming ChromeDriver is already in our classpath102 System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");103104 //Assuming the address and port number of the remote server is "http://remote.server.address:4444/wd/hub"105 //Specify desired capabilities106 DesiredCapabilities capabilities = DesiredCapabilities.chrome();107108 WebDriver driver = null;109 try {110 //Connect to remote driver with desired capabilities111 driver = new RemoteWebDriver(new URL("http://remote.server.address:4444/wd/hub"), capabilities);112 } catch (Exception e) {113 e.printStackTrace();114 }115116 //Assuming we want to navigate to a certain URL117 driver.navigate().to("https://www.google.com");118 //Do other actions on the remote driver119120 //Close the remote driver121 driver.quit();122 }123}
Language: Python
Framework: Pytest
1### Assumptions:2- The radio button and drop-down list options are available on a web page.3- The web page is developed using HTML, CSS, and JavaScript.4- The web page has a form that allows the user to select the radio button and drop-down list options.5- The form submits the selected options to a server-side application that stores the data in a database.67```python8import pytest9from selenium import webdriver10from selenium.webdriver.common.by import By11from selenium.webdriver.support.ui import Select12from selenium.webdriver.support.ui import WebDriverWait13from selenium.webdriver.support import expected_conditions as EC1415# Instantiate driver object for local execution16driver = webdriver.Chrome()1718# Instantiate driver object for remote execution (add the desired capabilities)19'''20from selenium.webdriver.common.desired_capabilities import DesiredCapabilities21driver = webdriver.Remote(22 command_executor='http://<remote_client_ip>:<port>/wd/hub',23 desired_capabilities=DesiredCapabilities.CHROME24)25'''2627# Test case for database testing28@pytest.mark.Database29def test_save_options_to_database():30 # Open the web page with the form31 driver.get('<web_page_url>')3233 # Find the radio button element and select it34 radio_button = driver.find_element(By.ID, 'radio_button_id')35 radio_button.click()3637 # Find the drop-down list element and select an option38 dropdown = Select(driver.find_element(By.ID, 'dropdown_id'))39 dropdown.select_by_visible_text('Option 1')4041 # Submit the form42 submit_button = driver.find_element(By.ID, 'submit_button_id')43 submit_button.click()4445 # Wait for the confirmation message to appear46 confirmation_message = WebDriverWait(driver, 10).until(47 EC.visibility_of_element_located((By.ID, 'confirmation_message_id'))48 )4950 # Check if the confirmation message is displayed correctly51 assert confirmation_message.text == 'Options saved successfully.'5253 # Check if the selected options are saved correctly in the database54 # (Assuming the options are saved in a 'options' table with columns 'radio_button' and 'dropdown')55 cursor = get_database_connection().cursor()56 cursor.execute('SELECT radio_button, dropdown FROM options;')57 result = cursor.fetchone()58 assert result[0] == True # Assuming 'radio_button' is a boolean column59 assert result[1] == 'Option 1' # Assuming 'dropdown' is a text column6061# Close the driver object62driver.quit()63```
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