在空手道UI场景中处理基本身份验证 [英] Handling Basic Authentication in Karate UI scenario

查看:48
本文介绍了在空手道UI场景中处理基本身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have just started implementing karate UI (v0.9.5). Have already implemented api testing using karate and it works perfectly.

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

Following the HTTP basic auth strategy on this page - https://github.com/intuit/karate#http-basic-authentication-example the basic auth handling works for api tests. I set the HTTP headers once and run all api tests. Now for the UI testing, the URL that I open brings up the basic auth pop-up as shown below:

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

So I thought that I could use the same strategy that I used for api tests to handle this. In the background section of my feature file, i call the feature file that does the authentication and sets headers as below:

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

The called feature file to set headers (admin-headers.feature). This feature file gets the token after admin user login is performed via karate-config.js. Then assigns the token along with the Base64 encoded basic auth to the headers calling headers.js. The Base64 user and password are being input as maven arguments and read via karate-config variables.

(//admin-headers.feature)

(/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代码.

The js code for returning Auth and Cookie to above feature file (/headers.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):

My UI test feature file (/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'

运行上面的功能文件仍然显示auth弹出窗口.

Running the above feature file still shows the auth pop-up.

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

I then tried to set the cookies while I am initialising the driver (which I think is probably not the right way?) as below:

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?

The above also does not work. What is it that I am doing wrong here? the pop-up keeps coming up because the cookie is not set when the driver is initialised and then opens the specified url?

非常感谢您的帮助.

推荐答案

我认为您提出了一个很好的功能请求,即configure driver也应该使用cookie,以便您可以导航到该页面并在其中设置cookie.镜头,然后我打开了功能请求: https://github.com/intuit/karate/issues /1053

I think you raised a very good feature request, that configure driver should take cookies also, so that you can navigate to the page and set cookies in one-shot, and I opened a feature request: https://github.com/intuit/karate/issues/1053

因此,请尝试以下顺序,请参考文档以获取cookie(): https ://github.com/intuit/karate/tree/master/karate-core#cookieset

So try this sequence, refer docs for cookie(): https://github.com/intuit/karate/tree/master/karate-core#cookieset

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

现在它应该可以工作了!

And now it should work !

这篇关于在空手道UI场景中处理基本身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