microsoft / playwright-pytest

Pytest plugin to write end-to-end browser tests with Playwright.
https://playwright.dev/python/docs/test-runners
Apache License 2.0
423 stars 69 forks source link

truncate_file_name() didn't work when I configured --output #231

Open floraachy opened 1 month ago

floraachy commented 1 month ago

I set tracing,screenshot and video opened.

floraachy commented 1 month ago

In pytest, I have configured options for taking screenshots, recording videos, and tracing。 [pytest] addopts = --tracing=on --screenshot=on --video=on

also, I have configured options "--output" with my own directory. For example: --output=outputs/tracing

When I run test, if the test case name is too long, it will be unable to take screenshots and record videos.

testcase name: test_login_user_no_phone[chromium-网页登录,正确用户名和密码登录成功,弹窗后提示未绑定手机号码]

I checked pytest_playwright.py, It will runcates the length of the screenshot file name. But it seems doesn't work.

pytest_playwright.py

def _build_artifact_test_folder( pytestconfig: Any, request: pytest.FixtureRequest, folder_or_file_name: str ) -> str: output_dir = pytestconfig.getoption("--output") return os.path.join( output_dir, truncate_file_name(slugify(request.node.nodeid)), truncate_file_name(folder_or_file_name), )

def truncate_file_name(file_name: str) -> str: if len(file_name) < 256: return file_name return f"{file_name[:100]}-{hashlib.sha256(file_name.encode()).hexdigest()[:7]}-{file_name[-100:]}"

mxschmitt commented 1 month ago

it will be unable to take screenshots and record videos.

Can you share the error which you receive?

floraachy commented 1 month ago

it will be unable to take screenshots and record videos.

Can you share the error which you receive?

only one case, it didn‘t generate pictures and video., this is the error:

image

When I changed the testcase name, long name to short name. It run success and genertate picures and videos successfully. f0fad1b99e256b05b9790f87d846020

mxschmitt commented 1 month ago

Could you help us creating a minimal reproduction for it? I tried to execute the following ,but for me it was working as expected:

import pytest

testdata = [
    ("网页登录,正确用户名和密码登录成功,弹窗后提示未绑定手机号码弹窗后提示未绑定手机号码弹窗后提示未绑定手机号码]机号码弹窗绑定手机号码弹窗后提示未绑定手机号码]机号码弹窗后提绑定手机号码弹窗后提示未绑定手机号码]机号码弹窗后提绑定手机号码弹窗后提示未绑定手机号码]机号码弹窗后提后提示未绑定手机号码弹窗后提示未绑定手机号码]机号码弹窗后提示未绑定手机号码弹窗后提示未绑定手机号码]机号码弹窗后提示未绑定手机号码弹窗后提示未绑定手机号码]机号码弹窗后提示未绑定手机号码弹窗后提示未绑定手机号码]"),
]

@pytest.mark.parametrize("name", testdata)
def test_timedistance_v0(name, page):
    print(name)

I was running it with --video=on --screenshot=on --tracing=on

floraachy commented 1 month ago
网页登录,正确用户名和密码登录成功,弹窗后提示未绑定手机号码

test_gitlink.py


from playwright.sync_api import Page, expect
import pytest

@pytest.mark.parametrize('case',
                         [{"title": "网页登录,正确用户名和密码登录成功,弹窗后提示未绑定手机号码", "name": "GitLink"}],
                         ids=lambda x: x["title"])
def test_gitlink(page: Page, case):
    page.goto("https://www.gitlink.org.cn/explore")

    expect(page).to_have_url("https://www.gitlink.org.cn/explore")
    assert "GitLink" == case["name"]

main.py


import pytest

if __name__ == '__main__':
    pytest.main(["--browser=chromium", "--headed"])

pytest.ini

[pytest]
addopts =
    --tracing=on
    --screenshot=on
    --video=on
    --output=results

When I run main.py, ERROR happened: image

mxschmitt commented 1 month ago

For me the test passes unfortunately so your system might have different settings compared to my machine. Could you run the following and provide me the values?

python
>>> os.pathconf('/', 'PC_PATH_MAX')
...
>>> os.pathconf('/', 'PC_NAME_MAX')
...
floraachy commented 1 month ago
os.pathconf('/', 'PC_NAME_MAX')

My system version: Windows 11 专业版 - 23H2 - 22631.3880 Python version=3.9.5 image

mxschmitt commented 1 month ago

I see, while looking at the error message, I stumped across this post - we recommend to have this setting enabled, could you try enabling this? The issue is that the whole path length reaches this limit, so it would be hard from our side to take care of that, since the parent path could be already very long.

floraachy commented 1 month ago

I see, while looking at the error message, I stumped across this post - we recommend to have this setting enabled, could you try enabling this? The issue is that the whole path length reaches this limit, so it would be hard from our side to take care of that, since the parent path could be already very long.

I changed Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled from 0 to 1, no error happen and the folder generated successfully.

Could we limit the maximum length of directory names through code? I thought this method: truncate_file_name, was used to limit the length of directory . I still can't understand why it didn't work.

1721179357692

floraachy commented 1 month ago

I see, while looking at the error message, I stumped across this post - we recommend to have this setting enabled, could you try enabling this? The issue is that the whole path length reaches this limit, so it would be hard from our side to take care of that, since the parent path could be already very long.

I made a little change, and It solved my problem for now. (I don't want adjust my system:Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled )。

5c296df52a040e56556e5d70019c2c0