生成一段商城项目的自动化测试代码
时间: 2024-06-02 17:02:36 浏览: 7
好的,请稍等,我正在使用ChitGPT为您生成一段商城项目的自动化测试代码。
以下是由ChitGPT生成的商城项目的自动化测试代码:
```
import pytest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class TestShoppingCart:
@pytest.fixture(scope="session")
def driver(self):
driver = webdriver.Chrome()
driver.maximize_window()
yield driver
driver.quit()
def test_add_to_cart(self, driver):
driver.get("https://www.example.com/shop")
search_input = driver.find_element_by_name("search")
search_input.send_keys("product name")
search_input.send_keys(Keys.RETURN)
assert "Search Results" in driver.title
products = driver.find_elements_by_class_name("product")
for product in products:
if "product name" in product.text:
product.click()
break
add_to_cart_button = driver.find_element_by_class_name("add-to-cart")
add_to_cart_button.click()
cart_icon = driver.find_element_by_class_name("cart-icon")
assert "1" in cart_icon.text
def test_remove_from_cart(self, driver):
driver.get("https://www.example.com/cart")
checkout_button = driver.find_element_by_class_name("checkout-button")
assert checkout_button.text == "Checkout"
remove_buttons = driver.find_elements_by_class_name("remove-button")
product_title = None
for remove_button in remove_buttons:
product_title = remove_button.parent.find_element_by_class_name("product-title").text
if "product name" in product_title:
remove_button.click()
break
assert product_title not in driver.page_source
```
希望这个自动生成的测试代码能够满足您的需求。如有不足之处,请您指出。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)