Playwright使用await.goto()时游戏未加载完成即中断的问题
解决Playwright游戏加载测试中页面提前停止加载的问题
问题背景
编写游戏加载测试脚本时,调用await page.goto()后,游戏还没进入画面就停止加载了。目前用await page.waitForTimeout(10000)固定等待10秒,需要替代方案实现等待40秒,同时确保游戏完全加载显示后再终止脚本。
1. 智能等待(推荐,比固定超时更可靠)
不要依赖固定时间等待,而是根据页面状态或元素加载情况判断:
方式一:goto时设置等待条件和超时
直接在page.goto()里配置等待网络空闲(无网络请求超过500ms),同时设置40秒超时,确保游戏资源加载完成:
import { test, expect } from '@playwright/test'; test('game spin', async ({ page }) => { await page.goto('https://gpas-games-trtgpasdev01.extdev.eu/games/StarsAblaze/last/game/index.html#clientType=casino&clientPlatform=web&username=User15&password=Pass123456&casino=playtech85001&locale=en&real=1&historyurl=http%3A%2F%2Fbbc.co.uk&cashierurl=https%3A%2F%2Fwww.bgo.com%2Fcashier%3Fmode%3Ddeposit%26clienttype%3Dcasino&backurl=http%3A%2F%2Fashgaming.com&minimumSpinTime=0&crosslaunchurl=http%3A%2F%2Fbbc.co.uk', { waitUntil: 'networkidle', // 等待网络空闲 timeout: 40000 // 最长等待40秒 }); });
方式二:等待游戏核心元素出现
游戏加载完成后通常会显示核心元素(比如游戏画布、主界面容器),直接等待这个元素可见:
import { test, expect } from '@playwright/test'; test('game spin', async ({ page }) => { await page.goto('https://gpas-games-trtgpasdev01.extdev.eu/games/StarsAblaze/last/game/index.html#clientType=casino&clientPlatform=web&username=User15&password=Pass123456&casino=playtech85001&locale=en&real=1&historyurl=http%3A%2F%2Fbbc.co.uk&cashierurl=https%3A%2F%2Fwww.bgo.com%2Fcashier%3Fmode%3Ddeposit%26clienttype%3Dcasino&backurl=http%3A%2F%2Fashgaming.com&minimumSpinTime=0&crosslaunchurl=http%3A%2F%2Fbbc.co.uk'); // 替换成游戏实际的核心元素选择器,比如游戏画布ID或主容器类名 await page.waitForSelector('#game-canvas', { state: 'visible', // 等待元素可见 timeout: 40000 // 超时40秒 }); });
2. 强制等待40秒(满足需求但不推荐)
如果必须用固定时间等待,直接修改waitForTimeout的参数即可:
import { test, expect } from '@playwright/test'; test('game spin', async ({ page }) => { await page.goto('https://gpas-games-trtgpasdev01.extdev.eu/games/StarsAblaze/last/game/index.html#clientType=casino&clientPlatform=web&username=User15&password=Pass123456&casino=playtech85001&locale=en&real=1&historyurl=http%3A%2F%2Fbbc.co.uk&cashierurl=https%3A%2F%2Fwww.bgo.com%2Fcashier%3Fmode%3Ddeposit%26clienttype%3Dcasino&backurl=http%3A%2F%2Fashgaming.com&minimumSpinTime=0&crosslaunchurl=http%3A%2F%2Fbbc.co.uk'); // 强制等待40秒 await page.waitForTimeout(40000); });
注意
- 固定超时
waitForTimeout不适合稳定性测试,万一游戏加载速度波动,容易导致测试失败,优先用智能等待。 - 部分游戏可能用动态加载,需要根据实际页面结构调整等待的元素或状态。
内容的提问来源于stack exchange,提问作者tega enajekpo
相关产品推荐
相关产品推荐

