如何在python中使用Selenium测试嵌套页面?例如,测试Add-to-Cart场景并签出?

2024-10-01 02:29:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用Selenium编写python测试来测试一个场景,在这个场景中,我将一个产品添加到我的购物车,然后转到我的购物车并继续点击Checkout按钮。基本上我想从A页(主页)转到B页(立即购买页),它嵌套在A页中,然后转到C页(结帐页),它依次位于B页中

我的“立即购买”页面测试工作正常。它从我的Basepage继承,并且'Buy Now'页的测试成功通过。问题是当我试图使签出从“立即购买”页面继承时。测试失败了。 请记住,不需要滚动。元素应该已经在视图中了。

import time
from src.base_page import BasePage


class BuyNowPage(BasePage):

    twoYrBundle = 'radio-btn-label-0'
    oneYrBundle = 'radio-btn-label-1'
    gateLockOnly = 'radio-btn-label-2'
    addToCart_button = 'AddToCart-product-template'
    continueShopping_button = 'Continue shopping'

    # Checking all existing buying options on Buy Now page are click-able
    def check_first_buying_option_click(self):
        self._driver.find_element_by_id(BuyNowPage.twoYrBundle).click()

    def check_second_buying_option_click(self):
        self._driver.find_element_by_id(BuyNowPage.oneYrBundle).click()

    def check_third_buying_option_click(self):
        self._driver.find_element_by_id(BuyNowPage.gateLockOnly).click()

    # Checking Add To Cart button click
    def check_add_1yr_bundle_to_cart(self):
        self._driver.find_element_by_id(BuyNowPage.oneYrBundle).click()
        time.sleep(2)
        self._driver.find_element_by_id(BuyNowPage.addToCart_button).click()
        time.sleep(2)
import time
from src.home_page import HomePage
from src.buy_now_page import BuyNowPage
from src.test_template import TestTemplate


class TestBuyNowPage(TestTemplate):

    # Check Buy Now page buying options are click-able
    def test_buy_buttons_available(self):
        home_page = HomePage(self.driver)
        home_page.check_buy_now_button_click()
        buy_now_page = BuyNowPage(self.driver)
        buy_now_page.check_first_buying_option_click()
        time.sleep(3)
        buy_now_page.check_second_buying_option_click()
        time.sleep(3)
        buy_now_page.check_third_buying_option_click()
        time.sleep(3)

    def test_adding_1yr_bundle_to_cart(self):
        home_page = HomePage(self.driver)
        home_page.check_buy_now_button_click()
        buy_now_page = BuyNowPage(self.driver)
        buy_now_page.check_second_buying_option_click()
        buy_now_page.check_add_1yr_bundle_to_cart()

以上代码成功通过

import time
from src.home_page import HomePage
from src.base_page import BasePage
from src.buy_now_page import BuyNowPage


class CheckoutPage(BuyNowPage):

    checkout_button = 'checkout'

    # Check checkout button
    def check_checkout_button(self):
        self._driver.find_element_by_link_text(HomePage.buyNow_button).click()
        self._driver.find_element_by_id(BuyNowPage.addToCart_button).click()
        self._driver.find_element_by_link_text(CheckoutPage.checkout_button).click()
from src.home_page import HomePage
from src.buy_now_page import BuyNowPage
from src.checkout_page import CheckoutPage
from src.test_template import TestTemplate


class TestCheckOutPage(TestTemplate):

    # Check Checkout button works
    def test_checkout_button(self):
        home_page = HomePage(self.driver)
        home_page.check_buy_now_button_click()
        buy_now_page = BuyNowPage(self.driver)
        buy_now_page.check_second_buying_option_click()
        buy_now_page.check_add_1yr_bundle_to_cart()
        check_out_page = CheckoutPage(self.driver)
        check_out_page.check_checkout_button()

我得到以下错误。它似乎找不到“Checkout”按钮元素,但我认为问题可能是其他原因? 失败(错误=1)

错误

Traceback (most recent call last):
  File "C:\Python\lib\unittest\case.py", line 59, in testPartExecutor
    yield
  File "C:\Python\lib\unittest\case.py", line 615, in run
    testMethod()
  File "C:\Users\xxxx\PycharmProjects\ChromeWebTest\tests\test_checkout_page.py", line 16, in test_checkout_button
    check_out_page.check_checkout_button()
  File "C:\Users\xxxx\PycharmProjects\ChromeWebTest\src\checkout_page.py", line 15, in check_checkout_button
    self._driver.find_element_by_link_text(CheckoutPage.checkout_button).click()
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"checkout"}
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64)



Assertion failed

Process finished with exit code 1

Assertion failed

断言失败


Tags: fromimportselfsrccheckdriverpagebutton