Skip to content

Commit

Permalink
fix crash when calling highlight on an xpath locator
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jul 4, 2024
1 parent f06685c commit 6cd5def
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/browser/xpath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ const createXmlDom = (node: Element | Document) => {
return undefined
}
const result = new DOMParser().parseFromString(createXml(root), 'text/xml')
return node instanceof Element ? result.getElementById(node.id) : result
// if it's an html element where its id exists in the xml dom too, then return the element
// in the dom matching that id, as it can be used as the context (.) for xpath selectors
return node instanceof Element ? result.getElementById(node.id) ?? result : result
}

const matchXmlElementToHtmlElement = (root: Element | Document, element: Element) =>
Expand All @@ -148,6 +150,7 @@ export default {
if (!isUi5()) {
return []
}
debugger
return evaluateXPathToNodes<Element>(
fixSelectorContext(root, selector),
createXmlDom(root),
Expand All @@ -166,6 +169,7 @@ export default {
if (!isUi5()) {
return undefined
}
debugger
const node = getRootElement(root)
if (node === null) {
return undefined
Expand Down
4 changes: 2 additions & 2 deletions tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Ui5Tester {
this.selectorEngineId = `ui5_${selectorEngineName}`
}
navigateToUi5DocsPage = async (page: Page, path: `/${string}`) => {
await page.goto(`https://ui5.sap.com/1.112.3${path}`)
await page.goto(`https://ui5.sap.com/1.125.1${path}`)
await page.waitForSelector(
`${this.selectorEngineId}=${
this.selectorEngineName === 'css' ? '*' : '//*'
Expand All @@ -22,7 +22,7 @@ export class Ui5Tester {
navigateToControlSample = (page: Page, lib: string, sampleId: string) =>
this.navigateToUi5DocsPage(
page,
`/resources/sap/ui/documentation/sdk/index.html?sap-ui-xx-sample-id=${sampleId}&sap-ui-xx-sample-lib=${lib}&sap-ui-xx-sample-origin=.&sap-ui-xx-dk-origin=https://ui5.sap.com`,
`/resources/sap/ui/documentation/sdk/index.html?sap-ui-xx-sample-id=${sampleId}&sap-ui-xx-sample-lib=${lib}&sap-ui-xx-sample-origin=.&sap-ui-xx-dk-origin=https%3A%2F%2Fui5.sap.com&sap-ui-theme=sap_horizon_dark&sap-ui-rtl=false&sap-ui-density=sapUiSizeCompact`,
)

registerSelectorEngine = async () =>
Expand Down
4 changes: 4 additions & 0 deletions tests/xpath.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ test.describe('ui5 site', () => {
page.locator('#__toolbar0').locator('ui5_xpath=./following-sibling::*').first(),
).toHaveAttribute('id', '__toolbar1'))
})
test.only('highlight', ({ page }) =>
// highlighting causes the root node to be the root html element instead of the document object for some reason,
// which we need to have handling for
page.locator('ui5_xpath=//sap.m.Button[1]').highlight())
})
test.describe('demo apps', () => {
test('multiple root nodes', async ({ page }) => {
Expand Down

0 comments on commit 6cd5def

Please sign in to comment.