playwright-pytest icon indicating copy to clipboard operation
playwright-pytest copied to clipboard

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

Open floraachy opened this issue 1 year ago • 10 comments

I set tracing,screenshot and video opened.

floraachy avatar Jul 10 '24 03:07 floraachy

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:]}"

floraachy avatar Jul 10 '24 05:07 floraachy

it will be unable to take screenshots and record videos.

Can you share the error which you receive?

mxschmitt avatar Jul 10 '24 07:07 mxschmitt

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

floraachy avatar Jul 10 '24 09:07 floraachy

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

mxschmitt avatar Jul 12 '24 12:07 mxschmitt

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

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

floraachy avatar Jul 15 '24 00:07 floraachy

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')
...

mxschmitt avatar Jul 15 '24 11:07 mxschmitt

os.pathconf('/', 'PC_NAME_MAX')

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

floraachy avatar Jul 16 '24 00:07 floraachy

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.

mxschmitt avatar Jul 16 '24 12:07 mxschmitt

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 avatar Jul 17 '24 01:07 floraachy

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

floraachy avatar Jul 17 '24 02:07 floraachy

This issue has two viable solutions:

  1. Enable long path support in Windows by setting LongPathsEnabled=1 in Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem (new Windows 11 default)

  2. Use shorter test case names to stay under Windows' default path length limits

The truncate_file_name() function is working as designed, but Windows' default path length restrictions can still cause issues with the full artifact paths. Since this is a Windows-specific limitation that can be addressed through system settings or test naming conventions, I suggest we close this issue.

For users encountering similar issues, please either:

  • Enable long path support in Windows (recommended)
  • Use shorter test names
  • Configure a shorter base output directory path

Closing as the functionality is working as expected and workarounds are available.

mxschmitt avatar Dec 18 '24 16:12 mxschmitt