Skip to content

Instantly share code, notes, and snippets.

@d7my11
Last active August 19, 2019 13:20
Show Gist options
  • Save d7my11/1472b3faa36d495951c516ebe430d0d2 to your computer and use it in GitHub Desktop.
Save d7my11/1472b3faa36d495951c516ebe430d0d2 to your computer and use it in GitHub Desktop.
replace webdriver.io legacy commands with version 5 commands
// HOW TO USE IT
// just add a new file to your project directory
// change `BASE_DIR` -if needed-
// run `node upgrade-wdio-v5.js`
// then `git status` to see the changes
const fs = require('fs')
const BASE_DIR = 'ui-test/'
function readDirAsync (path) {
return new Promise(function (resolve, reject) {
fs.readdir(path, function (error, result) {
if (error) {
reject(error)
} else {
resolve(result)
}
})
})
}
const replaceRules = [
{ regex: /browser\.waitForVisible\((\w+)\)/g, replacement: '$($1).waitForDisplayed()' },
{ regex: /browser\.getText\((\w+)\)/g, replacement: '$($1).getText()' },
{ regex: /browser\.getAttribute\((\w+),\s(.+)\)/g, replacement: '$($1).getAttribute($2)' },
{ regex: /browser\.isVisible\((\w+)\)/g, replacement: '$($1).isDisplayed()' },
{ regex: /deleteCookie/g, replacement: 'deleteCookies' },
{ regex: /getCookie/g, replacement: 'getCookies' },
{ regex: /isVisible/g, replacement: 'isDisplayed' }
]
async function replaceOldStuff (dir = BASE_DIR) {
const dirs = await readDirAsync(dir)
const excludedFiles = /(yml|png|pdf)/
for (let i = 0; i < dirs.length; i++) {
if (dirs[i].endsWith('.js')) {
const file = fs.readFileSync(dir + dirs[i], 'utf8')
let result = file
replaceRules.forEach((rule) => {
result = result.replace(rule.regex, rule.replacement)
})
fs.writeFileSync(dir + dirs[i], result, 'utf8', function (err) {
if (err) return console.log(err)
})
} else if (!excludedFiles.test(dirs[i])) {
const foundInSub = await replaceOldStuff(dir + dirs[i] + '/')
if (foundInSub) return true
}
}
return false
}
replaceOldStuff()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment