|
| 1 | +import { expect } from 'chai' |
| 2 | +import Locator from '../../../lib/locator.js' |
| 3 | +import { buildLocatorString } from '../../../lib/helper/Playwright.js' |
| 4 | + |
| 5 | +describe('buildLocatorString', () => { |
| 6 | + it('should make plain XPath relative', () => { |
| 7 | + const locator = new Locator({ xpath: '//div' }) |
| 8 | + expect(buildLocatorString(locator)).to.equal('xpath=.//div') |
| 9 | + }) |
| 10 | + |
| 11 | + it('should make XPath with parentheses (from at()) relative', () => { |
| 12 | + const locator = new Locator('.item').at(1) |
| 13 | + const result = buildLocatorString(locator) |
| 14 | + expect(result).to.match(/^xpath=\(\.\/\//) |
| 15 | + }) |
| 16 | + |
| 17 | + it('should make XPath from at().find() relative', () => { |
| 18 | + const locator = new Locator('.item').at(1).find('.label') |
| 19 | + const result = buildLocatorString(locator) |
| 20 | + expect(result).to.match(/^xpath=\(\.\/\//) |
| 21 | + }) |
| 22 | + |
| 23 | + it('should make XPath from first() relative', () => { |
| 24 | + const locator = new Locator('.item').first() |
| 25 | + const result = buildLocatorString(locator) |
| 26 | + expect(result).to.match(/^xpath=\(\.\/\//) |
| 27 | + }) |
| 28 | + |
| 29 | + it('should make XPath from last() relative', () => { |
| 30 | + const locator = new Locator('.item').last() |
| 31 | + const result = buildLocatorString(locator) |
| 32 | + expect(result).to.match(/^xpath=\(\.\/\//) |
| 33 | + }) |
| 34 | + |
| 35 | + it('should not double-prefix already relative XPath', () => { |
| 36 | + const locator = new Locator({ xpath: './/div' }) |
| 37 | + expect(buildLocatorString(locator)).to.equal('xpath=.//div') |
| 38 | + }) |
| 39 | + |
| 40 | + it('should handle XPath that was already relative inside parentheses', () => { |
| 41 | + const locator = new Locator({ xpath: '(.//div)[1]' }) |
| 42 | + expect(buildLocatorString(locator)).to.equal('xpath=(.//div)[1]') |
| 43 | + }) |
| 44 | + |
| 45 | + it('should return CSS locators unchanged', () => { |
| 46 | + const locator = new Locator('.my-class') |
| 47 | + expect(buildLocatorString(locator)).to.equal('.my-class') |
| 48 | + }) |
| 49 | +}) |
0 commit comments