import pytest from playwright.sync_api import sync_playwright, expect import allure import os import sys import configparser # 添加项目根目录到 Python 路径 current_file_path = os.path.abspath(__file__) project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(current_file_path)))))) sys.path.insert(0, project_root) # 配置参数 class Config: """测试配置类""" PAGE_LOAD_WAIT = 5 ELEMENT_WAIT = 3 ACTION_WAIT = 2 FINAL_WAIT = 5 # 读取配置文件 def get_config(): config = configparser.ConfigParser() config_file = os.path.join(project_root, 'base_framework', 'base_config', 'config.ini') config.read(config_file, encoding='utf-8') return config @allure.feature('Switch4 Page') class TestSwitch4Page: """Switch4 页面自动化测试用例""" @classmethod def setup_class(cls): """测试类设置,只执行一次""" config = get_config() cls.switch4_url = config.get('QA', 'switch4') cls.playwright = sync_playwright().start() cls.browser = cls.playwright.chromium.launch( headless=False, slow_mo=300 ) cls.context = cls.browser.new_context() cls.page = cls.context.new_page() @classmethod def teardown_class(cls): """测试类清理,只执行一次""" print(f"测试完成,{Config.FINAL_WAIT}秒后关闭浏览器...") import time time.sleep(Config.FINAL_WAIT) cls.context.close() cls.browser.close() cls.playwright.stop() @allure.title("测试页面导航和加载状态") def test_page_navigation_and_load_state(self): """测试页面导航和加载状态""" with allure.step("导航到页面"): self.page.goto(self.switch4_url) with allure.step("等待页面加载完成"): self.page.wait_for_load_state('networkidle') with allure.step("验证页面标题"): # 直接检查标题是否包含 survey(不区分大小写) title = self.page.title() assert "survey" in title.lower(), f"页面标题不包含 survey,实际标题: {title}" with allure.step("验证页面可见性"): expect(self.page.locator('body')).to_be_visible() @allure.title("测试所有可交互元素") def test_all_interactive_elements(self): """测试所有可交互元素(按钮、输入框、链接)""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("测试 Back 按钮"): back_button = self.page.get_by_role('button', name='Back') expect(back_button).to_be_visible() expect(back_button).to_be_enabled() with allure.step("测试 Joyhub ID/邮箱输入框"): joyhub_input = self.page.get_by_role('textbox', name='Your Joyhub ID or registered email') expect(joyhub_input).to_be_visible() expect(joyhub_input).to_be_enabled() with allure.step("测试 Amazon Order ID 输入框"): amazon_input = self.page.get_by_role('textbox', name='Your Amazon Order ID') expect(amazon_input).to_be_visible() expect(amazon_input).to_be_enabled() with allure.step("测试 PayPal 按钮"): paypal_button = self.page.get_by_role('button', name='PayPal') expect(paypal_button).to_be_visible() expect(paypal_button).to_be_enabled() with allure.step("测试 Cash App 按钮"): cashapp_button = self.page.get_by_role('button', name='Cash App') expect(cashapp_button).to_be_visible() expect(cashapp_button).to_be_enabled() with allure.step("测试 Amazon Gift Card 按钮"): giftcard_button = self.page.get_by_role('button', name='Amazon Gift Card') expect(giftcard_button).to_be_visible() expect(giftcard_button).to_be_enabled() with allure.step("测试 PayPal/Venmo 邮箱输入框"): paypal_email_input = self.page.get_by_role('textbox', name='Your PayPal/Venmo email') expect(paypal_email_input).to_be_visible() expect(paypal_email_input).to_be_enabled() with allure.step("测试 Submit 按钮"): submit_button = self.page.get_by_role('button', name='Submit') expect(submit_button).to_be_visible() with allure.step("测试电话号码链接"): phone_link = self.page.get_by_role('link', name='+1(888)820-9880') expect(phone_link).to_be_visible() expect(phone_link).to_be_enabled() @allure.title("测试空输入边界条件") def test_empty_input_boundary(self): """测试空输入边界条件""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("不输入任何内容,直接点击提交"): submit_button = self.page.get_by_role('button', name='Submit') expect(submit_button).to_be_disabled() @allure.title("测试超长输入边界条件") def test_long_input_boundary(self): """测试超长输入边界条件""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("输入超长字符串到 Joyhub ID 输入框"): long_string = 'a' * 500 joyhub_input = self.page.get_by_role('textbox', name='Your Joyhub ID or registered email') joyhub_input.fill(long_string) with allure.step("验证输入内容"): expect(joyhub_input).to_have_value(long_string) @allure.title("测试特殊字符输入边界条件") @pytest.mark.parametrize("special_chars", [ "!@#$%^&*()", "", "测试中文输入", "日本語の入力", "emoji 🚀🎉", "\\n\\r\\t", "..//..//etc/passwd" ]) def test_special_characters_input(self, special_chars): """测试特殊字符输入边界条件""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step(f"输入特殊字符: {special_chars[:20]}..."): joyhub_input = self.page.get_by_role('textbox', name='Your Joyhub ID or registered email') joyhub_input.fill(special_chars) with allure.step("验证输入成功"): expect(joyhub_input).to_have_value(special_chars) @allure.title("测试正常表单填写流程") def test_normal_form_submission(self): """测试正常表单填写流程""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("选择 PayPal 支付方式"): paypal_button = self.page.get_by_role('button', name='PayPal') paypal_button.click() with allure.step("输入 Joyhub ID"): joyhub_input = self.page.get_by_role('textbox', name='Your Joyhub ID or registered email') joyhub_input.fill('test@example.com') with allure.step("输入 Amazon Order ID"): amazon_input = self.page.get_by_role('textbox', name='Your Amazon Order ID') amazon_input.fill('123-4567890-1234567') with allure.step("输入 PayPal/Venmo 邮箱"): paypal_email_input = self.page.get_by_role('textbox', name='Your PayPal/Venmo email') paypal_email_input.fill('paypal@example.com') with allure.step("验证所有输入"): expect(joyhub_input).to_have_value('test@example.com') expect(amazon_input).to_have_value('123-4567890-1234567') expect(paypal_email_input).to_have_value('paypal@example.com') @allure.title("测试按钮点击状态变化") def test_button_state_changes(self): """测试按钮点击状态变化""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("点击 PayPal 按钮"): paypal_button = self.page.get_by_role('button', name='PayPal') paypal_button.click() with allure.step("验证 PayPal 按钮被选中"): pass with allure.step("点击 Cash App 按钮切换"): cashapp_button = self.page.get_by_role('button', name='Cash App') cashapp_button.click() with allure.step("验证 Cash App 按钮被选中"): pass @allure.title("测试链接点击行为") def test_link_click_behavior(self): """测试链接点击行为""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("滚动到电话号码链接"): phone_link = self.page.get_by_role('link', name='+1(888)820-9880') phone_link.scroll_into_view_if_needed() with allure.step("点击电话号码链接"): phone_link.click() with allure.step("验证链接仍然可见"): expect(phone_link).to_be_visible() @allure.title("测试页面标题和内容") def test_page_content(self): """测试页面标题和内容""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("验证页面主标题"): main_title = self.page.get_by_role('heading', level=1, name='Try Our Toys for FREE') expect(main_title).to_be_visible() with allure.step("验证二级标题"): before_start_title = self.page.get_by_role('heading', level=2, name='Before You Start') expect(before_start_title).to_be_visible() steps_title = self.page.get_by_role('heading', level=2, name='Simple Steps to Get Your Cashback') expect(steps_title).to_be_visible() with allure.step("验证页面描述"): feedback_text = self.page.get_by_text('Leave Your Feedback for More Rewards') expect(feedback_text).to_be_visible() @allure.title("测试表单重置行为") def test_form_reset(self): """测试表单重置行为""" self.page.goto(self.switch4_url) self.page.wait_for_load_state('networkidle') with allure.step("填写表单"): joyhub_input = self.page.get_by_role('textbox', name='Your Joyhub ID or registered email') joyhub_input.fill('test@example.com') amazon_input = self.page.get_by_role('textbox', name='Your Amazon Order ID') amazon_input.fill('123-4567890-1234567') with allure.step("刷新页面"): self.page.reload() self.page.wait_for_load_state('networkidle') with allure.step("验证表单被重置"): expect(joyhub_input).to_have_value('') expect(amazon_input).to_have_value('') if __name__ == "__main__": pytest.main([__file__, "-v"])