MCPcopy Index your code
hub / github.com/codeceptjs/CodeceptJS / query

Function query

lib/command/query.js:8–98  ·  view source on GitHub ↗
(locator, context, options = {})

Source from the content-addressed store, hash-verified

6import { xpathLocator } from '../utils.js'
7
8export default async function query(locator, context, options = {}) {
9 const html = options.file ? fs.readFileSync(options.file, 'utf8') : await readStdin()
10
11 if (!html || !html.trim()) {
12 console.error('codeceptq: no HTML input. Pipe HTML via stdin or use --file <path>.')
13 process.exitCode = 2
14 return
15 }
16
17 let xpathExpr
18 let contextExpr = null
19 try {
20 xpathExpr = buildXPath(locator, options)
21 if (context) contextExpr = buildXPath(context, {})
22 } catch (err) {
23 console.error(`codeceptq: cannot build XPath: ${err.message}`)
24 process.exitCode = 2
25 return
26 }
27
28 const { doc, source } = htmlToDoc(html)
29
30 let nodes
31 try {
32 if (contextExpr) {
33 const ctxNodes = toArray(xpath.select(contextExpr, doc))
34 const seen = new Set()
35 nodes = []
36 for (const ctx of ctxNodes) {
37 for (const m of toArray(xpath.select(xpathExpr, ctx))) {
38 if (!seen.has(m)) {
39 seen.add(m)
40 nodes.push(m)
41 }
42 }
43 }
44 } else {
45 nodes = toArray(xpath.select(xpathExpr, doc))
46 }
47 } catch (err) {
48 console.error(`codeceptq: XPath evaluation failed for "${xpathExpr}": ${err.message}`)
49 process.exitCode = 2
50 return
51 }
52
53 const limit = parseInt(options.limit, 10) || 20
54 const snippetLen = parseInt(options.snippet, 10) || 500
55 const truncated = nodes.slice(0, limit)
56 const where = options.file || 'stdin'
57
58 if (options.json) {
59 process.stdout.write(
60 JSON.stringify(
61 {
62 locator,
63 context: context || null,
64 xpath: xpathExpr,
65 contextXPath: contextExpr,

Callers 1

codeceptq.jsFile · 0.50

Calls 9

readStdinFunction · 0.85
buildXPathFunction · 0.85
htmlToDocFunction · 0.85
renderSnippetFunction · 0.85
quoteFunction · 0.85
pushMethod · 0.80
logMethod · 0.80
toArrayFunction · 0.70
addMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…