Skip to content

[Bug]: when testing file inputs, state seems to persist between pytest/playwright tests #2996

@nerdoc

Description

@nerdoc

Version

1.55.0

Steps to reproduce

I have a rather complicated setup with server rendered components (https://tetraframework.com) in Django.
While this may not be a playwrite issue itself, it could be one, as the underlying issue I suppose is not a "bug", but a "feature" of browsers.

https://playwright.dev/python/docs/api/class-locator#locator-set-input-files

I try to test a component that produces a file input and lets you upload the file.
The component produces:

<div>
        {{ form.file }} <!-- renders to:  <input type="file" id="id_file" x-model="file">  -->
        <div id="errors">{{ form.file.errors }}</div>
    ...
        <button id="submit-button" @click="submit()">Upload</button>
        ...
    </div>
@pytest.mark.playwright
def test_component_upload_with_no_file_must_fail(page, live_server):
    page.goto("about:blank")  # should force browser to drop old file input completely
    page.goto(
        live_server.url
        + reverse("generic_ui_component_test_view", args=["UploadComponent"])
    )

    # Clear any existing file selection by setting empty files
    # page.locator("#id_file").set_input_files("")

    # don't add a file to the input, just click on "submit"
    page.locator("#submit-button").click()
    page.wait_for_load_state()

    assert "This field is required" in page.locator("#errors").inner_html()

The problem is:

  • If this test is run separatedly, it passes
  • if the test is run right after another test that uses the same component, but passes a file, it fails, because the file is still uploaded.

I think that playwright uses the browser cache here to still hold the file in the cache.

I've tried everything, getting a new browser, new context, new page. no difference.

Please proof me wrong - I apologize for that, if so. And yes, it bay not be playwright's fault if the browser enging below is the culprit, but playwrite should IMHO cope with that, as browsers do that as "feature".

Expected behavior

I expect to have a "fresh" rendered, empty file input field in a new test, even when the last test filled that field

Actual behavior

the old file seems to be reused.

Additional context

No response

Environment

- Operating System: [Ubuntu 22.04]
- CPU: [arm64]
- Browser: [All, Chromium, Firefox, WebKit]
- Python Version: [3.12]
- Other info:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions