簡體   English   中英

如何使用 TestCafe 對所有測試進行身份驗證

[英]How to Authenticate for all tests using TestCafe

我知道這個話題討論了很多,但我有一個獨特的情況。

為了測試我們的驗收環境,我們需要點擊https://authentication-example.com ,它運行一個腳本來添加會話 cookie 從而對我們進行身份驗證。 然后我們導航到https://acceptance-site.com來運行測試。

在嘗試了許多選項之后,我能得到的最接近解決方案是使用一個角色,例如

const a3 = Role('https://acceptance-site.com', async testController => {
    await testController.navigateTo('https://authentication-example.com');
    await testController.navigateTo('https://acceptance-site.com');
}, { preserveUrl: true });

fixture('Testing acceptance')
    .beforeEach(async testController => {
        await testController.useRole(authenticate);
});

test('example1', async testController => {
    await testController.debug();
}).disablePageReloads;

test('example2', async testController => {
    await testController.debug();
}).disablePageReloads;

test('example3', async testController => {
    await testController.debug();
}).disablePageReloads;

該解決方案使我無法加載與角色結束時不同的任何新頁面。

如果我從角色中刪除 { preserveUrl: true },example2 和 example3 將加載空白頁面。 如果我從測試中刪除 .disablePageReloads,則 escond 和第三次測試的身份驗證失敗,並且我收到錯誤“無法在...處找到資源的 DNS 記錄”如果我擔任以下角色

const a3 = Role('https://authentication-example.com', async testController => {
    await testController.navigateTo('https://acceptance-site.com');
}, { preserveUrl: true });

所有測試的身份驗證都失敗。

我注意到,當工作成功時,我應該得到的cookie實際上是在調試時保存在應用程序下的會話中,它被這個錘頭存儲包裝器改變了。 測試時的基本 url 是 testCafe 服務器的 ip 和端口,前面是隨機字母,例如 172.0.0.1:8080/hoi23hh/ https://acceptance-site.com這導致我的 cookie 被保存在會話中(不是在不使用 test cafe 時通常發生的 cookie 下)作為錘頭|storage-wrapper|hoi23hh|acceptance-site.com

當我刪除 .disablePageReloads,但在角色上保留 preserveUrl: true 時,“cookie”保持不變,但基本網址更改為 172.0.0.1:8080/ohgbo223/ https://acceptance-site.com

因此,url 中的“hoi23hh”更改為“ohgbo223”,cookie/會話密鑰不再與 url 匹配,身份驗證失敗。

您對保持身份驗證有什么建議,同時仍然可以更改頁面等

我不建議對角色使用disablePageReloads功能,因為它是內部的並且可能不穩定。 ReserveUrl 選項將允許每個測試從https://acceptance-site.com頁面開始。 所以,我認為這是你的情況最可取的方式::

const a3 = Role('https://acceptance-site.com', async testController => {
    ...
}, { preserveUrl: true });

fixture('Testing acceptance')
    .beforeEach(async testController => {
        await testController.useRole(authenticate);
});

test('example1', async testController => {
    await testController.debug();
})

test('example2', async testController => {
    await testController.debug();
})

暫無
暫無

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

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