我有一个工作webdriverio框架,执行没有任何问题。但是最近我在其间包含了一个REST api调用,从那时起,任何跟随api调用的wdio命令都会失败并显示“不是函数”的错误消息
api响应成功。没有api,成功执行相同的wdio方法。
如果在两者之间存在api调用,那么$ feature失去了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);
});
});
而不是supertest,尝试使用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);
});