在 Playwright 中,无法将 page.goto 与 headless webkit 一起使用
我正在尝试为 Playwright 中的 Drupal 网站编写测试。我通过 WSL2 在 Ubuntu 20.04 上运行。
我在 Playwright 配置文件中设置了一个基本 URL (https://www.example.com
)。
我有几个简单的步骤:
await this.page.goto('/user/logout');
await this.page.goto('/user/login');
await this.page.locator('input[name="name"]').fill('[email protected]');
await this.page.locator('input[name="pass"]').fill('password');
await this.page.locator('input:has-text("Login")').click();
await this.page.waitForSelector('text="You are logged in"');
await this.page.goto('/admin/structure/webform/manage/myform', { waitUntil: 'networkIdle' });
我想在模拟 iPhone 上进行测试,所以我在我的配置文件中使用了这个:
name: 'iPhone 6/7/8',
use: devices['iPhone 8'],
问题是,在这个仅处于无头模式的 iPhone 8 模拟设备中,最后一个 goto 失败(await this.page.goto('/admin/struct/webform/manage/myform')
)。当我运行测试时,这种情况 100% 都会发生。
该测试在 webkit(桌面)、chrome(桌面)、firefox(桌面)和 android(移动)上运行良好。当非无头时,该测试也适用于 iOS。相同的步骤也适用于真实的 iOS 设备。
我需要将 networkIdle
添加到最后一步,以使其在无头 Chrome(桌面版和 Android 版)上运行,因此我猜我可能需要进行另一项更改才能在 iOS 上运行,但是什么?
当我观看实际测试的视频时,当到达最后一步时,屏幕开始快速闪烁约 3 秒,然后变黑。
其他信息
基于Playwright 设备列表,我复制了 iPhone 8 的代码并进行了实验,看看具体是什么导致了错误。
如下所示,将 isMobile
从 true
更改为 false
将允许测试通过。因此,特定错误似乎与将 isMobile
设置为 true
相关。
projects: [
{
name: 'iPhone 6/7/8',
use: {
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/15.4 Mobile/15A372 Safari/604.1',
browserName: 'webkit',
viewport: {
width: 375,
height: 667,
},
deviceScaleFactor: 2,
isMobile: false,
hasTouch: true,
defaultBrowserType: 'webkit',
},
},
],
I'm trying to write a test for a Drupal website in Playwright. I'm running on Ubuntu 20.04 via WSL2.
I set a base url (https://www.example.com
) in my Playwright config file.
I have a few simple steps:
await this.page.goto('/user/logout');
await this.page.goto('/user/login');
await this.page.locator('input[name="name"]').fill('[email protected]');
await this.page.locator('input[name="pass"]').fill('password');
await this.page.locator('input:has-text("Login")').click();
await this.page.waitForSelector('text="You are logged in"');
await this.page.goto('/admin/structure/webform/manage/myform', { waitUntil: 'networkIdle' });
And I wanted to test on a simulated iPhone, so I used this in my config file:
name: 'iPhone 6/7/8',
use: devices['iPhone 8'],
The problem is that in this iPhone 8 simulated device in headless mode only, the last goto fails (await this.page.goto('/admin/structure/webform/manage/myform')
). It happens 100% of the time when I run the test.
The test works fine on webkit (desktop), chrome (desktop), firefox (desktop), and android (mobile). The test also works on iOS when not headless. And the same steps work on a real iOS device.
I needed to add networkIdle
to the final step to get it to work on headless Chrome (both desktop and Android), so I'm guessing I might need to make another change to get it to work on iOS, but what?
When I watch the video of my test in action, when it gets to the final step, the screen starts flickering very rapidly for about 3 seconds and then cuts to black.
Additional info
Based on the list of Playwright devices, I copied the code for iPhone 8 and experimented to see what specifically causes the error.
As shown below, changing isMobile
from true
to false
will allow the tests to pass. So the specific error seems to be related to whatever setting isMobile
to true
does.
projects: [
{
name: 'iPhone 6/7/8',
use: {
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/15.4 Mobile/15A372 Safari/604.1',
browserName: 'webkit',
viewport: {
width: 375,
height: 667,
},
deviceScaleFactor: 2,
isMobile: false,
hasTouch: true,
defaultBrowserType: 'webkit',
},
},
],
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 playwright 问题中所述,此行为似乎是由操作系统故障引起的(在我的例子中,Ubuntu 在 WSL2 上运行)。
通过更新 ubuntu 中的所有软件包解决了这个问题;不幸的是,有几个软件包,我不确定哪个是确切的解决方案,但要点是:
package.json
。As described in the playwright issue, this behavior seems to be caused by a failure in the OS (in my case, Ubuntu running on WSL2).
This was solved by updating all packages in ubuntu; unfortunately, there were several packages and I'm not sure which was the exact fix, but the takeaways are:
package.json
.