Search code examples
javascriptjavakaratesession-storage

Is there a way of getting a sessionStorage using Karate DSL?


I currently need to get an authentication token that is being held under a sessionStorage key and use it on other API calls. I'm using Karate UI automation to access the browser and get to the page but I wasn’t able to access sessionStorage. I also thought about using some JS to get it, but I’m not sure if will work. Does anyone has any ideas?


Solution

  • Should be easy using script(). I just tried these 2 statements in a test and it worked fine:

    * script("sessionStorage.setItem('foo', 'bar')")
    * match script("sessionStorage.getItem('foo')") == 'bar'
    

    I'm using version 0.9.5.RC5 and the documentation is here: https://github.com/intuit/karate/tree/develop/karate-core#script