player icon indicating copy to clipboard operation
player copied to clipboard

Variables available in multiple scenarios

Open kevin-verschaeve opened this issue 7 years ago • 2 comments

Hi,

Let me explain :)

I'm profiling an api, and I need to call my login page which return a token I need to pass to all my next calls.

Currently, to do this, I call /login, then set token json('token') and use that token in my header of the next requests in the same scenario. The issue is that I need that token for all my scenarios, so I have to login at the begining of each one.

That would be great to call only once the /login page and set my token as a variable available in all my next scenarios.

May be some code would be more explicit :

currenlty:

group login
    visit url('/login')
        header 'accept: "application/json"'
        method 'POST'
        body
        """
        {
            "username": "user",
            "password": "pass"
        }
        """
        expect status_code() == 200
        set token json('token')

scenario
    name "Feature 1"
    include login
    visit url('/feature-1')
        header 'content-type: "application/json"'
        header 'X-TOKEN: ' ~ token
        expect status_code() == 200

scenario
    name "Feature 2"
    include login
    visit url('/feature-2')
        header 'content-type: "application/json"'
        header 'X-TOKEN: ' ~ token
        expect status_code() == 200

include login is repeated and increase the time of the whole blackfire profile.

What would be great:

scenario
     name "Login"
     visit url('/login')
        header 'accept: "application/json"'
        method 'POST'
        body
        """
        {
            "username": "user",
            "password": "pass"
        }
        """
        expect status_code() == 200
        setGlobal token json('token')

scenario
    name "Feature 1"
    visit url('/feature-1')
        header 'content-type: "application/json"'
        header 'X-TOKEN: ' ~ token
        expect status_code() == 200

scenario
    name "Feature 2"
    visit url('/feature-2')
        header 'content-type: "application/json"'
        header 'X-TOKEN: ' ~ token
        expect status_code() == 200

What do you think ?

kevin-verschaeve avatar Feb 07 '19 14:02 kevin-verschaeve

I liked the idea.

The issue is that currently, scenarios can be run concurently. So we have to find a way to mark the scenario as a requirement.

iamluc avatar Feb 11 '19 14:02 iamluc

Most of the testing framework has some hooks to stick before / after scenarios. What do you think about something similar that are not executed concurently? For example:

beforeAll
     visit url('/login')
        header 'accept: "application/json"'
        method 'POST'
        body
        """
        {
            "username": "user",
            "password": "pass"
        }
        """
        expect status_code() == 200
        set token json('token')

beforeEach
     visit url('/refersh-token?token=' ~ token)
        header 'accept: "application/json"'
        method 'POST'
        expect status_code() == 200
        set token json('token')

scenario
    name "Feature 1"
    visit url('/feature-1')
        header 'content-type: "application/json"'
        header 'X-TOKEN: ' ~ token
        expect status_code() == 200

WDYT?

scyzoryck avatar Oct 25 '19 17:10 scyzoryck