Skip to content

Commit 04b137d

Browse files
committed
refactor: migrate Appium helper to ESM
- Convert require() to import statements - Change webdriverio from dynamic require to static import - Import webdriverio as namespace (import * as webdriverio) - Import all dependencies at the top of the file: - ElementNotFound from './errors/ElementNotFound.js' - dontSeeElementError from './errors/ElementAssertion.js' - Remove inline require statements from methods: - dontSeeElement() - seeElement() - waitForVisible() - waitForInvisible() - Use Locator.build() instead of new Locator() for ESM compatibility - Maintain export default Appium at the end - All imports use .js extensions for ESM compliance Verified: - Module loads successfully - No require() statements remaining - Tests run without errors
1 parent 6d59918 commit 04b137d

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

lib/helper/Appium.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
let webdriverio
2-
1+
import * as webdriverio from 'webdriverio'
32
import fs from 'fs'
43
import axios from 'axios'
54
import { v4 as uuidv4 } from 'uuid'
@@ -10,6 +9,8 @@ import { truth } from '../assert/truth.js'
109
import recorder from '../recorder.js'
1110
import Locator from '../locator.js'
1211
import ConnectionRefused from './errors/ConnectionRefused.js'
12+
import ElementNotFound from './errors/ElementNotFound.js'
13+
import { dontSeeElementError } from './errors/ElementAssertion.js'
1314

1415
const mobileRoot = '//*'
1516
const webRoot = 'body'
@@ -181,7 +182,6 @@ class Appium extends Webdriver {
181182
this.appiumV2 = config.appiumV2 || true
182183
this.axios = axios.create()
183184

184-
webdriverio = require('webdriverio')
185185
if (!config.appiumV2) {
186186
console.log('The Appium core team does not maintain Appium 1.x anymore since the 1st of January 2022. Appium 2.x is used by default.')
187187
console.log('More info: https://bit.ly/appium-v2-migration')
@@ -1549,11 +1549,9 @@ class Appium extends Webdriver {
15491549
// For mobile native apps, use safe isDisplayed wrapper
15501550
const parsedLocator = parseLocator.call(this, locator)
15511551
const res = await this._locate(parsedLocator, false)
1552-
const { truth } = require('../assert/truth')
1553-
const Locator = require('../locator')
15541552

15551553
if (!res || res.length === 0) {
1556-
return truth(`elements of ${new Locator(parsedLocator)}`, 'to be seen').negate(false)
1554+
return truth(`elements of ${Locator.build(parsedLocator)}`, 'to be seen').negate(false)
15571555
}
15581556

15591557
const selected = []
@@ -1563,7 +1561,7 @@ class Appium extends Webdriver {
15631561
}
15641562

15651563
try {
1566-
return truth(`elements of ${new Locator(parsedLocator)}`, 'to be seen').negate(selected)
1564+
return truth(`elements of ${Locator.build(parsedLocator)}`, 'to be seen').negate(selected)
15671565
} catch (err) {
15681566
throw err
15691567
}
@@ -1714,10 +1712,6 @@ class Appium extends Webdriver {
17141712
// For mobile native apps, use safe isDisplayed wrapper
17151713
const parsedLocator = parseLocator.call(this, locator)
17161714
const res = await this._locate(parsedLocator, true)
1717-
const ElementNotFound = require('./errors/ElementNotFound')
1718-
const { truth } = require('../assert/truth')
1719-
const { dontSeeElementError } = require('./errors/ElementAssertion')
1720-
const Locator = require('../locator')
17211715

17221716
if (!res || res.length === 0) {
17231717
throw new ElementNotFound(parsedLocator)
@@ -1730,7 +1724,7 @@ class Appium extends Webdriver {
17301724
}
17311725

17321726
try {
1733-
return truth(`elements of ${new Locator(parsedLocator)}`, 'to be seen').assert(selected)
1727+
return truth(`elements of ${Locator.build(parsedLocator)}`, 'to be seen').assert(selected)
17341728
} catch (e) {
17351729
dontSeeElementError(parsedLocator)
17361730
}
@@ -1784,7 +1778,6 @@ class Appium extends Webdriver {
17841778
// For mobile native apps, use safe isDisplayed wrapper
17851779
const parsedLocator = parseLocator.call(this, locator)
17861780
const aSec = sec || this.options.waitForTimeoutInSeconds
1787-
const Locator = require('../locator')
17881781

17891782
return this.browser.waitUntil(
17901783
async () => {
@@ -1801,7 +1794,7 @@ class Appium extends Webdriver {
18011794
},
18021795
{
18031796
timeout: aSec * 1000,
1804-
timeoutMsg: `element (${new Locator(parsedLocator)}) still not visible after ${aSec} sec`,
1797+
timeoutMsg: `element (${Locator.build(parsedLocator)}) still not visible after ${aSec} sec`,
18051798
},
18061799
)
18071800
}
@@ -1816,7 +1809,6 @@ class Appium extends Webdriver {
18161809
// For mobile native apps, use safe isDisplayed wrapper
18171810
const parsedLocator = parseLocator.call(this, locator)
18181811
const aSec = sec || this.options.waitForTimeoutInSeconds
1819-
const Locator = require('../locator')
18201812

18211813
return this.browser.waitUntil(
18221814
async () => {
@@ -1831,7 +1823,7 @@ class Appium extends Webdriver {
18311823

18321824
return selected.length === 0
18331825
},
1834-
{ timeout: aSec * 1000, timeoutMsg: `element (${new Locator(parsedLocator)}) still visible after ${aSec} sec` },
1826+
{ timeout: aSec * 1000, timeoutMsg: `element (${Locator.build(parsedLocator)}) still visible after ${aSec} sec` },
18351827
)
18361828
}
18371829

0 commit comments

Comments
 (0)