Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py]: move python code to test_locators.py #2102

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions examples/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from urllib.request import urlopen
import requests
from requests.auth import HTTPBasicAuth
from http.server import HTTPServer, SimpleHTTPRequestHandler
from threading import Thread

import pytest
from selenium import webdriver
Expand Down Expand Up @@ -188,6 +190,24 @@ def wait_for_server(url, timeout):
print("Selenium server has been terminated")


@pytest.fixture(scope="session")
def html_server():
"""
Start an HTTP server to serve files from the `tests/` directory.
"""
elements_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)))
os.chdir(elements_dir)
port = free_port()
server = HTTPServer(("localhost", port), SimpleHTTPRequestHandler)

thread = Thread(target=server.serve_forever, daemon=True)
thread.start()

yield f"http://localhost:{port}"

server.shutdown()


@pytest.fixture(scope="function")
def server():
_host = "localhost"
Expand Down
30 changes: 30 additions & 0 deletions examples/python/tests/elements/locators.html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't place HTML code here. Instead, you can use the static pages deployed on the official Selenium website
https://www.selenium.dev/selenium/web/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't find a similar html page, so created a PR to add it in selenium - SeleniumHQ/selenium#14905

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<html>
<body>
<style>
.information {
background-color: white;
color: black;
padding: 10px;
}
</style>
<h2>Contact Selenium</h2>

<form>
<input type="radio" name="gender" value="m" />Male &nbsp;
<input type="radio" name="gender" value="f" />Female <br>
<br>
<label for="fname">First name:</label><br>
<input class="information" type="text" id="fname" name="fname" value="Jane"><br><br>
<label for="lname">Last name:</label><br>
<input class="information" type="text" id="lname" name="lname" value="Doe"><br><br>
<label for="newsletter">Newsletter:</label>
<input type="checkbox" name="newsletter" value="1" /><br><br>
<input type="submit" value="Submit">
</form>

<p>To know more about Selenium, visit the official page
<a href ="https://www.selenium.dev/">Selenium Official Page</a>
</p>

</body>
</html>
52 changes: 52 additions & 0 deletions examples/python/tests/elements/test_locators.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture
def driver(html_server):
"""
Initialize the WebDriver and navigate to the elements/locators.html page.
"""
driver = webdriver.Chrome()
driver.implicitly_wait(0.5)
driver.get(f"{html_server}/elements/locators.html")
yield driver
driver.quit()

def test_class_name(driver):
element = driver.find_element(By.CLASS_NAME, "information")
assert element is not None
assert element.tag_name == "input"

def test_css_selector(driver):
element = driver.find_element(By.CSS_SELECTOR, "#fname")
assert element is not None
assert element.get_attribute("value") == "Jane"

def test_id(driver):
element = driver.find_element(By.ID, "lname")
assert element is not None
assert element.get_attribute("value") == "Doe"

def test_name(driver):
element = driver.find_element(By.NAME, "newsletter")
assert element is not None
assert element.tag_name == "input"

def test_link_text(driver):
element = driver.find_element(By.LINK_TEXT, "Selenium Official Page")
assert element is not None
assert element.get_attribute("href") == "https://www.selenium.dev/"

def test_partial_link_text(driver):
element = driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page")
assert element is not None
assert element.get_attribute("href") == "https://www.selenium.dev/"

def test_tag_name(driver):
element = driver.find_element(By.TAG_NAME, "a")
assert element is not None
assert element.get_attribute("href") == "https://www.selenium.dev/"

