Selenium to Playwright Migration
My recommendations for anyone starting to learn Playwright from the ground zero are as follows, especially if you come from a selenium background.
- Imagine you’re using
driver.find_element(By.CSS_SELECTOR, ‘#user-name’)
it to identify a text box in Selenium. In Playwright, if you’re usingpage.locator(‘#user-name’)
it, you are just making the script work, not seizing the opportunity to enhance your skills and broaden your toolkit. I have captured a few things about locators in Playwright. https://medium.com/@ceroshjacob/playwright-locators-targeting-elements-efficiently-ba2352ba6d76 - If you are trying to find a replacement for the statement
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ‘#user-name’)))
before entering the user name while using Playwright, then you should read Playwright documentation more in detail. You can have a quick read-through about the auto-wait strategy of Playwright: https://medium.com/@ceroshjacob/playwrights-auto-wait-strategy-e070066c27c2 - If you feel the urge to replicate each user name entry in Playwright inside a
try…finally
statement, similar to Selenium, it’s a sign of habit. You are not accepting the invitation to explore the Playwright's unique features. you can understand the various retry strategy Playwright offers: https://medium.com/@ceroshjacob/playwrights-retry-feature-for-individual-test-steps-fb866f63bed2
In short, understand that Playwright has much more to offer. If something is done similarly, always question yourself about the reason for migration. Always refer to the Playwright’s documentation. Find a mentor and engage for a code review. Refer to a video once you find a specific topic and need an example to understand the feature implementation. Never start the research from the video, as many outdated videos exist. Since the Playwright releases are frequent, old videos will miss many feature advancements. For example, how you handle the cookie dialogue when it appears in the latest version is much more elegant than the old way.
My recent publication compiled a comprehensive collection of 100 similar typescript programs. Each program not only elucidates essential Typescript concepts and expounds upon the significance of test automation but also provides practical guidance on its implementation using Playwright. This resource will undoubtedly be valuable if you look deeper into similar topics.