SeleniumBase icon indicating copy to clipboard operation
SeleniumBase copied to clipboard

Comparing Playwright to SeleniumBase

Open chiangbar opened this issue 1 year ago • 3 comments

Dear seleniumbase author, I am currently building a web testing platform. Can you compare the playwright library from a functional and performance perspective? This will also allow more engineers to recognize the great work of seleniumbase.🤣

chiangbar avatar Sep 26 '24 23:09 chiangbar

Both Playwright and SeleniumBase support different formats.

Playwright:

  • Python
  • pytest
  • .NET
  • Java
  • NodeJS.

SeleniumBase:

  • Python -> SB() and Driver()
  • pytest -> BaseCase class and sb fixture
  • BDD (Gherkin)

Since SeleniumBase doesn't have .NET, Java, or NodeJS formats, then Playwright is the only available choice if you need one of those. However, if you're a Python user, then we can make some meaningful comparisons.

Let's compare pytest formats for both:

Here's a Playwright example from https://playwright.dev/python/docs/writing-tests:

import re
from playwright.sync_api import Page, expect

def test_has_title(page: Page):
    page.goto("https://playwright.dev/")
    expect(page).to_have_title(re.compile("Playwright"))

def test_get_started_link(page: Page):
    page.goto("https://playwright.dev/")
    page.get_by_role("link", name="Get started").click()
    expect(page.get_by_role("heading", name="Installation")).to_be_visible()

Here's the SeleniumBase BaseCase version of that same example:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class MyTests(BaseCase):
    def test_has_title(self):
        self.open("https://playwright.dev/")
        self.assert_title_contains("Playwright")

    def test_get_started_link(self):
        self.open("https://playwright.dev/")
        self.click_link("Get started")
        self.assert_text("Installation", "h1")

And here's the SeleniumBase pytest fixture version: (No imports are needed)

def test_has_title(sb):
    sb.open("https://playwright.dev/")
    sb.assert_title_contains("Playwright")

def test_get_started_link(sb):
    sb.open("https://playwright.dev/")
    sb.click_link("Get started")
    sb.assert_text("Installation", "h1")

So for script simplicity, SeleniumBase is more simplified.

Let's compare Playwright code-gen to the SeleniumBase Recorder:

Playwright generated code:

from playwright.sync_api import Page, expect

def test_example(page: Page) -> None:
    page.got("https://demo.playwright.dev/todomvc/#/")
    page.get_by_placeholder("What needs to be done?").click()
    page.get_by_placeholder("What needs to be done?").fill("water the plants")
    page.get_by_placeholder("What needs to be done?").press("Enter")
    page.get_by_placeholder("What needs to be done?").fill("feed the dog")
    page.get_by_placeholder("What needs to be done?").press("Enter")
    page.locator("li").filter(has_text="water the plants").get_by_label("Toggle Todo").check()
    page.get_by_role("link", name="Completed").click()
    expect(page.get_by_test_id("todo-title")).to_contain_text("water the plants")
    page.get_by_role("link", name="Active").click()
    expect(page.get_by_test_id("todo-title")).to_contain_text("feed the dog")

Here's the same thing autogenerated with the SeleniumBase Recorder:

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class RecorderTest(BaseCase):
    def test_recording(self):
        self.open("https://demo.playwright.dev/todomvc/#/")
        self.type("input.new-todo", "water the plants\n")
        self.type("input.new-todo", "feed the dog\n")
        self.check_if_unchecked('input[aria-label="Toggle Todo"]')
        self.click('a[href="#/completed"]')
        self.assert_exact_text("water the plants", 'label[data-testid="todo-title"]')
        self.click('a[href="#/active"]')
        self.assert_exact_text("feed the dog", 'label[data-testid="todo-title"]')

(When using the Recorder, use SHIFT+7 to switch into Assert Text Mode, then ESC once to go back.)

As seen by the autogenerated code, the SeleniumBase version was more simplified.

Let's talk about methods: SeleniumBase has more Python methods than Playwright.

SeleniumBase also has a lot more tools, such as UC Mode, which lets you bypass CAPTCHAs. (Playwright does not have that stealth ability.)

Here are some other tools that are unique to SeleniumBase:


Performance-wise, both frameworks are very similar. SeleniumBase also has several command-line options for speeding up tests, such as Reuse-Session Mode (--rs), which chains your tests together to reuse the same browser window. You can also change the pageLoadStrategy, Skip JS waits, and use headless mode for a speed boost.

mdmintz avatar Sep 27 '24 01:09 mdmintz

https://github.com/WaterLoran/Loran

小伙子, 用我的框架, AOP+描述式 的, 底层用的就是SB

WaterLoran avatar Oct 12 '24 08:10 WaterLoran

https://zhiminzhan.medium.com/correct-wrong-playwrights-advantage-over-selenium-part-1-playwright-is-modern-and-faster-than-0a652c7e9ee7

WaterLoran avatar Oct 12 '24 08:10 WaterLoran