Testing / Karate Framework Interview questions
How do you implement reusable authentication flows using call in Karate?
Authentication is one of the most common candidates for reuse across a Karate suite, since nearly every scenario touching a protected endpoint needs a valid token, but the login flow itself shouldn't be copy-pasted into every feature file.
# login.feature Feature: reusable login Scenario: Given url baseUrl + '/auth/login' And request { username: '#(username)', password: '#(password)' } When method post Then status 200 * def token = response.token
# Background of any feature needing auth Background: * def creds = { username: 'admin', password: 'secret' } * def loginResult = callonce read('login.feature') creds * configure headers = { Authorization: 'Bearer ' + loginResult.token }
Wrapping the login flow in its own feature file, called with callonce from a shared Background, means the actual login logic exists in exactly one place, gets executed only once per suite run (assuming the token stays valid for the whole run), and every subsequent scenario simply inherits the resulting Authorization header through configure without repeating any of the setup.
More Related questions...