Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
harmin-parra committed Sep 25, 2024
1 parent cd39be7 commit 9714e99
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 1 deletion.
8 changes: 8 additions & 0 deletions runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ echo =================
echo Python - Selenium
echo =================
pytest web_selenium/tests/ --driver $BROWSER # --hub $HUB
echo ===============
echo Python - Pytest
echo ===============
pytest web_pytest/tests/ --driver $BROWSER \
--alluredir /tmp \
--html=../reporting/report-pytest/index.html \
--css=report.css
echo ========================
echo Python - Robot Framework
echo ========================
Expand Down Expand Up @@ -125,6 +132,7 @@ echo =============
echo Java - Karate
echo =============
# mvn -Dtest="web_karate/**, rest_api_karate/**" -Dbrowser=$BROWSER test
# mvn -Dtest="karate/TestRunner#modularityTest" -Dbrowser=firefox test
mvn -Dtest="karate/TestRunner#allTests" -Dbrowser=$BROWSER test

# Purge weird Allure Karate entries
Expand Down
2 changes: 1 addition & 1 deletion tests-python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ playwright==1.47.0
pytest
pytest-html==4.1.1
pytest-playwright==0.5.2
pytest-webtest-extras==1.2.0
#pytest-webtest-extras==1.2.2
requests
robotframework==7.1
robotframework-browser==18.8.1
Expand Down
60 changes: 60 additions & 0 deletions tests-python/web_pytest/conftest.py
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()
2 changes: 2 additions & 0 deletions tests-python/web_pytest/pages/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .webform_page import WebFormPage
from .ajax_page import AjaxPage
20 changes: 20 additions & 0 deletions tests-python/web_pytest/pages/ajax_page.py
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"
65 changes: 65 additions & 0 deletions tests-python/web_pytest/pages/webform_page.py
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()
17 changes: 17 additions & 0 deletions tests-python/web_pytest/tests/ajax_test.py
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")
36 changes: 36 additions & 0 deletions tests-python/web_pytest/tests/webform_test.py
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")

0 comments on commit 9714e99

Please sign in to comment.