Comprehensive study of Selenium and Python's Web automation testing project actual combat

Table of contents

 Summary:

1. Install dependencies

2. Write test cases

3. Execute the test case

4 Conclusion


  Summary:

With the continuous development and updating of web applications, it becomes more and more important to ensure their quality and stability. To achieve this goal, web automation testing has become an essential part. This article will introduce a practical web automation testing project based on Selenium and Python to help readers better understand and apply these technologies.

1. Install dependencies

Before we begin, we need to install and configure the following dependencies:

  • Python 3.x
  • Selenium WebDriver
  • Chrome browser
  • ChromeDriver driver

2. Write test cases

We will write a simple test case to verify that the login form is working. First, we need to fire up the Chrome browser and open the test site:

from selenium import webdriver

# 启动Chrome浏览器
driver = webdriver.Chrome()

# 打开测试网站
driver.get("http://example.com/login")

Then, we'll fill in the username and password, and submit the login form:

# 填写用户名和密码
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
username.send_keys("john.doe")
password.send_keys("pa$$w0rd")

# 提交登录表单
submit_button = driver.find_element_by_css_selector("button[type='submit']")
submit_button.click()

Finally, we'll verify that the login was successful, and close the browser:

# 验证登录是否成功
assert "Welcome, John Doe!" in driver.page_source

# 关闭浏览器
driver.quit()

3. Execute the test case

To execute test cases, we can use Python's unittest framework. Create a file called tests.py and write the following code:

import unittest
from selenium import webdriver

class LoginTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get("http://example.com/login")

    def test_login(self):
        username = self.driver.find_element_by_name("username")
        password = self.driver.find_element_by_name("password")
        submit_button = self.driver.find_element_by_css_selector("button[type='submit']")

        username.send_keys("john.doe")
        password.send_keys("pa$$w0rd")
        submit_button.click()

        assert "Welcome, John Doe!" in self.driver.page_source

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

Run the following command at the command line to execute the test case:

python tests.py

4 Conclusion

Through this article, we have learned how to use Selenium and Python for actual web automation testing projects. This technique can be used to test various aspects of a website, including form filling, page navigation, data validation, and more. Hope this article helps you!

Automated test learning framework diagram:

Automated testing benefits:

 

 

Guess you like

Origin blog.csdn.net/Free355/article/details/130363441