Karate UI - Maintain browser cookies/session/local storage variable value accorss different scnearios

蹲街弑〆低调 提交于 2021-01-28 09:39:26

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/61365319/karate-ui-maintain-browser-cookies-session-local-storage-variable-value-accors

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!