Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在hook.py中before_all_extend方法中启动浏览器,在运行feature时遇到了同步异步的问题 #329

Open
VencentMei opened this issue Nov 27, 2024 · 0 comments

Comments

@VencentMei
Copy link

VencentMei commented Nov 27, 2024

问题描述 / Describe the bug
在before_all_extend方法中使用sync_playwright()启用了浏览器,手动编写step,但在运行feature时报错It looks like you are using Playwright Sync API inside the asyncio loop.

问题复现步骤 / To Reproduce
源码:
hook.py文件

from playwright.sync_api import Page, sync_playwright
from flybirds.core.global_context import GlobalContext
import flybirds.utils.flybirds_log as log
import flybirds.core.global_resource as gr

def get_page() -> Page:
    """
    获取页面对象
    """
    return gr.get_value("plugin_page").page

def before_all_extend(context):
    playwright = sync_playwright().start()
    try:
        browser = playwright.chromium.launch(headless=False)
        browser_context = browser.new_context(ignore_https_errors=True)
        page = browser_context.new_page()
        log.info("before_all_extend")
        page.goto("https://www.jd.com")
        page.wait_for_load_state("domcontentloaded")
        gr.set_value("playwright", playwright)
        gr.set_value("browser", browser)
        gr.set_value("plugin_page", page)

    except Exception as e:
        log.error("before_all_extend error:" + str(e))
        if browser:
            browser.close()
        raise


def after_all_extend(context):
    browser = gr.get_value("browser")
    playwright = gr.get_value("playwright")
    if browser:
        browser.close()
    if playwright:
        playwright.stop()
    else:
        log.error("Error: Playwright or Browser not properly initialized.")

steps.py

import flybirds.core.global_resource as gr
import pscript.custom_handle.operation as operation
from flybirds.utils.flybirds_log import logger as log
from behave import step, when
from playwright.sync_api import Page

def get_page() -> Page:
    """
    获取页面对象
    """
    return gr.get_value("plugin_page")

@step("访问百度")
def visit_baidu(context):
    page = get_page()
    page.goto("https://www.baidu.com")
    page.wait_for_load_state("domcontentloaded")
    log.info("visit_baidu")

test_baidu.feature

# language: zh-CN

功能: 测试同浏览器跳转

    场景: 测试跳转百度
         访问百度

报错

2024-11-27 15:08:42,876 - flybirds_log - INFO - before_all_extend
2024-11-27 15:08:43,236 - flybirds_log - INFO - [web run] OnBefore run event!
2024-11-27 15:08:43,236 - flybirds_log - ERROR - Init browser has error! Error msg is:
2024-11-27 15:08:43,236 - flybirds_log - ERROR - It looks like you are using Playwright Sync API inside the asyncio loop.
Please use the Async API instead.
2024-11-27 15:08:43,237 - flybirds_log - INFO - global initialization error
2024-11-27 15:08:43,237 - flybirds_log - INFO - Traceback (most recent call last):
  File "C:\Users\12077\miniconda3\envs\test3.10\lib\site-packages\flybirds\core\plugin/event\web_run.py", line 67, in run
    OnBefore.init_ui_driver(context)
  File "C:\Users\12077\miniconda3\envs\test3.10\lib\site-packages\flybirds\core\plugin/event\web_run.py", line 32, in init_ui_driver
    play_wright, browser = ui_driver.init_driver()
TypeError: cannot unpack non-iterable NoneType object
@VencentMei VencentMei changed the title 在hook.py中before_all_extend方法中打开浏览器,如何使得在运行每个feature时使用此浏览器,而不是另外启动一个浏览器 在hook.py中before_all_extend方法中启动浏览器,在运行feature时遇到了同步异步的问题 Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant