我有一个可以正常运行的webdriverio框架,该框架可以正常执行。但是最近我在两者之间加入了一个REST api调用,从那时起,此api调用之后的所有wdio命令都失败,并显示错误消息“不是函数”
api响应成功。如果没有api,则相同的wdio方法将成功执行。
如果两者之间存在api调用,则$功能会失去对wdio实例的跟踪,这很奇怪。我正在努力解决此问题,并且没有之间的api调用,无法创建我的测试自动化框架。因此,对此表示感谢。
为方便起见,下面提供了我使用的示例代码
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);
});
});
答案 0 :(得分:2)
尝试使用sync-request代替超级测试。这将使Rest请求同步以用于测试环境。
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);
});