Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/python/tests/elements/test_locators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from pathlib import Path

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with


def test_class_name():
Expand Down Expand Up @@ -82,3 +85,54 @@ def test_xpath(driver):
assert element.get_attribute("type") == "radio"

driver.quit()


RELATIVE_LOCATORS_PAGE = (Path(__file__).parent.parent / "resources" / "relative_locators.html").as_uri()


def test_relative_above(driver):
driver.get(RELATIVE_LOCATORS_PAGE)
email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"})
email = driver.find_element(email_locator)

assert email.get_attribute("id") == "email"


def test_relative_below(driver):
driver.get(RELATIVE_LOCATORS_PAGE)
password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
password = driver.find_element(password_locator)

assert password.get_attribute("id") == "password"


def test_relative_to_left_of(driver):
driver.get(RELATIVE_LOCATORS_PAGE)
cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"})
cancel = driver.find_element(cancel_locator)

assert cancel.get_attribute("id") == "cancel"


def test_relative_to_right_of(driver):
driver.get(RELATIVE_LOCATORS_PAGE)
submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"})
submit = driver.find_element(submit_locator)

assert submit.get_attribute("id") == "submit"


def test_relative_near(driver):
driver.get(RELATIVE_LOCATORS_PAGE)
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
email = driver.find_element(email_locator)

assert email.get_attribute("id") == "email"


def test_relative_chaining(driver):
driver.get(RELATIVE_LOCATORS_PAGE)
submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"})
submit = driver.find_element(submit_locator)

assert submit.get_attribute("id") == "submit"
23 changes: 23 additions & 0 deletions examples/python/tests/resources/relative_locators.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Relative Locators</title>
<style>
form { width: 300px; font-family: sans-serif; }
label, input { display: block; margin-bottom: 8px; }
input { width: 100%; }
button { margin-top: 8px; }
</style>
</head>
<body>
<form>
<label id="lbl-email" for="email">Email Address</label>
<input id="email" type="text">
<label id="lbl-password" for="password">Password</label>
<input id="password" type="password">
<button id="cancel" type="button">Cancel</button>
<button id="submit" type="button">Submit</button>
</form>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L95-L96" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
Expand All @@ -444,8 +444,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email"));
{{< /tab >}}
{{< tab header="Python" >}}
password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L103-L104" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
Expand All @@ -471,8 +471,8 @@ we can locate the cancel button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit"));
{{< /tab >}}
{{< tab header="Python" >}}
cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L111-L112" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
Expand All @@ -498,8 +498,8 @@ we can locate the submit button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L119-L120" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
Expand Down Expand Up @@ -527,8 +527,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L127-L128" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email"));
Expand All @@ -553,8 +553,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L135-L136" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L95-L96" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
Expand All @@ -442,8 +442,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email"));
{{< /tab >}}
{{< tab header="Python" >}}
password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L103-L104" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
Expand All @@ -468,8 +468,8 @@ we can locate the cancel button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit"));
{{< /tab >}}
{{< tab header="Python" >}}
cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L111-L112" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
Expand All @@ -494,8 +494,8 @@ we can locate the submit button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L119-L120" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
Expand All @@ -522,8 +522,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L127-L128" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email"));
Expand All @@ -547,8 +547,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L135-L136" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L95-L96" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
Expand All @@ -445,8 +445,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email"));
{{< /tab >}}
{{< tab header="Python" >}}
password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L103-L104" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
Expand All @@ -471,8 +471,8 @@ we can locate the cancel button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit"));
{{< /tab >}}
{{< tab header="Python" >}}
cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L111-L112" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
Expand All @@ -497,8 +497,8 @@ we can locate the submit button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L119-L120" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
Expand All @@ -525,8 +525,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L127-L128" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email"));
Expand All @@ -550,8 +550,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L135-L136" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).above(By.id("password"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").above({By.ID: "password"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L95-L96" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.TagName("input")).Above(By.Id("password"));
Expand All @@ -446,8 +446,8 @@ we can locate the text field element using the fact that it is an "input" elemen
{{< tab header="Java" >}}
By passwordLocator = RelativeLocator.with(By.tagName("input")).below(By.id("email"));
{{< /tab >}}
{{< tab header="Python" >}}
password_locator = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L103-L104" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var passwordLocator = RelativeBy.WithLocator(By.TagName("input")).Below(By.Id("email"));
Expand All @@ -472,8 +472,8 @@ we can locate the cancel button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By cancelLocator = RelativeLocator.with(By.tagName("button")).toLeftOf(By.id("submit"));
{{< /tab >}}
{{< tab header="Python" >}}
cancel_locator = locate_with(By.TAG_NAME, "button").to_left_of({By.ID: "submit"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L111-L112" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var cancelLocator = RelativeBy.WithLocator(By.tagName("button")).LeftOf(By.Id("submit"));
Expand All @@ -498,8 +498,8 @@ we can locate the submit button element using the fact that it is a "button" ele
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L119-L120" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).RightOf(By.Id("cancel"));
Expand All @@ -526,8 +526,8 @@ but its associated [input label element](https://developer.mozilla.org/en-US/doc
{{< tab header="Java" >}}
By emailLocator = RelativeLocator.with(By.tagName("input")).near(By.id("lbl-email"));
{{< /tab >}}
{{< tab header="Python" >}}
email_locator = locate_with(By.TAG_NAME, "input").near({By.ID: "lbl-email"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L127-L128" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var emailLocator = RelativeBy.WithLocator(By.tagName("input")).Near(By.Id("lbl-email"));
Expand All @@ -551,8 +551,8 @@ You can also chain locators if needed. Sometimes the element is most easily iden
{{< tab header="Java" >}}
By submitLocator = RelativeLocator.with(By.tagName("button")).below(By.id("email")).toRightOf(By.id("cancel"));
{{< /tab >}}
{{< tab header="Python" >}}
submit_locator = locate_with(By.TAG_NAME, "button").below({By.ID: "email"}).to_right_of({By.ID: "cancel"})
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L135-L136" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var submitLocator = RelativeBy.WithLocator(By.tagName("button")).Below(By.Id("email")).RightOf(By.Id("cancel"));
Expand Down