def test_xpath(driver):
element = driver.find_element(By.XPATH, "//input[@value='f']")
assert element is not None
assert element.get_attribute("type") == "radio"
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ available in Selenium.
WebDriver driver = new ChromeDriver();
driver.findElement(By.className("information"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.CLASS_NAME, "information")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L17" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.ClassName("information"));
Expand Down Expand Up @@ -114,10 +113,9 @@ textbox, using css.
WebDriver driver = new ChromeDriver();
driver.findElement(By.cssSelector("#fname"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.CSS_SELECTOR, "#fname")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L22" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.CssSelector("#fname"));
Expand Down Expand Up @@ -146,10 +144,9 @@ We will identify the Last Name field using it.
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("lname"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.ID, "lname")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L27" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.Id("lname"));
Expand Down Expand Up @@ -179,10 +176,9 @@ We will identify the Newsletter checkbox using it.
WebDriver driver = new ChromeDriver();
driver.findElement(By.name("newsletter"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.NAME, "newsletter")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L32" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.Name("newsletter"));
Expand Down Expand Up @@ -210,10 +206,9 @@ In the HTML snippet shared, we have a link available, let's see how will we loca
WebDriver driver = new ChromeDriver();
driver.findElement(By.linkText("Selenium Official Page"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.LINK_TEXT, "Selenium Official Page")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.LinkText("Selenium Official Page"));
Expand Down Expand Up @@ -242,10 +237,9 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
WebDriver driver = new ChromeDriver();
driver.findElement(By.partialLinkText("Official Page"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L42" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.PartialLinkText("Official Page"));
Expand All @@ -272,10 +266,9 @@ From the above HTML snippet shared, lets identify the link, using its html tag "
WebDriver driver = new ChromeDriver();
driver.findElement(By.tagName("a"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.TAG_NAME, "a")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.TagName("a"));
Expand Down Expand Up @@ -308,10 +301,9 @@ first name text box. Let us create locator for female radio button using xpath.
WebDriver driver = new ChromeDriver();
driver.findElement(By.xpath("//input[@value='f']"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.XPATH, "//input[@value='f']")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L52" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.Xpath("//input[@value='f']"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ available in Selenium.
WebDriver driver = new ChromeDriver();
driver.findElement(By.className("information"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.CLASS_NAME, "information")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L17" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.ClassName("information"));
Expand Down Expand Up @@ -111,10 +110,9 @@ textbox, using css.
WebDriver driver = new ChromeDriver();
driver.findElement(By.cssSelector("#fname"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.CSS_SELECTOR, "#fname")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L22" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.CssSelector("#fname"));
Expand Down Expand Up @@ -142,10 +140,9 @@ We will identify the Last Name field using it.
WebDriver driver = new ChromeDriver();
driver.findElement(By.id("lname"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.ID, "lname")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L27" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.Id("lname"));
Expand Down Expand Up @@ -174,10 +171,9 @@ We will identify the Newsletter checkbox using it.
WebDriver driver = new ChromeDriver();
driver.findElement(By.name("newsletter"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.NAME, "newsletter")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L32" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.Name("newsletter"));
Expand All @@ -204,10 +200,9 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
WebDriver driver = new ChromeDriver();
driver.findElement(By.linkText("Selenium Official Page"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.LINK_TEXT, "Selenium Official Page")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L37" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.LinkText("Selenium Official Page"));
Expand Down Expand Up @@ -235,10 +230,9 @@ In the HTML snippet shared, we have a link available, lets see how will we locat
WebDriver driver = new ChromeDriver();
driver.findElement(By.partialLinkText("Official Page"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.PARTIAL_LINK_TEXT, "Official Page")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L42" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.PartialLinkText("Official Page"));
Expand All @@ -264,10 +258,9 @@ From the above HTML snippet shared, lets identify the link, using its html tag "
WebDriver driver = new ChromeDriver();
driver.findElement(By.tagName("a"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.TAG_NAME, "a")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L47" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.TagName("a"));
Expand Down Expand Up @@ -299,10 +292,9 @@ first name text box. Let us create locator for female radio button using xpath.
WebDriver driver = new ChromeDriver();
driver.findElement(By.xpath("//input[@value='f']"));
{{< /tab >}}
{{< tab header="Python" >}}
driver = webdriver.Chrome()
driver.find_element(By.XPATH, "//input[@value='f']")
{{< /tab >}}
{{< tab header="Python" text=true >}}
{{< gh-codeblock path="/examples/python/tests/elements/test_locators.py#L52" >}}
{{< /tab >}}
{{< tab header="CSharp" >}}
var driver = new ChromeDriver();
driver.FindElement(By.Xpath("//input[@value='f']"));
Expand Down
Loading