I am have a working webdriverio framework which executes without any issue. But recently I included a REST api call in between and since then any wdio commands followed the api call is failing with the error message that "is not a function"
The api response is successful. Without api the same wdio methods are executed successfully.
Something happens weird that $ functionality loses the track of wdio instance if there is an api call in between. I am struggling to fix this and without having api call in between , cant create my test automation framework. Hence input on this will be much appreciated.
The sample code I used is given below for your refeence
import { expect } from 'chai';
import apiCheck from 'src/ext/api.check';
import lPage from 'src/pages/login.page';
import sPage from 'src/pages/summary.page';
let superTest = require('supertest');
const request = superTest('http://localhost:3000/api/');
const apiEndPoint = 'auth/login';
const headerOrigin = 'http://localhost:3001';
const headerCookie = '__DEEI_SESSION__=abcd1234';
describe('fund part ', () => {
beforeEach(() => {
browser.url('login?');
});
it('Get API Response in seperate ts file and run through wdio steps', async () => {
let accountNo = lPage.getLoginAccountType('Standard');
try {
let res = await apiCheck.getApiLoginData(accountNo);
console.log(res);
} catch (error) {
console.log(error);
}
lPage.enterLoginData(accountNo, '2dsXS£');
});
it('Get API Response in same it block and run through wdio steps', async () => {
let accountNo = lPage.getLoginAccountType('Standard');
let requestBody = `{"username": \"${accountNo}\", "password": "2dsXS£$"}`;
let response = await request
.post(apiEndPoint)
.send(requestBody)
.set('Origin', headerOrigin)
.set('Content-Type', 'application/json')
.set('Cookie', headerCookie)
.expect(200);
lPage.enterLoginData(accountNo, password);
});
});
Instead of supertest, try using sync-request. This would make Rest request synchronous for use in testing environment.
const request = require('sync-request');
it('Get API Response in same it block and run through wdio steps', () => {
let accountNo = lPage.getLoginAccountType('Standard');
let requestBody = `{"username": \"${accountNo}\", "password": "2dsXS£$"}`;
let response = request(
'POST',
'_SOME_POST_END_POINT',
{
headers: { _SOME_HEADER_IF_NEEDED_},
json: {_JSON_FOR_POST}
}
);
lPage.enterLoginData(accountNo, password);
});