如何在helper.js中使用.httpAuth创建函数

问题描述

我想创建一个登录功能,该功能使用testcafe.httpAuth中的helper.js接受Auth Dialog的用户名和密码。如何创建该函数,以便可以在需要登录的任何地方使用它。

解决方法

请注意,.httpAuth方法可以用作fixturetest的方法。

如果我对您的理解正确,那么您想将一些导入的{username,password}对象与HTTP Autentication一起使用。

可以通过使用来自帮助文件的导入来实现:

authHelper.js

const authCredentials = {
    username: 'username',password: 'password'
}

export default authCredentials;

test-1.js

import auth from './authHelper'

fixture `My fixture 1`
    .page `http://example.com`
    .httpAuth(auth);

test('Test1',async t => {}); // Logs in as username

test-2.js

import auth from './authHelper'

fixture `My fixture 2`
    .page `http://example.com`;

test
    .httpAuth(auth)
    ('Test1',async t => {}); // Logs in as username