两次测试之间变量不会更新

时间:2019-01-17 23:35:06

标签: python selenium testing class-method

我正在使用硒,我的unittest类中有两个测试(我正在学习一个教程)。我正在将@classmethod用于setUpClass。每个测试都可以单独使用,但不能一起使用,就像下面的代码一样。似乎第二个测试中的变量乘积与第一个测试中的相同,因此长度为3而不是1。即使我更改了变量的名称,也会发生这种情况。所以我有一个AssertionError: 1 != 3。可能需要时间来加载第二次搜索的结果

为什么呢?我该怎么办?

import unittest
from selenium import webdriver

class SearchTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # create a new Firefox session
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page
        cls.driver.get("http://demo-store.seleniumacademy.com/")
        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 names
        # displayed currently on result page using
        # find_elements_by_xpath method
        products = self.driver.find_elements_by_xpath\
            ("//h2[@class='product-name']/a")
        print('products', products)
        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 names displayed
        # currently on result page using
        # find_elements_by_xpath method
        products = self.driver.find_elements_by_xpath\
            ("//h2[@class='product-name']/a")
        print('products',products)
        self.assertEqual(1, len(products))

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

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

1 个答案:

答案 0 :(得分:1)

我更改了代码:

import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class SearchTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # create a new Firefox session
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page
        cls.driver.get("http://demo-store.seleniumacademy.com/")
        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
        title = "phones"
        self.search_field.send_keys(title)
        self.search_field.submit()

        # get all the anchor elements which have product names
        # displayed currently on result page using
        # find_elements_by_xpath method
        try:
            element = WebDriverWait(self.driver, 10).until(
                EC.title_contains(title)
            )
        finally:
            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
        title = "salt shaker"
        self.search_field.send_keys(title)
        self.search_field.submit()

        # get all the anchor elements which have
        # product names displayed
        # currently on result page using
        # find_elements_by_xpath method
        try:
            element = WebDriverWait(self.driver, 10).until(
                EC.title_contains(title)
            )
        finally:
            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)

现在,我等待两种方法,直到页面标题更改。它有效,但是您怎么看?这是解决问题的好方法吗?

编辑:在评论之后,我再次更改了代码:

import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class SearchTests(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        # create a new Firefox session
        cls.driver = webdriver.Firefox()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page
        cls.driver.get("http://demo-store.seleniumacademy.com/")
        #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
        title = "phones"
        self.search_field.send_keys(title)
        self.search_field.submit()

        # get all the anchor elements which have product names
        # displayed currently on result page using
        # find_elements_by_xpath method
        element = WebDriverWait(self.driver, 10).until(
            EC.title_contains(title)
        )
        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
        title = "salt shaker"
        self.search_field.send_keys(title)
        self.search_field.submit()

        # get all the anchor elements which have
        # product names displayed
        # currently on result page using
        # find_elements_by_xpath method
        element = WebDriverWait(self.driver, 10).until(
            EC.title_contains(title)
        )
        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)