import fs from 'node:fs'
import { describe, it, expect, vi } from 'vitest'
import { findStrategy } from '../src/utils/findStrategy.js'
import '../src/node.js'
vi.mock('fs', () => ({
default: {
readFileSync: vi.fn().mockReturnValue('random string')
}
}))
describe('selector strategies helper', () => {
it('should find an element using "css selector" method', () => {
const element = findStrategy('.red')
expect(element.using).toBe('css selector')
expect(element.value).toBe('.red')
})
it('should find an element using "id" method', () => {
const element = findStrategy('#purplebox')
expect(element.using).toBe('css selector')
expect(element.value).toBe('#purplebox')
})
it('should find an element using "name" method through jsonwp', () => {
const element = findStrategy('[name="searchinput"]', false)
expect(element.using).toBe('name')
expect(element.value).toBe('searchinput')
})
it('should find an element using "name" method through WC3', () => {
const element = findStrategy('[name="searchinput"]', true)
expect(element.using).toBe('css selector')
expect(element.value).toBe('[name="searchinput"]')
})
it.each([
{
selector: '[name="search.with.dot"]',
expectedValue: 'search.with.dot',
},
{
selector: '[name="search (with parentheses)"]',
expectedValue: 'search (with parentheses)',
},
{
selector: "[name='search with \"double quotes\"']",
expectedValue: 'search with "double quotes"',
},
{
selector: "[name=\"search with 'single quotes'\"]",
expectedValue: "search with 'single quotes'",
},
{
// Complex name selector example taken from Youi.tv selectors, see https://github.com/YOU-i-Labs/appium-youiengine-driver#selector-strategies
selector: "[name=\"searchinput[@isDisplayed='true']\"]",
expectedValue: "searchinput[@isDisplayed='true']",
},
])(
'should find an element using "name" method with special characters in the name (using selector %s)',
({ selector, expectedValue }) => {
const element = findStrategy(selector)
expect(element.using).toBe('name')
expect(element.value).toBe(expectedValue)
}
)
it('should find an element using "name" method by "name" strategy if isMobile is used even when w3c is used', () => {
const element = findStrategy('[name="searchinput"]', true, true)
expect(element.using).toBe('name')
expect(element.value).toBe('searchinput')
})
it('should find an element using "link text" method', () => {
const element = findStrategy('=GitHub Repo')
expect(element.using).toBe('link text')
expect(element.value).toBe('GitHub Repo')
})
it('should find an element using "partial link text" method', () => {
const element = findStrategy('*=new')
expect(element.using).toBe('partial link text')
expect(element.value).toBe('new')
})
it('should find an element using "id" method', () => {
const element = findStrategy('id=purplebox')
expect(element.using).toBe('id')
expect(element.value).toBe('purplebox')
})
it('should find an element using "tag name" method and tag format ', () => {
const element = findStrategy('')
expect(element.using).toBe('tag name')
expect(element.value).toBe('textarea')
})
it('should find an element using "tag name" method and tag format ', () => {
const element = findStrategy('