gpt4 book ai didi

python - 变量不会在测试之间更新

转载 作者:行者123 更新时间:2023-11-28 21:19:13 26 4
gpt4 key购买 nike

我正在使用 selenium,并且在我的 unittest 类中有两个测试(我正在学习教程)。我正在为 setUpClass 使用 @classmethod。每个测试单独使用,但两者一起使用,就像我下面的代码一样,不起作用。看起来第二次测试中的变量产品与第一次测试中的一样,所以长度是 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)

最佳答案

我更改了我的代码:

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)

关于python - 变量不会在测试之间更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54245819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com