处理空手道 UI 场景中的基本身份验证

2024-03-06

我刚刚开始实现空手道 UI (v0.9.5)。已经使用空手道实现了 api 测试,并且效果完美。

遵循此页面上的 HTTP 基本身份验证策略 -https://github.com/intuit/karate#http-basic-authentication-example https://github.com/intuit/karate#http-basic-authentication-example基本的身份验证处理适用于 api 测试。我设置了一次 HTTP 标头并运行所有 api 测试。 现在进行 UI 测试,我打开的 URL 将显示基本身份验证弹出窗口,如下所示:

所以我想我可以使用与 api 测试相同的策略来处理这个问题。在我的功能文件的背景部分,我调用执行身份验证并设置标头的功能文件,如下所示:

被调用的功能文件设置标题(admin-headers.feature)。通过 karate-config.js 执行管理员用户登录后,此功能文件会获取令牌。然后将令牌与 Base64 编码的基本身份验证一起分配给调用 headers.js 的标头。 Base64 用户和密码作为 Maven 参数输入,并通过 karate-config 变量读取。

(/admin-headers.feature)

Feature: karate-config.js will perform one time login for admin and
  set the session token for all subsequent requests

  Background:
    * def session = adminAuthInfo.authSession
    * def basic_auth = call read('classpath:basic-auth.js') { username: '#(basicAuthUser)', password: '#(basicAuthPassword)' }
    * configure headers = read('classpath:headers.js')

  Scenario: One-time login for user and set the
    session token in request header

将 Auth 和 Cookie 返回到上述功能文件 (/headers.js) 的 js 代码。

function() {
    var session = karate.get('session');
    var basic_auth = karate.get('basic_auth');
    if(session){
        return {
            Authorization: basic_auth,
            Cookie: "SESSION=" + session
        };
    } else {
        return {};
    }
}

我的 UI 测试功能文件(/ui-test.feature):

Feature: Login test

  Background:
    # Authorise via api
    * callonce read('classpath:common/headers/admin-headers.feature') 
    * configure driver = { type: 'chrome' }

  Scenario: Test login
    Given driver 'https://test.internal.mysite.com/names'

运行上述功能文件仍然会显示身份验证弹出窗口。

然后,我在初始化驱动程序时尝试设置 cookie(我认为这可能不是正确的方法?),如下所示:

Feature: Login test

  Background:
    # Authorise via api
    * def login = callonce read('classpath:common/headers/admin-headers.feature')
    * def uiCookie = { name: 'SESSION', value: '#(login.userAuthInfo.authSession)', domain: 'test.internal.mysite.com' }
    * configure driver = { type: 'chrome', cookie: '#(uiCookie)' }

  Scenario: Test login
    Given driver 'https://test.internal.mysite.com/names'

上面的也行不通。我在这里做错了什么?一直弹出弹窗是因为驱动初始化然后打开指定的url时没有设置cookie?

非常感谢您的帮助。


我认为您提出了一个非常好的功能请求,即configure driver还应该接受 cookie,以便您可以一次性导航到该页面并设置 cookie,我打开了一个功能请求:https://github.com/intuit/karate/issues/1053 https://github.com/intuit/karate/issues/1053

所以尝试这个序列,请参阅文档cookie(): https://github.com/intuit/karate/tree/master/karate-core#cookieset https://github.com/intuit/karate/tree/master/karate-core#cookieset

* driver 'about:blank'
* cookie(uiCookie)
* driver 'https://test.internal.mysite.com/names'

现在应该可以了!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

处理空手道 UI 场景中的基本身份验证 的相关文章

随机推荐