2

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

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:

enter image description here

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:

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)

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

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 {};
    }
}

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'

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

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'

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?

Help is much appreciated.

rochitsen
  • 248
  • 2
  • 10

1 Answers1

1

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

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 !

Peter Thomas
  • 40,008
  • 10
  • 46
  • 142
  • I tried the above approach. I ran this with default driver: chrome and I can see in karate.log that the cookie is set. But when i open the url after setting the cookie, still get the basic auth pop-up. Printing the cookie shows me the cookie info. Could it be something that chrome has that it does not let you automate this bit? I also tried running this with driver: chromedriver and for this setting the cookie gives an error - "invalid domain". Any ideas? – rochitsen Feb 24 '20 at 05:01
  • 1
    @rochitsen unfortunately no ideas. check if you need a `domain` key: https://github.com/intuit/karate/tree/master/karate-core#cookieset - if you study the cookies needed and set them - you should be able to bypass login. work with your front-end dev team if needed - last resort, follow this process: https://github.com/intuit/karate/tree/develop/examples/ui-test – Peter Thomas Feb 24 '20 at 05:51
  • so I tried this as the login url to be opened by driver - 'https://basicauthUser:basicAuthPassword@test.internal.mysite.com/login'. This way I was able to bypass the basic auth pop-up (login code in a called feature). The login page opened, where I logged in using karate and the cookie was set automatically. Got the control back to my calling feature (actual tests) which then opens other pages '/names' and the tests succeed. I have run this successfully for driver type: chrome and chromedriver. Hopefully this solution works in the long term. – rochitsen Feb 25 '20 at 03:36
  • 1
    @rochitsen yes, that sounds right. you can try the experiment to extract the cookie value and use that using the pattern in my answer - if it doesn't work - do try to pass us info to improve karate, see link in my prev comment. and also read this :) https://stackoverflow.com/help/someone-answers – Peter Thomas Feb 25 '20 at 04:13