-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd39be7
commit 9714e99
Showing
8 changed files
with
209 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import pytest | ||
|
||
from selenium import webdriver | ||
from selenium.webdriver.firefox.options import Options as Options_Firefox | ||
from selenium.webdriver.chrome.options import Options as Options_Chrome | ||
from selenium.webdriver.chromium.options import ChromiumOptions as Options_Chromium | ||
from selenium.webdriver.edge.options import Options as Options_Edge | ||
from selenium.webdriver.chromium.webdriver import ChromiumDriver as WebDriver_Chromium | ||
from selenium.webdriver.chromium.service import ChromiumService as Service_Chromium | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption("--driver", action="store", default="firefox") | ||
parser.addoption("--hub", action="store", default=None) | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def optdriver(request): | ||
return request.config.getoption("--driver").lower() | ||
|
||
|
||
@pytest.fixture(scope='session') | ||
def opthub(request): | ||
return request.config.getoption("--hub") | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def driver(optdriver, opthub): | ||
_driver = None | ||
options = None | ||
if optdriver == "chrome": | ||
options = Options_Chrome() | ||
options.add_argument("--headless=new") | ||
if opthub is None: | ||
_driver = webdriver.Chrome(options=options) | ||
elif optdriver == "chromium": | ||
options = Options_Chromium() | ||
options.add_argument("--headless=new") | ||
if opthub is None: | ||
_driver = WebDriver_Chromium( | ||
browser_name="Chromium", | ||
vendor_prefix="Chromium", | ||
options=options, | ||
service=Service_Chromium() | ||
) | ||
elif optdriver == "firefox": | ||
options = Options_Firefox() | ||
options.add_argument("--headless") | ||
if opthub is None: | ||
_driver = webdriver.Firefox(options=options) | ||
elif optdriver == "edge": | ||
options = Options_Edge() | ||
options.add_argument("--headless=new") | ||
if opthub is None: | ||
_driver = webdriver.Edge(options=options) | ||
if opthub is not None: | ||
server = f"http://{hub}:4444/wd/hub" | ||
_driver = webdriver.Remote(command_executor=server, options=options) | ||
yield _driver | ||
_driver.quit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .webform_page import WebFormPage | ||
from .ajax_page import AjaxPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support.wait import WebDriverWait | ||
from selenium.webdriver.support import expected_conditions as EC | ||
|
||
|
||
class AjaxPage: | ||
|
||
def __init__(self, driver): | ||
self.driver = driver | ||
self.button = driver.find_element(By.ID, "button") | ||
# self.title = driver.find_element(By.ID, "title") | ||
|
||
def click(self): | ||
self.button.click() | ||
|
||
def verify(self): | ||
wait = WebDriverWait(self.driver, 15) | ||
wait.until(EC.presence_of_element_located((By.ID, "title"))) | ||
title = self.driver.find_element(By.ID, "title") | ||
assert title.text == "AJAX" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from selenium.webdriver.common.by import By | ||
from selenium.webdriver.support.ui import Select | ||
from selenium.webdriver.support.wait import WebDriverWait | ||
from selenium.webdriver.support import expected_conditions as EC | ||
from selenium.webdriver.common.action_chains import ActionChains | ||
from selenium.webdriver.remote.file_detector import LocalFileDetector | ||
|
||
|
||
class WebFormPage: | ||
|
||
def __init__(self, driver): | ||
self.driver = driver | ||
self.input = driver.find_element(By.NAME, "my-text") | ||
self.password = driver.find_element(By.NAME, "my-password") | ||
self.textarea = driver.find_element(By.NAME, "my-textarea") | ||
self.number = Select(driver.find_element(By.NAME, "my-select")) | ||
self.city = driver.find_element(By.NAME, "my-datalist") | ||
self.driver.file_detector = LocalFileDetector() | ||
self.file = driver.find_element(By.NAME, "my-file") | ||
self.color = driver.find_element(By.NAME, "my-colors") | ||
self.date = driver.find_element(By.NAME, "my-date") | ||
self.range = driver.find_element(By.NAME, "my-range") | ||
self.button = driver.find_element(By.XPATH, "//button[@type='submit']") | ||
|
||
def set_input(self, value): | ||
self.input.send_keys(value) | ||
|
||
def set_password(self, value): | ||
self.password.send_keys(value) | ||
|
||
def set_textarea(self, value): | ||
self.textarea.send_keys(value) | ||
|
||
def set_number(self, value): | ||
self.number.select_by_index(value) | ||
|
||
def set_city(self, value): | ||
self.city.send_keys(value) | ||
|
||
def set_file(self, value): | ||
self.file.send_keys(value) | ||
|
||
def set_color(self, value): | ||
self.driver.execute_script(f"document.getElementsByName('my-colors')[0].value = '{value}'") | ||
|
||
def set_date(self, value): | ||
self.date.send_keys(value) | ||
# Click elsewhere to close the calendar | ||
self.driver.find_element(By.XPATH, "//body").click() | ||
|
||
def set_range(self, value): | ||
# Two solutions | ||
# Solution #1: Trigger mouse events to move the range handler | ||
dimensions = self.range.rect | ||
current = int(self.range.get_attribute("value")) | ||
step = dimensions['width'] / (int(self.range.get_attribute("max")) - int(self.range.get_attribute("min"))) | ||
offset = (value - current) * step | ||
action = ActionChains(self.driver) | ||
action.move_to_element_with_offset(self.range, offset, 0).click().perform() | ||
|
||
# solution #2: Set the range value directly | ||
self.driver.execute_script(f"document.getElementsByName('my-range')[0].value = '{value}'") | ||
|
||
def submit(self): | ||
self.button.click() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from web_pytest.conftest import driver | ||
from web_pytest import pages | ||
|
||
|
||
def test_ajax_verification(driver, extras): | ||
""" | ||
Testing an AJAX page. | ||
Test using WebDriverWait().until() | ||
""" | ||
driver.get("http://qa-demo.gitlab.io/reports/web/ajax.html") | ||
extras.screenshot_selenium(driver, "Initial page") | ||
ajax = pages.AjaxPage(driver) | ||
ajax.click() | ||
extras.screenshot_selenium(driver, "Trigger event") | ||
ajax.verify() | ||
extras.screenshot_selenium(driver, "Verify event result") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
from web_pytest.conftest import driver | ||
from web_pytest import pages | ||
|
||
|
||
def test_fill_in_form(driver, extras): | ||
""" | ||
Testing the following field types of a webform : | ||
- Input text | ||
- Text area | ||
- Select | ||
- Checkbox | ||
- Radio button | ||
- File upload | ||
- Color picker | ||
- Date picker | ||
- Input range | ||
- Button | ||
""" | ||
driver.get("https://www.selenium.dev/selenium/web/web-form.html") | ||
extras.screenshot_selenium(driver, "Empty form") | ||
webform = pages.WebFormPage(driver) | ||
webform.set_input("login") | ||
webform.set_password("password") | ||
webform.set_textarea("My textarea") | ||
webform.set_number(2) | ||
webform.set_city("Los Angeles") | ||
webform.set_file("/tmp/test/file.xml") | ||
webform.set_color("#00ff00") | ||
webform.set_date("01/01/2024") | ||
webform.set_range(1) | ||
extras.screenshot_selenium(driver, "Complete form") | ||
print(extras.format_xml_file("/tmp/test/file.xml")) | ||
webform.submit() | ||
extras.screenshot_selenium(driver, "Submit form") |