This test case checks that two factor authentication is properly implemented and that customers are prompted to provide an additional form of identification for added security.
Language: Java
Framework: Selenium 4
1//Assuming driver is already set up and web page is loaded23//Locate the login button on the page and click it4driver.findElement(By.xpath("//button[contains(text(),'Log in')]")).click();56//Enter login credentials7driver.findElement(By.id("input-email")).sendKeys("example@email.com");8driver.findElement(By.id("input-password")).sendKeys("examplePassword");910//Click on login button11driver.findElement(By.xpath("//button[contains(text(),'Log in')]")).click();1213//Assuming that 2FA is implemented after successful login and that OTP is generated and sent to the customer's email14//We will use a hardcoded OTP as we don't have access to the customer's email in this test environment15String OTP = "123456";1617//Assuming that 2FA implementation requires entering the OTP on a separate page18//Locate input field for OTP and enter the hardcoded OTP19driver.findElement(By.id("input-otp")).sendKeys(OTP);2021//Click on Submit22driver.findElement(By.xpath("//button[contains(text(),'Submit')]")).click();2324//Assert that the user is redirected to the homepage after successful 2FA implementation25Assert.assertEquals("Homepage", driver.getTitle());2627//Assuming that the test environment is set up to use a remote client with desired capabilities28//Connecting to remote client with desired capabilities29RemoteWebDriver remoteDriver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox());30//Replace "firefox" with the desired browser name3132//Now transfer the above code into the desired automation testing framework, such as TestNG or JUnit.
Language: Python
Framework: Selenium 4
1from selenium import webdriver2from selenium.webdriver.common.by import By3from selenium.webdriver.support.ui import WebDriverWait4from selenium.webdriver.support import expected_conditions as EC56#Assuming Shopify URL below7url = "https://www.shopify.com/"89#Assuming the button for 2FA implementation, change as necessary10two_factor_auth_button = "button-text"1112#Assuming Shopify webpage has the user's phone number on file for 2FA13phone_num_field = "input[name=phone_number]"1415#Assuming Shopify webpage has a button to verify the phone number provided16phone_num_verify_button = "button[name=verify]"1718#Assuming that upon verification of the phone number, users are then prompted to enter an additional code sent to their device19auth_code_field = "input[name=auth_code]"2021#Assuming Shopify webpage has a button to submit the authentication code provided22auth_code_submit_button = "button[name=submit]"2324#Assuming a valid username and password have been inputted25username = "myusername"26password = "mypassword"2728#Connect to local driver29driver = webdriver.Firefox()3031#Connect to remote client with desired capabilities, comment out if necessary32'''33from selenium.webdriver.common.desired_capabilities import DesiredCapabilities34capabilities = DesiredCapabilities.FIREFOX.copy()35capabilities['platform'] = "WINDOWS"36capabilities['version'] = "10"37driver = webdriver.Remote(38 desired_capabilities=capabilities,39 command_executor="http://localhost:8080/wd/hub"40)41'''4243#Navigate to Shopify website44driver.get(url)4546#Click login button and enter credentials47login_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "login")))48login_button.click()49username_field = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "username")))50username_field.send_keys(username)51password_field = driver.find_element_by_name("password")52password_field.send_keys(password)53login_submit_button = driver.find_element_by_css_selector("button[type='submit']")54login_submit_button.click()5556#Click 2FA implementation button57two_fa_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, two_factor_auth_button)))58two_fa_button.click()5960#Enter phone number and verify61phone_num = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, phone_num_field)))62phone_num.send_keys("+1234567890")63phone_num_verify_button = driver.find_element_by_css_selector("button[name='verify']")64phone_num_verify_button.click()6566#Enter authentication code and submit67auth_code = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, auth_code_field)))68auth_code.send_keys("1234")69auth_code_submit_button = driver.find_element_by_css_selector("button[name='submit']")70auth_code_submit_button.click()7172#Close browser73driver.quit()
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