Skip to content

Commit

Permalink
fix: type issue after xpath update
Browse files Browse the repository at this point in the history
  • Loading branch information
AnWeber committed Jul 14, 2023
1 parent 03cb593 commit 4c02fc3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
17 changes: 13 additions & 4 deletions src/plugins/xml/provideAssertValueXPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { log } from '../../io';
import * as models from '../../models';
import * as utils from '../../utils';
import { isNode, parseFromString } from './nodeUtils';
import { SelectedValue, select } from 'xpath';
import { SelectReturnType, SelectedValue, select } from 'xpath';

export async function provideAssertValueXPath(type: string, value: string | undefined, response: models.HttpResponse) {
if (type === 'xpath' && value && response.body) {
Expand All @@ -13,16 +13,25 @@ export async function provideAssertValueXPath(type: string, value: string | unde
try {
const node = parseFromString(body);
const results = select(value, node);
return getSelectReturnType(results);
} catch (err) {
log.warn(`xpath ${value} throws error`, err);
}
}
return false;
}

export function getSelectReturnType(results: SelectReturnType) {
if (results !== null) {
if (Array.isArray(results)) {
if (results.length === 1) {
const resultNode = results[0];
return getTextContent(resultNode);
}
return results.map(obj => getTextContent(obj));
} catch (err) {
log.warn(`xpath ${value} throws error`, err);
}
}
return false;
return getTextContent(results);
}

function getTextContent(resultNode: SelectedValue) {
Expand Down
11 changes: 3 additions & 8 deletions src/plugins/xml/xpathVariableReplacer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { log } from '../../io';
import { ProcessorContext, VariableType } from '../../models';
import * as utils from '../../utils';
import { getNode, isNode } from './nodeUtils';
import { getNode } from './nodeUtils';
import { getSelectReturnType } from './provideAssertValueXPath';
import { select } from 'xpath';

export async function xpathVariableReplacer(
Expand All @@ -16,13 +17,7 @@ export async function xpathVariableReplacer(
const node = getNode(match.groups.variable, variables);
if (node) {
const results = select(match.groups.xpath, node);
if (results.length === 1) {
const resultNode = results[0];
if (isNode(resultNode)) {
return resultNode.textContent;
}
return resultNode;
}
return getSelectReturnType(results);
}
} catch (err) {
(scriptConsole || log).warn(`xpath ${match.groups.xpath} throws error`, err);
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"skipLibCheck": true,
"noUnusedParameters": true,
"noImplicitAny": true,
"noImplicitThis": true,
Expand Down

0 comments on commit 4c02fc3

Please sign in to comment.