簡體   English   中英

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/

我個人更喜歡瀏覽器中可用的 fetch API。 node-fetchnode-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 });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM