Hello guys,
I am new to Codeceptjs with WebdriverIO and JavaScript and I just started to work with REST helper. I was wondering if there a way to store post response and retrieve values from it?
I would be really grateful if you could share your examples.
Feature('POST tests');
Scenario('Verify creating new user', async () => {
userData = {
name: faker.name.firstName(),
job: 'leader'
};
const res = await I.sendPostRequest('/api/users', userData);
expect(res.status).to.eql(201);
expect(res.data.name).to.eql(userData.name);
});
Scenario('Verify uploading a file', async () => {
form.append('attachment', fs.createReadStream('test/fixtures/test_image.png'));
const res = await I.sendPostRequest('https://httpbin.org/post', form);
expect(res.status).to.eql(200);
});
You can get access to the data object on the response, like so
1 Like
mirao
July 12, 2020, 2:35pm
3
Yes, the examples are referenced from CodeceptJS docs: https://codecept.io/examples/#rest-example-tests
result.status
contains HTTP response (e.g. 200 which means O.K.) and result.data
contains response (e.g. JSON response).
More details on https://github.com/axios/axios#response-schema (CodeceptJS uses axios library to perform REST requests)
Thank you a lot guys. Finally I was able to store response. Now it looks like this:
var response = await I.sendPostRequest(url, postBody);
var users = response.data;
console.log(users);
Console output:
[
{id:‘001c’, name:‘Max’, city:‘London’},
{id:‘002c’, name:‘Tom’, city:‘Bristol’}
]
My question is, how can I access name field in one or all records? For example if I want to verify it equals expected value?
Ex.
expect(users.name).to.eql(‘Max’);
samosa
July 13, 2020, 1:32pm
5
expect(users[0].name).to.equal(“Max”)
1 Like
Hello guys,
I am trying the same to fetch the response but somehow I get this error message:- ‘Property ‘status’ does not exist on type ‘void’.ts(2339)’
I see the return type of the I.sendGetRequest(’/rtos’) to be void.
Sample code:-
const res = await I.sendGetRequest(’/rtos’);
expect(res.status).to.eql(200); --> GIves compile time error on this line.
Please help me on this.
Thanks,
Deven J.
Hello Team,
Can someone please help me here ?