Tengo esta página simple que abre una nueva ventana.
<html> <body> <button id="open">Open window</button> </body> <script> var windowObjectReference; function openRequestedPopup() { windowObjectReference = window.open("http://www.cnn.com/", "CNN"); } document.querySelector("#open").addEventListener("click", openRequestedPopup); </script> </html>También tengo esta prueba:
import {Selector} from 'testcafe'; fixture `Close window` .page('http://127.0.0.1:8080/'); test('Close window', async t => { await t.click("#open"); await t.switchToWindow(f => f.url.host==="www.cnn.com").closeWindow(); });Al intentar encontrar la ventana basada en un predicado, la prueba falla
PS C:\work\New folder> testcafe edge .\test.js Running tests in: - Microsoft Edge 96.0.1054.43 / Windows 10 Example page × Close the current window 1) Cannot find the window specified in the action parameters. Browser: Microsoft Edge 96.0.1054.43 / Windows 10 1/1 failed (5s)No puedo entender qué debo hacer para obtener la ventana abierta desde el código js.
¿Cómo puedo obtener la ventana e inspeccionar algunos elementos en el interior?
Hemos solucionado problemas relacionados con el modo "Múltiples ventanas del navegador". Instale la versión alfa actual ( npm i testcafe@alpha / npm i testcafe@1.17.2-alpha.2 ). Además, debe corregir el valor de host de su predicado. El nombre de host real de esta página de CNN es edition.cnn.com (puede comprobarlo sin TestCafe observando la propiedad window.location.hostname en la consola del navegador):
await t.switchToWindow(f => f.url.host === 'edition.cnn.com').closeWindow(); Alternativamente, puede usar t.getCurrentWindow en lugar del predicado:
await t.click("#open"); const cnnWindow = await t.getCurrentWindow(); await t.closeWindow(cnnWindow);