{"id":459,"date":"2024-06-12T07:45:08","date_gmt":"2024-06-12T07:45:08","guid":{"rendered":"https:\/\/istqblearning.com\/testing\/?page_id=459"},"modified":"2024-06-17T08:43:54","modified_gmt":"2024-06-17T08:43:54","slug":"create-selenium-python-script-login-form-with-a-wrong-password","status":"publish","type":"page","link":"https:\/\/istqblearning.com\/testing\/create-selenium-python-script-login-form-with-a-wrong-password\/","title":{"rendered":"Create Selenium Python script: login form with a wrong password"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-3-1024x576.png\" alt=\"\" class=\"wp-image-482\" srcset=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-3-1024x576.png 1024w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-3-300x169.png 300w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-3-768x432.png 768w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-3-1536x864.png 1536w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-3.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Copy and past the script below into login-form-with-a-wrong-password.py file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>from selenium.webdriver.common.by import By<br>from selenium import webdriver<br>from selenium.webdriver.support import expected_conditions<br>from selenium.webdriver.support.wait import WebDriverWait<br><br># Creates a new instance of the chrome driver.<br>driver = webdriver.Chrome()<br># Loads https:\/\/istqblearning.com web page in the current browser session<br>driver.get(\"https:\/\/istqblearning.com\")<br><br># Wait up to 30 seconds for the website to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, \"Home\")))<br><br># This account has been registered in https:\/\/istqblearning.com. You can register a new one.<br>username = \"tester\"<br># \"Test12345678\" password of the \"tester\" username is wrong. Valid is \"Test12345678@\"<br>password = \"Test12345678\"<br><br># Maximizes the current window that webdriver is using<br>driver.maximize_window()<br><br># Wait up to 30 seconds for the Log In menu to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, \"Log In\")))<br><br># Find an element given a By strategy and locator<br>driver.find_element(By.LINK_TEXT, \"Log In\").click()<br><br># Wait up to 30 seconds for the login form to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.ID, \"user_login\")))<br><br># Find an element given a By strategy and locator.<br>driver.find_element(By.ID, \"user_login\").send_keys(username)<br># Find an element given a By strategy and locator.<br>driver.find_element(By.ID, \"user_pass\").send_keys(password)<br><br>driver.find_element(By.ID, \"wp-submit\").click()<br><br># Wait up to 30 seconds for the login error message to appear<br>WebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.ID, \"login_error\")))<br><br># If we change to a different assertion value, running the test script will fail.<br># For example: we change<br># \"Error: The password you entered for the username tester is incorrect. Lost your password?\" to<br># \"Error: The password you entered for the username tester is incorrect. Lost your password? - Failed\"<br><br>assert (driver.find_element(By.XPATH, \"\/\/div[@id='login_error']\/p\").text ==<br>        \"Error: The password you entered for the username tester is incorrect. Lost your password?\")<br><br>driver.quit()<br><br>print(\"Running test script login-form-with-a-wrong-password successfully\")<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Step<\/strong><\/td><td>Description<\/td><\/tr><tr><td>1<\/td><td>Opens a Chrome browser<\/td><\/tr><tr><td>2<\/td><td>Loads the&nbsp;<a href=\"https:\/\/istqblearning.com\/\">https:\/\/istqblearning.com\/<\/a>&nbsp;website from the current browser<\/td><\/tr><tr><td>3<\/td><td>Waits up to 30 seconds for the website to display successfully<\/td><\/tr><tr><td>4<\/td><td>Maximizes the website window<\/td><\/tr><tr><td>5<\/td><td>Waits up to 30 seconds for the Log In menu to appear<\/td><\/tr><tr><td>6<\/td><td>Clicks on Log In menu of the website<\/td><\/tr><tr><td>7<\/td><td>Waits up to 30 seconds for the Login form to appear<\/td><\/tr><tr><td>8<\/td><td>Inputs username into Username or Email Address textbox<\/td><\/tr><tr><td>9<\/td><td>Inputs an invalid password into Password textbox<\/td><\/tr><tr><td>10<\/td><td>Clicks on Log In button on the login form<\/td><\/tr><tr><td>11<\/td><td>Wait up to 30 seconds for the login error message to appear<\/td><\/tr><tr><td>12<\/td><td>Assert the &#8220;Error: The password you entered for the username tester is incorrect. Lost your password?&#8221; message. If running the test script without any error, the behavior of logging works as expected.<\/td><\/tr><tr><td>13<\/td><td>Quits the current browser<\/td><\/tr><tr><td>14<\/td><td>Print the message in console \u201cRunning test script login-form-with-an-invalid-password successfully\u201d<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Scenario of the test script<\/figcaption><\/figure>\n\n\n\n<p>Here is the statement of the step 12<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>assert (driver.find_element(By.XPATH, \"\/\/div[@id='login_error']\/p\").text ==<br>        \"Error: The password you entered for the username tester is incorrect. Lost your password?\")<\/code><\/pre>\n\n\n\n<p>What is XPATH in Selenium?<\/p>\n\n\n\n<p>A HTML document can be considered as a XML document, and then we can use xpath which will be the path traversed to reach the element of interest to locate the element. The XPath could be absolute xpath, which is created from the root of the document. Example &#8211; \/html\/form\/input[1]. This will return the male radio button. Or the xpath could be relative. Example- \/\/input[@name=\u2018fname\u2019]. This will return the first name text box.<\/p>\n\n\n\n<p>Selenium provides support for these 8 traditional location strategies in WebDriver. This <a href=\"https:\/\/www.selenium.dev\/documentation\/webdriver\/elements\/locators\/\">https:\/\/www.selenium.dev\/documentation\/webdriver\/elements\/locators\/<\/a> link explains all of them in details.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-4-1024x576.png\" alt=\"\" class=\"wp-image-485\" srcset=\"https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-4-1024x576.png 1024w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-4-300x169.png 300w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-4-768x432.png 768w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-4-1536x864.png 1536w, https:\/\/istqblearning.com\/testing\/wp-content\/uploads\/2024\/06\/Khong-co-tieu-de-4.png 1920w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In this step, we use the Inspect feature of Chrome browser to get a value of an element in a website.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Copy and past the script below into login-form-with-a-wrong-password.py file. from selenium.webdriver.common.by import Byfrom selenium import webdriverfrom selenium.webdriver.support import expected_conditionsfrom selenium.webdriver.support.wait import WebDriverWait# Creates a new instance of the chrome driver.driver = webdriver.Chrome()# Loads https:\/\/istqblearning.com web page in the current browser sessiondriver.get(&#8220;https:\/\/istqblearning.com&#8221;)# Wait up to 30 seconds for the website to appearWebDriverWait(driver, 30).until(expected_conditions.visibility_of_element_located((By.LINK_TEXT, &#8220;Home&#8221;)))# This account [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-459","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages\/459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/comments?post=459"}],"version-history":[{"count":14,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages\/459\/revisions"}],"predecessor-version":[{"id":663,"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/pages\/459\/revisions\/663"}],"wp:attachment":[{"href":"https:\/\/istqblearning.com\/testing\/wp-json\/wp\/v2\/media?parent=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}