TestCafe:如果用户从另一台机器登录,如何测试注销

TestCafe: How to test logout if user logged in from another machine

我有一个场景,我想在 chrome 上开始 运行 测试,在特定点我希望我的测试打开不同的浏览器 (firefox) 并执行与chrome 然后再次返回 chrome 并验证 ui 中的更改。有没有办法用 testcafe 做这个?

很高兴我问了。

为了测试在另一个浏览器中的登录是否会触发当前浏览器中的注销,不需要运行 不同的浏览器。 您可以从您的测试代码发送相应的登录命令。

node.js 内置标准 http 库足以完成该任务。官方文档有一个特定的部分是关于http请求的:https://nodejs.org/en/knowledge/HTTP/clients/how-to-create-a-HTTP-request/

我个人更喜欢浏览器中可用的提取 API。 node-fetch 在节点中提供此 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 });
}