【问题标题】:Open multiple browser windows in 1 testcafe test在 1 个 testcafe 测试中打开多个浏览器窗口
【发布时间】:2021-07-17 15:07:30
【问题描述】:
我正在尝试使用 testcafe 编写一个可能在文档中产生冲突的测试。为此,我需要在 2 个不同的浏览器会话(一个是隐身窗口)中以 2 个不同的用户登录,并为每个用户打开文档编辑器并编辑相同的文本。在编辑内容时,两个用户都在编辑器中以产生冲突,这一点很重要。
有没有办法在 1 个 testcafe 测试中打开多个浏览器会话?或者同时处理具有 2 个不同用户角色的文档的替代解决方案?
测试的样子:
- 打开浏览器窗口
- 使用 User1 登录
- 导航到文档编辑器 (Tinymce)
- 打开隐身浏览器(不关闭第一个窗口)
- 使用 User2 登录
- 导航到文档编辑器
- 使用 User1 将“文本”编辑为“紫色”,然后保存
- 使用 User2 将“text”编辑为“gold”,然后保存
- 断言发生了冲突
【问题讨论】:
-
TestCafe 没有内置方式来启动具有不同选项的浏览器窗口。 Multiple Browser Windows 功能在同一进程中启动不同的窗口,从而防止将不同的选项应用于它们。因此,如果您希望您的一个用户处于正常模式而其他用户处于隐身模式,请同时运行两个不同的测试并同步它们。稍后,我会举一个例子给你演示一种可能的实现方式。
标签:
testing
automated-tests
integration-testing
testcafe
e2e
【解决方案1】:
我为您做了一个示例,说明如何运行多个 TestCafe 测试并使用 Promise 同步它们:https://github.com/DevExpress/testcafe-example-multiuser-scenario
克隆此存储库:git clone https://github.com/DevExpress/testcafe-example-multiuser-scenario.git
安装软件包:npm install 并使用 npm run test 运行测试。
如果您使用这种方法,您的测试将如下所示:
test('Some test', async t => {
await stage('First stage');
//do somthing
await stage('Second stage');
//do anything else
stage('End');
});
以及驱动各个阶段的脚本:
await user1.runStage('First stage'); //will run the 'First stage' in the test and wait for the next stage request
await user1.runStage('Second stage'); //will run the 'Second stage'
user1.runStage('End');