selenium编写web测试用例

测试用例:百度和有道;执行测试用例:RunTest.py

百度用例脚本:

from selenium import webdriver
import unittest
import time


class My_Tests(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.maximize_window()
        self.browser.implicitly_wait(5)
        #
        self.base_url = "http://www.baidu.com"

    # 测试用例
    def Test_BaiDu(self):
        browser = webdriver.Chrome()
        browser.get(self.base_url + "/")
        browser.find_element_by_xpath("//*[@id='kw']").clear()
        browser.find_element_by_xpath("//*[@id='kw']").send_keys("hello python")
        browser.find_element_by_xpath("//*[@id='su']").click()

        time.sleep(3)
        my_title = browser.title
        self.assertEqual(my_title, "hello python")

    def tearDown(self):
        # 退出
        self.browser.quit()


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

有道测试用例:

from selenium import webdriver
import time
import unittest


class My_Tests(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.maximize_window()
        self.browser.implicitly_wait(5)
        self.base_url = "http://www.youdao.com"

    def Test_YouDao(self):
        browser = webdriver.Chrome()
        browser.get(self.base_url + "/")
        browser.find_element_by_xpath("//*[@id='translateContent']").clear()
        browser.find_element_by_xpath("//*[@id='translateContent']").send_keys("您好")
        browser.find_element_by_xpath("//*[@id='form']/button").click()

        time.sleep(3)
        my_title = browser.title
        self.assertEqual(my_title, "您好")

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


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

执行测试用例脚本:

import unittest
from Test_202001_SeleniumT import Test_0103_01_Baidu
from Test_202001_SeleniumT import Test_0103_02_YouDao

# 构造测试集
suite = unittest.TestSuite()
# 添加测试用例
suite.addTest(Test_0103_01_Baidu.My_Tests("Test_BaiDu"))
suite.addTest(Test_0103_02_YouDao.My_Tests("Test_YouDao"))

if __name__ == "__main__":
    # 执行测试
    runner = unittest.TextTestRunner()
    runner.run(suite)

 

你可能感兴趣的:(Selenium自动化测试)