How is Selenium used to write automated test scripts?

How is Selenium used to write automated test scripts? It provides many testing tools and APIs that can interact with browsers, simulate user operations, and check various aspects of web pages. Below are some steps that can help you write Selenium automated test scripts.

  1. Install the Selenium library and browser driver

  First, you need to install Selenium library and browser driver, such as Chrome driver or Firefox driver. You can find the corresponding driver and installation guide on the Selenium official website or browser website.

  2. Create a test script

  Next, you can write test scripts using your favorite programming language (such as Python, Java, etc.). In the test script, you need to use the Selenium library's API to interact with the browser. Here is a Python example that uses the Selenium library and the Chrome browser driver to launch a browser and open a Google website:

  from selenium import webdriver

  #Create Chrome browser object

  driver = webdriver.Chrome()

  # Open Google website

  driver.get('https://www.google.com/')

  3. Position page elements

  In the test script, you need to locate various elements on the page, such as text boxes, buttons, links, etc. The Selenium library provides a variety of locators, such as ID, class, XPath, CSS selectors, etc. Here is a Python example that uses XPath to locate the Google search box and enter text into it:

  from selenium import webdriver

  from selenium.webdriver.common.by import By

  #Create Chrome browser object

  driver = webdriver.Chrome()

  # Open Google website

  driver.get('https://www.google.com/')

  # Locate the search box and enter text

  search_box = driver.find_element(By.XPATH, '//input[@name="q"]')

  search_box.send_keys('Selenium automated testing')

  4. Perform operations

  In the test script, you need to perform various actions such as clicking, entering text, selecting drop-down lists, etc. The Selenium library provides a variety of operation methods, such as click(), send_keys(), select_by_value(), etc. Here is a Python example that enters text into the Google search box and clicks the search button:

  from selenium import webdriver

  from selenium.webdriver.common.by import By

  #Create Chrome browser object

  driver = webdriver.Chrome()

  # Open Google website

  driver.get('https://www.google.com/')

  # Locate the search box and enter text

  search_box = driver.find_element(By.XPATH, '//input[@name="q"]')

  search_box.send_keys('Selenium automated testing')

  # Locate the search button and click

  search_button = driver.find_element(By.XPATH, '//button[@type="submit"]')

  search_button.click()

  5. Verification results

  In the test script, you need to verify that the test results are correct. The Selenium library provides a variety of methods to check various elements and attributes on the page, such as text, attribute, title, etc. Here is a Python example that verifies whether a Google search results page contains the keyword "Selenium":

  from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC

  #Create Chrome browser object driver = webdriver.Chrome()

  # Open Google website driver.get('https://www.google.com/')

  # Locate the search box and enter the text search_box = driver.find_element(By.XPATH, '//input[@name="q"]')search_box.send_keys('Selenium automated testing')

  # Locate the search button and click search_button = driver.find_element(By.XPATH, '//button[@type="submit"]')search_button.click()

  # Wait for the search results page to load wait = WebDriverWait(driver, 10)wait.until(EC.presence_of_element_located((By.XPATH, '//div[@id="search"]')))

  # Verify whether the page contains the keyword "Selenium" search_results = driver.find_element(By.XPATH, '//div[@id="search"]')assert 'Selenium' in search_results.text

  The above is a basic Selenium automated test script example. Of course, depending on the specific testing needs and scenarios, you need to write different test scripts to verify various functions and interactions of the website. Writing efficient and reliable automated test scripts requires continuous practice and experience accumulation.

Guess you like

Origin blog.csdn.net/spasvo_dr/article/details/132586563