[Bug]: Browser plugin: `visit()` and `reload()` return at the first `load` from any frame, so a page with an iframe is used before it has loaded

Aperta
#2,022 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 5 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
php, playwright
Ambito
testing

Direzione di ricerca

Start by reading Client::execute() and tracing how its lifecycle messages interact with the replies for goto and reload; the issue does not name file paths or tests. Check how Playwright reports the main frame's load state when a page has an iframe, and verify both visit() and reload() wait for the page to finish loading before returning.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug
What Happened

visit() asks Playwright for goto with waitUntil: 'load', so a test can expect the page to have loaded when it returns. On a page containing an iframe, it can return while the page is still loading. Scripts and assertions that follow then run against a half-loaded document.

How to Reproduce

A page with an iframe, followed by a script that takes two seconds to arrive:

<!doctype html>
<title>x</title>
<iframe></iframe>
<script src="/slow.js"></script> <!-- served after a 2s delay; sets window.slow = true -->
it('waits for the page', function () {
    $page = visit('http://127.0.0.1:8797/');

    dump($page->script('document.readyState'), $page->script('window.slow === true'));
});
Page document.readyState window.slow === true
With the <iframe> loading false
Same page without it complete true
Sample Repository

No response

Pest Version

pest-plugin-browser v4.3.1, Playwright 1.60.0, Chromium

PHP Version

8.4

Operation System

Linux

Notes
Cause

Client::execute() stops reading messages when either the reply to the request arrives, or any lifecycle message matches waitUntil:

if (
    (isset($response['id']) && $response['id'] === $requestId)
    || (isset($params['waitUntil']) && isset($response['params']['add']) && $params['waitUntil'] === $response['params']['add'])
) {
    break;
}

Playwright sends loadstate once per frame, with that frame's guid as the message's guid. The second condition does not look at guid, so the load of a blank iframe ends the wait for the main frame.

Page::reload() passes waitUntil: 'load' too, so it goes through the same condition.

Possible fix

Playwright only answers goto and reload once the requested state is reached on the main frame, so the reply matched by id may be enough on its own and the second condition could go. I don't know why it was added, so there may be a case that needs it.

Comparing $response['guid'] with the request's $guid would not cover everything: goto targets the frame, but reload is sent to the page guid, so a loadstate from the frame would never match.

I have not tested either change.

Workaround

After visit(), wait for the page itself:

$page->script(<<<'JS'
    new Promise((resolve) => document.readyState === 'complete'
        ? resolve(true)
        : window.addEventListener('load', () => resolve(true), { once: true }))
JS);
Lingua principale
PHP
Stelle
11.8k
Fork
559
Merge medio
5g 23h
PR unite (30g)
24

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pestphp/pest

Tutte le issue di pestphp/pest

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.