Skip to content

Instantly share code, notes, and snippets.

@ahobson
Created December 16, 2022 15:23
Show Gist options
  • Save ahobson/91df90560dc4e1ca7ab156d3c7642f3e to your computer and use it in GitHub Desktop.
Save ahobson/91df90560dc4e1ca7ab156d3c7642f3e to your computer and use it in GitHub Desktop.
cypress to playwright helper script
#!/usr/bin/env ruby
# using ruby since it is already installed on macOS
if ARGV.length != 1
STDERR.puts "Usage: #{$0} cypress_test_file.js"
exit(2)
end
File.readlines(ARGV[0]).each do |line|
print(
line.
gsub(/describe\((.*),.*/,
'test.describe(\1, () => {').
gsub(/it\((.*),.*/,
'test(\1, async ({page}) => {').
gsub('cy.get(',
'await page.locator(').
gsub(/cy.contains(.*);/,
'await expect(page.getByText\1).toBeVisible();').
gsub(/await (page.locator.*).should\('be.disabled'\)/,
'await expect(\1).toBeDisabled()').
gsub(/await (page.locator.*).should\('be.enabled'\)/,
'await expect(\1).toBeEnabled()').
gsub(/\.clear\(\)\.type/,
'.type').
gsub(/await (page.locator.*).type\((.*)\).blur\(\)/,
'await \1.type(\2); await \1.blur()').
gsub(/await (page.locator.*).should('be.empty')/,
'await expect(\1).toBeEmpty()').
gsub(/\.select\(([^)]*)\)/,
'.selectOption({ label: \1})').
gsub(/await (page.locator.*).contains\((.*)\)/,
'await expect(\1).toContainText(\2)')
)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment