Skip to content

Commit 0d8045c

Browse files
committed
Implements Locator.or_
1 parent 37a9833 commit 0d8045c

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

lib/playwright/locator.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,18 @@ defmodule Playwright.Locator do
902902
locator(context, "nth=#{index}")
903903
end
904904

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

908918
# @spec page(Locator.t()) :: Page.t()
909919
# def page(locator)

test/api/locator_test.exs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,57 @@ defmodule Playwright.LocatorTest do
641641
end
642642
end
643643

644+
describe "Locator.or_/2" do
645+
test "returns a locator that matches either given condition", %{page: page} do
646+
page
647+
|> Page.set_content("""
648+
<section>
649+
<div class="pink">pink</div>
650+
<div class="orange">orange</div>
651+
<div class="green">green</div>
652+
</section>
653+
""")
654+
655+
pink = Page.locator(page, ".pink")
656+
orange = Page.locator(page, ".orange")
657+
redish = Locator.or_(pink, orange)
658+
assert Locator.text_content(redish) == "pink"
659+
660+
page
661+
|> Page.set_content("""
662+
<section>
663+
<div class="orange">orange</div>
664+
<div class="green">green</div>
665+
</section>
666+
""")
667+
668+
pink = Page.locator(page, ".pink")
669+
orange = Page.locator(page, ".orange")
670+
redish = Locator.or_(pink, orange)
671+
672+
assert Locator.text_content(redish) == "orange"
673+
end
674+
675+
test "raises an error when the given locators don't share a frame", %{page: page, browser: browser} do
676+
other_page = Playwright.Browser.new_page(browser)
677+
678+
on_exit(:ok, fn ->
679+
Playwright.Page.close(other_page)
680+
end)
681+
page
682+
|> Page.set_content("<div></div>")
683+
684+
div_locator = Page.locator(page, "div")
685+
other_page
686+
|> Page.set_content("<span></span>")
687+
span_locator = Page.locator(other_page, "span")
688+
689+
assert_raise ArgumentError, "Locators must belong to the same frame", fn ->
690+
Locator.or_(div_locator, span_locator)
691+
end
692+
end
693+
end
694+
644695
describe "Locator.press/2" do
645696
test "focuses an element and 'presses' a key within it", %{page: page} do
646697
locator = Page.locator(page, "input")

0 commit comments

Comments
 (0)