diff --git a/examples/python/tests/elements/test_locators.py b/examples/python/tests/elements/test_locators.py index 3622b70d97f2..1b552bf16582 100644 --- a/examples/python/tests/elements/test_locators.py +++ b/examples/python/tests/elements/test_locators.py @@ -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(): @@ -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" diff --git a/examples/python/tests/resources/relative_locators.html b/examples/python/tests/resources/relative_locators.html new file mode 100644 index 000000000000..05d2b0927d79 --- /dev/null +++ b/examples/python/tests/resources/relative_locators.html @@ -0,0 +1,23 @@ + + + + + Relative Locators + + + +
+ + + + + + +
+ + diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.en.md b/website_and_docs/content/documentation/webdriver/elements/locators.en.md index a4b25ce9f2a7..d5e01be974c0 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.en.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.en.md @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.ja.md b/website_and_docs/content/documentation/webdriver/elements/locators.ja.md index 23c3a9739602..6d9091bffac9 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.ja.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.ja.md @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md b/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md index 938657a01042..189058081435 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.pt-br.md @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); diff --git a/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md b/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md index 7f82676ad8dc..65ce76149658 100644 --- a/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/elements/locators.zh-cn.md @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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")); @@ -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"));