jeudi 17 janvier 2019

Variable doesn't update between tests

I'm using selenium and I have two tests in my unittest class (I'm following a tutorial). I'm using @classmethod for setUpClass. Each test alone works but both together, like in my code below, doesn't. It appears the variable products in the second test remain the same like in first test, so the lenght is 3 instead of 1. This happens even if I change the name of the variable. So I have an AssertionError: 1 != 3. It probabily need time to loading the results of the second search

Why so? What can I do?

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)

Aucun commentaire:

Enregistrer un commentaire