Selenium:是setUpClass(),tearDownClass(),@ classmethod装饰器让每个测试共享一个浏览器实例吗?

时间:2019-03-11 09:57:26

标签: python-3.x selenium

我是硒的新鲜人。这是我的两个测试文件,第一个包含2个测试用例,如果运行它,则两个测试仅打开1个Chrome会话。 第二个包含3个测试用例,但每个测试打开1个Chrome会话。

从书中开始,由于我对setUpClass()使用@classmethod装饰器,所以将tearDownClass设置为类级别,因此文件中的所有测试应该只有1个浏览器会话。如果我的理解错误,请纠正我...

->第一个文件(searchtests_with_class_methods.py)

import unittest
from selenium import webdriver

class SearchTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # create a new Chrome session
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigation to the application home page
        cls.driver.get("http://demo-store.seleniumacademy.com/")
        # ?don't know why need this title here
        cls.driver.title

    def test_search_by_category(self):
        # get the search textbox
        self.search_field = self.driver.find_element_by_name("q")
        self.search_field.clear()

        # enter search keyword and submit
        self.search_field.send_keys("phones")
        self.search_field.submit()

        # get all the anchor elements which have product name displayed
        # currently on result page using find_element_by_xpath method
        products = self.driver.find_elements_by_xpath("//h2[@class='product-name']/a")
        self.assertEqual(3, len(products))

    def test_search_by_name(self):
        # get the search textbox
        self.search_field = self.driver.find_element_by_name("q")
        self.search_field.clear()

        # enter search keyword and submit
        self.search_field.send_keys("salt shaker")
        self.search_field.submit()

        # get all the anchor elements which have product name displayed
        # currently on result page using find_element_by_xpath method
        products = self.driver.find_elements_by_xpath("//h2[@class='product-name']/a")
        self.assertEqual(1, len(products))

    @classmethod
    def tearDownClass(cls):
        # close the browser window
        cls.driver.quit()
if __name__ == '__main__':
    unittest.main(verbosity=2)

->第二个文件(homepagetests.py)

import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from builtins import classmethod

class HomePageTest(unittest.TestCase):
    @classmethod
    def setUp(cls):
        # create a new Chrome session
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page
        cls.driver.get("http://demo-store.seleniumacademy.com/")

    def test_search_field(self):
        # check search field exists on Home page
        self.assertTrue(self.is_element_present(By.NAME, "q"))

    def test_language_option(self):
        # check language options dropdown on Home page
        self.assertTrue(self.is_element_present(By.ID, "select-language"))

    def test_shopping_cart_empty_message(self):
        # check content of My Shopping Cart block on Home page
        shopping_cart_icon = self.driver.\
            find_element_by_css_selector("div.header-minicart span.icon")
        shopping_cart_icon.click()

        shopping_cart_status = self.driver.\
            find_element_by_css_selector("p.empty").text

        self.assertEqual("You have no items in your shopping cart.",
                        shopping_cart_status)

        close_button = self.driver.\
            find_element_by_css_selector("div.minicart-wrapper a.close")
        close_button.click()

    @classmethod
    def tearDown(cls):
        # close the browser window
        cls.driver.quit()

    def is_element_present(self, how, what):
        """
        Utility method to check presence of an element on page
        :params how: By locator type
        :params what: locator value
        """

        try:
            self.driver.find_element(by=how, value=what)
        except NoSuchElementException as e:
            return False
        return True

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

我在Mac OS 10.13.6上使用Python3.7.1,Selenium'3.141.0'和Chrome 72.0.3626.121。 对此行为感到困惑...您能帮忙吗?

1 个答案:

答案 0 :(得分:0)

今天我刚刚发现了问题所在,实际上第二个文件中有一个错字,其中“ def tearDown(cls):”应为“ def tearDownClass(cls):”,因为我使用的是@classmethod装饰器。我真是个愚蠢的人...最后,所有测试仅通过一个浏览器会话通过。 万一将来有人遇到我同样的问题,我没有删除这个问题。