Skip to content

Commit

Permalink
Implements Locator.or_
Browse files Browse the repository at this point in the history
  • Loading branch information
ry4n1m3 committed Nov 13, 2024
1 parent 37a9833 commit 0d8045c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/playwright/locator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -902,8 +902,18 @@ defmodule Playwright.Locator do
locator(context, "nth=#{index}")
end

# @spec or(Locator.t(), Locator.t()) :: Locator.t()
# def or(locator, other)
@doc """
Returns a new `Playwright.Locator` that matches either of the conditions of the given locators.
This implements the `or` function for locators, but `or` is not an allowed function name in elixir.
"""
@spec or_(Locator.t(), Locator.t()) :: Locator.t()
def or_(%Locator{frame: frame} = locator, %Locator{frame: frame} = other) do
new(frame, locator.selector <> ">> internal:or=" <> Jason.encode!(other.selector))
end
def or_(_, _) do
raise ArgumentError, "Locators must belong to the same frame"
end

# @spec page(Locator.t()) :: Page.t()
# def page(locator)
Expand Down
51 changes: 51 additions & 0 deletions test/api/locator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,57 @@ defmodule Playwright.LocatorTest do
end
end

describe "Locator.or_/2" do
test "returns a locator that matches either given condition", %{page: page} do
page
|> Page.set_content("""
<section>
<div class="pink">pink</div>
<div class="orange">orange</div>
<div class="green">green</div>
</section>
""")

pink = Page.locator(page, ".pink")
orange = Page.locator(page, ".orange")
redish = Locator.or_(pink, orange)
assert Locator.text_content(redish) == "pink"

page
|> Page.set_content("""
<section>
<div class="orange">orange</div>
<div class="green">green</div>
</section>
""")

pink = Page.locator(page, ".pink")
orange = Page.locator(page, ".orange")
redish = Locator.or_(pink, orange)

assert Locator.text_content(redish) == "orange"
end

test "raises an error when the given locators don't share a frame", %{page: page, browser: browser} do
other_page = Playwright.Browser.new_page(browser)

on_exit(:ok, fn ->
Playwright.Page.close(other_page)
end)
page
|> Page.set_content("<div></div>")

div_locator = Page.locator(page, "div")
other_page
|> Page.set_content("<span></span>")
span_locator = Page.locator(other_page, "span")

assert_raise ArgumentError, "Locators must belong to the same frame", fn ->
Locator.or_(div_locator, span_locator)
end
end
end

describe "Locator.press/2" do
test "focuses an element and 'presses' a key within it", %{page: page} do
locator = Page.locator(page, "input")
Expand Down

0 comments on commit 0d8045c

Please sign in to comment.