1

I am testing various components of my website and to test each component there is a pre-requisite that user must be logged in. When user login to website web server creates the cookie in the browser which then allow the user to access those components/pages. Now to test each component, for which I am writing several scenarios, I am writing login code every time in the scenarios because whenever my scenarios starts using the following line

@tag
Feature: User List
  I want to use this template for my user list

  @tag1
  Scenario: Login failure error when wrong credentials
    Given driver 'https://mywebsite.com/login'
    When input("input[name='session[username_or_email]']", 'hello')
    When input("input[name='session[password]']", ['asasas', Key.ENTER], 100)
    When click('div[role=button]')
    Then match html('#user-list') contains 'User Details'


    #THEN REST OF THE UI TEST

I have to repeat that login code in each feature file so that I can test my rest of the page. My requirement is when I run my login feature I have to store and retain the cookie when I execute the next scenarios. How do I do that without calling the login feature or login code again and again?

Using Karate core I have used karate single call feature in the karate config which allows me to set the HTTP headers and cookie which get used in the subsequent request so it was not an issue. Can I do something like that in the Karate UI? Any pointer towards the correct direction will be really appreciated.

Atul Chaudhary
  • 3,698
  • 1
  • 31
  • 51

1 Answers1

0

Yes, this is one of the advantages of Karate. All you need to know is what browser cookie(s) to set. You can figure this out by looking at the Chrome Developer Tools for example.

Then, here is the pattern:

* def token = 'setByCallSingle'
* driver 'about:blank'
* cookie({ name: 'my_token', value: token, domain: '.mycompany.com' })
* driver baseUrl + '/home'

I tried to update the docs to be more clear: https://github.com/intuit/karate/tree/develop/karate-core#cookieset

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • If I am understanding your example correctly "* def token = 'setByCallSingle'" means I am calling a feature which will return the token and then I set the cookie via that token which means that I have to implement that code in each feature file. How do I do that in the karate-config so that I don't have to call this token setting code in every feature. Am I understanding your example correctly? – Atul Chaudhary Apr 22 '20 at 13:20
  • @AtulChaudhary I assumed a) you are already using callSingle (you mentioned it) and b) it is the same token that lands as a cookie if you look at your browser. now - your question is about re-usable code, you can have this code in a `call`, I assume you know how to re-use features in karate. if you really want to bloat your `karate-config.js` you can do `karate.call()`. any questions ? – Peter Thomas Apr 22 '20 at 13:23
  • 1
    I got it now thanks for the explanation. I will try this out soon – Atul Chaudhary Apr 22 '20 at 13:27
  • 1
    Hi @Peter Thomas, I have got it working. High Level design is in karate.config I am calling my set cookie feature then storing the cookie value in global varuables. Then your sample code given above did the trick. – Atul Chaudhary Apr 29 '20 at 10:48
  • Hi @Atul Chaudhary I am also looking for same kind of solution. Is it possible for you if you can guide me how you achieve the same for UI automation. To avoid re-login before every scenario in one feature file. – Saurabh Garg Mar 25 '21 at 04:35