最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CYPRESS store cy.request response in a variable - Stack Overflow

programmeradmin5浏览0评论

I have this one file that calls a login function

testing.js

var res = accounts.createSession(config.email_prod,config.password_prod,user_id)

on another file, I have this:

accounts.js

export function createSession(email,password,user_id){
    cy.request({
        method:'POST',
        url:config.accounts_prod + '/token',
        headers:{ 
            'authorization': 'Basic testestestestest'
        },
        qs:{
            'grant_type':'password',
            'username':email,
            'password':password
        }
    }).then((response)=>{
        var X = response.body.access_token      
        cy.log("create session " + x)
        login(response.body.access_token, user_id)
    })
}

export function login(token,user_id){
    var result = cy.request({
    method:'POST',
    url:config.ws_prod + '/login.pl',
    headers:{ 
        'authorization': token,
        'Content-Type' : 'application/x-www-form-urlencoded'
    },
    body:'user_id='+user_id+'&device_id='+device_id+'&os_type='+os_type
    })
    return token
}

so I want to store token value and return it to res variable on testing.js file but everytime I store token (in this example I store it inside X) and I try to print it, it always says undefined but I can do cy.log(token) and it was fine on login() function, but that's all it can do, it cannot be store into a variable is there another way for me to store token ?

I have this one file that calls a login function

testing.js

var res = accounts.createSession(config.email_prod,config.password_prod,user_id)

on another file, I have this:

accounts.js

export function createSession(email,password,user_id){
    cy.request({
        method:'POST',
        url:config.accounts_prod + '/token',
        headers:{ 
            'authorization': 'Basic testestestestest'
        },
        qs:{
            'grant_type':'password',
            'username':email,
            'password':password
        }
    }).then((response)=>{
        var X = response.body.access_token      
        cy.log("create session " + x)
        login(response.body.access_token, user_id)
    })
}

export function login(token,user_id){
    var result = cy.request({
    method:'POST',
    url:config.ws_prod + '/login.pl',
    headers:{ 
        'authorization': token,
        'Content-Type' : 'application/x-www-form-urlencoded'
    },
    body:'user_id='+user_id+'&device_id='+device_id+'&os_type='+os_type
    })
    return token
}

so I want to store token value and return it to res variable on testing.js file but everytime I store token (in this example I store it inside X) and I try to print it, it always says undefined but I can do cy.log(token) and it was fine on login() function, but that's all it can do, it cannot be store into a variable is there another way for me to store token ?

Share Improve this question asked Nov 14, 2018 at 3:59 fannybfannyb 511 gold badge1 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Maybe if i use a callback like parameter, then the second function will wait for the asynchronous task is over

   export function createSession(email,password,user_id,callback){
    cy.request({
        method:'POST',
        url:config.accounts_prod + '/token',
        headers:{ 
            'authorization': 'Basic testestestestest'
        },
        qs:{
            'grant_type':'password',
            'username':email,
            'password':password
        }
    }).then((response)=>{
        var X = response.body.access_token      
        cy.log("create session " + x)
        callback(response.body.access_token, user_id);

    })
}

var login = function (token,user_id) {
    var result = cy.request({
    method:'POST',
    url:config.ws_prod + '/login.pl',
    headers:{ 
        'authorization': token,
        'Content-Type' : 'application/x-www-form-urlencoded'
    },
    body:'user_id='+user_id+'&device_id='+device_id+'&os_type='+os_type
    })
    return token
}



// then call first fn

createSession(email,password,user_id,login);

I had almost the same issue. Comment on this answer helped me

// testing.js
let res = null;
before(() => {
    accounts.createSession(config.email_prod, config.password_prod, user_id).then((responseToken) => {
        res = responseToken;
        console.log('response token', responseToken);
    });
});

it('My tests ...', () => {
    console.log('...where I can use my session token', res);
});

// accounts.js
export function createSession(email, password, user_id) {
    cy.request({
        method: 'POST',
        url: config.accounts_prod + '/token',
        headers: {
            'authorization': 'Basic testestestestest'
        },
        qs: {
            'grant_type': 'password',
            'username': email,
            'password': password
        }
    }).then((response) => {
        var x = response.body.access_token  
        cy.log("create session " + x)
        login(response.body.access_token, user_id)
        return response.body.access_token; // needs return statement
    })
}
发布评论

评论列表(0)

  1. 暂无评论