我有一个场景,我想开始在 chrome 上运行测试,在特定点我希望我的测试打开不同的浏览器 (firefox) 并执行与 chrome 中相同的步骤,然后再次返回 chrome 并验证用户界面的变化。有没有办法使用 testcafe 来做到这一点?
最佳答案
我很高兴我问了。
为了测试在另一个浏览器中的登录是否会触发当前浏览器中的注销,无需运行不同的浏览器。 您可以从测试代码发送相应的登录命令。
node.js 内置标准 http 库足以完成该任务。官方文档有专门关于http请求的章节:https://nodejs.org/en/knowledge/HTTP/clients/how-to-create-a-HTTP-request/
我个人更喜欢浏览器中可用的 fetch API。 node-fetch
Node 中提供了此 API。
所以你的测试代码可能看起来有点像这样:
import 'node-fetch';
import { URLSearchParams } from 'url';
// we assume we get page state and interaction from this seperate module
import { loginAction, getIsLogged } from './page-actions';
fixture `login logut`
.page `http://your.app/`;
test('User is logged out if logged in somewhere else', async t => {
// perform the login actions to login as "username"
await loginAction(t, 'yourUsername', 'yourPassword');
await simulateLoginFromSomewhereElse('yourUsername', 'yourPassword');
await t.expect(getIsLoggedIn(t)).eql(false);
});
async function simulateLoginFromSomewhereElse(username, password) {
// build the (form) data to be sent to the server
const params = new URLSearchParams();
params.append('username', 'yourUsername');
params.append('password', 'yourPassword');
await fetch(`http://your.app/login`, { method: 'POST', body: params });
}
关于javascript - TestCafe:如果用户从另一台计算机登录,如何测试注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64493052/