【问题标题】:Unit test case not working in python selenium单元测试用例在 python selenium 中不起作用
【发布时间】:2021-06-26 06:35:31
【问题描述】:

我尝试下面的单元测试用例,它没有打开网络浏览器并直接打印“完成”消息。

from selenium import webdriver
import unittest


class GoogleSearch(unittest.TestCase):

    # driver = None

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome(executable_path='../Drivers/chromedriver')
        cls.driver.maximize_window()

    def test_search(self):
        self.driver.get('https://www.google.com')
        self.driver.find_element_by_name("q").send_keys("facebook")
        self.driver.implicitly_wait(10)
        self.driver.find_element_by_name("btnI").click()
        # driver.find_element_by_name("btnI").send_keys(Keys.ENTER)

    @classmethod
    def tearDownClass(cls):
        # driver.implicitly_wait(5)
        cls.driver.quit()
        cls.print("test completed")


print("done")

【问题讨论】:

    标签: python selenium unit-testing testing automation


    【解决方案1】:

    定义完你的单元测试后,你必须调用它。使用unittest.main() 调用测试。

    from selenium import webdriver import unittest
    
    
    class GoogleSearch(unittest.TestCase):
    
        @classmethod
        def setUpClass(cls):
            cls.driver = webdriver.Chrome(executable_path='../Drivers/chromedriver')
            cls.driver.maximize_window()
    
        def test_search(self):
            self.driver.get('https://www.google.com')
            self.driver.find_element_by_name("q").send_keys("facebook")
            self.driver.implicitly_wait(10)
            self.driver.find_element_by_name("btnI").click()
            # driver.find_element_by_name("btnI").send_keys(Keys.ENTER)
    
        @classmethod
        def tearDownClass(cls):
            # driver.implicitly_wait(5)
            cls.driver.quit()
            cls.print("test completed")
    
    if __name__ == '__main__':
        unittest.main()  # <- runs your unittest
        print("done")
    

    【讨论】:

    • 它可以工作,但最后一条语句“完成”没有打印
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多