最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to move python function to the conftest as fixture? - Stack Overflow

programmeradmin1浏览0评论

I have the following python code:

main.py:

import pytest
import re
from playwright.sync_api import Page, expect
from time import sleep


TIMEOUT = 5000  # 5 seconds


# Helper function to send a message and assert response
def send_msg_and_assert(page: Page, message: str, regex: re.Pattern):
    page.type('[placeholder="Message..."]', message)
    page.keyboard.press("Enter")
    sleep(DEFAULT_WAIT_TIME / 1000)  # sleep time converted to seconds
    assert page.get_by_text(regex)


# Go to the chat page
def go_to_chats(page: Page):
    page.goto('/')
    page.wait_for_timeout(TIMEOUT)
    assert page.get_by_text("Chats").is_visible()


@pytest.mark.usefixtures("login_logout")
@pytest.mark.basic_interaction
@pytest.mark.parametrize("message, expected_regex", [
    ("How can you help me?", repile("/language|help/", re.IGNORECASE)),
    ("What kind of questions?", repile("/Language|Native|interview/", re.IGNORECASE)),
    ("What does my name mean?", repile("/sorry|determine|please tell/", re.IGNORECASE))
])
# This basic scenario is using playwright web application testing via the cohere web-page
def test_basic_interaction(go_to_chats, message: str, expected_regex: re.Pattern):
    # Step 1: Login and go to chats page
    page = go_to_chats

    # Step 2: Send message and assert response
    send_msg_and_assert(page, message, expected_regex)

    # Step 3: Logout

Here you can find the conftest.py with the fixtures:

import pytest
import os
import re
from playwright.sync_api import sync_playwright, Page

TIMEOUT = 5000  # 5 seconds
DEFAULT_WAIT_TIME = 30000  # 30 seconds for responses

# Constants and credentials
COHERE_API_KEY = "your-api-key"
USER_CREDENTIALS = {
    'email': '[email protected]',
    'password': "blabla",
    'user_name': 'goofy'
}


# Fixture for login, reusable across tests
@pytest.fixture(scope="session")
def login_logout():
    with sync_playwright() as p:
        browser = p.chromium.launch(headless=False)  # Set headless=True for no UI
        page = browser.new_page()

        # Performs login on the Cohere dashboard.
        page.goto('')
        page.locator('input[name="email"]').fill(USER_CREDENTIALS['email'])
        page.locator('input[name="password"]').fill(USER_CREDENTIALS['password'])
        page.get_by_role("button", name="Log in").click()
        page.wait_for_timeout(TIMEOUT)  # Wait for login to complete
        assert page.get_by_text(f"Welcome, {USER_CREDENTIALS['user_name']}!").is_visible()

        # Yield control to test
        yield page

        # Fixture for logout, reusable across tests
        # Performs logout after all tests are completed.
        page.goto('')
        page.wait_for_timeout(TIMEOUT)  # Wait for logout to complete
        assert page.get_by_role("heading", name="Log in").is_visible()
        browser.close()

The login_logout function in the conftest.py will be executed at the beginning of the session and at the end of the session to achieve the use of one session for test cases.

Now I was trying to move the go_to_chats() (or any other function) also to the fixtures but not as part of the login_logout() function. How can I achieve that?

发布评论

评论列表(0)

  1. 暂无评论