From a120a177ea9fb3803b2434d449bd0db97d873675 Mon Sep 17 00:00:00 2001 From: Douglas Cardoso Date: Wed, 8 Jan 2025 01:18:19 -0300 Subject: [PATCH 1/2] - Integrate with Broserist - Add type hint to self._driver in exmples - Fix typos --- .gitignore | 2 + README.md | 2 +- guara/setup.py | 6 +- test-requirements.txt | 3 +- .../pages/getting_started.py | 2 + tests/web_ui_playwrigth/pages/home.py | 2 + tests/web_ui_playwrigth/pages/setup.py | 2 + .../{test_local_page => }/sample.html | 0 tests/web_ui_selenium/test_browserist/home.py | 26 +++++++++ .../web_ui_selenium/test_browserist/setup.py | 57 +++++++++++++++++++ .../test_browserist/test_browserist.py | 32 +++++++++++ tests/web_ui_selenium/test_local_page/home.py | 17 ------ .../test_local_page/test_local_page.py | 6 +- .../web_ui_selenium/test_web_page/test_vpm.py | 2 +- 14 files changed, 133 insertions(+), 26 deletions(-) rename tests/web_ui_selenium/{test_local_page => }/sample.html (100%) create mode 100644 tests/web_ui_selenium/test_browserist/home.py create mode 100644 tests/web_ui_selenium/test_browserist/setup.py create mode 100644 tests/web_ui_selenium/test_browserist/test_browserist.py diff --git a/.gitignore b/.gitignore index 15201ac..c9570eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +captures + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/README.md b/README.md index 184adfc..885ea88 100644 --- a/README.md +++ b/README.md @@ -143,7 +143,7 @@ tests/web_ui_local/test_local_page.py::TestLocalTransaction::test_local_page INFO guara.transaction:transaction.py:26 2025-01-06 01:51:41.654542 Transaction 'OpenApp' INFO guara.transaction:transaction.py:28 url: file:////sample.html INFO guara.transaction:transaction.py:28 window_width: 1094 -INFO guara.transaction:transaction.py:28 window_hight: 765 +INFO guara.transaction:transaction.py:28 window_height: 765 INFO guara.transaction:transaction.py:28 implicitly_wait: 0.5 --------------------------------------------------------------- live log call ---------------------------------------------------------------- INFO guara.transaction:transaction.py:26 2025-01-06 01:51:41.788494 Transaction 'SubmitText' diff --git a/guara/setup.py b/guara/setup.py index 24f8bbb..fa2195c 100644 --- a/guara/setup.py +++ b/guara/setup.py @@ -9,7 +9,7 @@ class OpenApp(AbstractTransaction): Args: url (str): the path where the screenshot is saved. window_width (int): The width of the browser. Defaults to 1094 - window_hight (int): The hight of the browser. Defaults t0 765 + window_height (int): The height of the browser. Defaults t0 765 implicitly_wait (int): the implicity timeout for an element to be found. Defaults to 10 (seconds) Returns: @@ -19,8 +19,8 @@ class OpenApp(AbstractTransaction): def __init__(self, driver): super().__init__(driver) - def do(self, url, window_width=1094, window_hight=765, implicitly_wait=10): - self._driver.set_window_size(window_width, window_hight) + def do(self, url, window_width=1094, window_height=765, implicitly_wait=10): + self._driver.set_window_size(window_width, window_height) self._driver.get(url) self._driver.implicitly_wait(implicitly_wait) return self._driver.title diff --git a/test-requirements.txt b/test-requirements.txt index 0acd1a8..71efcc2 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -6,4 +6,5 @@ black requests caqui tox -pytest-playwright \ No newline at end of file +pytest-playwright +browserist \ No newline at end of file diff --git a/tests/web_ui_playwrigth/pages/getting_started.py b/tests/web_ui_playwrigth/pages/getting_started.py index 7c6013c..71e0c8a 100644 --- a/tests/web_ui_playwrigth/pages/getting_started.py +++ b/tests/web_ui_playwrigth/pages/getting_started.py @@ -1,3 +1,4 @@ +from playwright.sync_api import Page from guara.transaction import AbstractTransaction @@ -11,6 +12,7 @@ class NavigateToWritingTests(AbstractTransaction): def __init__(self, driver): super().__init__(driver) + self._driver: Page def do(self, **kwargs): self._driver.get_by_role("link", name="Writing tests", exact=True).click() diff --git a/tests/web_ui_playwrigth/pages/home.py b/tests/web_ui_playwrigth/pages/home.py index 6d404be..a8aad35 100644 --- a/tests/web_ui_playwrigth/pages/home.py +++ b/tests/web_ui_playwrigth/pages/home.py @@ -1,3 +1,4 @@ +from playwright.sync_api import Page from guara.transaction import AbstractTransaction @@ -11,6 +12,7 @@ class NavigateToGettingStarted(AbstractTransaction): def __init__(self, driver): super().__init__(driver) + self._driver: Page def do(self, **kwargs): self._driver.get_by_role("link", name="Get started").click() diff --git a/tests/web_ui_playwrigth/pages/setup.py b/tests/web_ui_playwrigth/pages/setup.py index 9b39f13..995bbc0 100644 --- a/tests/web_ui_playwrigth/pages/setup.py +++ b/tests/web_ui_playwrigth/pages/setup.py @@ -1,3 +1,4 @@ +from playwright.sync_api import Page from guara.transaction import AbstractTransaction @@ -14,6 +15,7 @@ class OpenApp(AbstractTransaction): def __init__(self, driver): super().__init__(driver) + self._driver: Page def do(self, with_url): self._driver.goto(with_url) diff --git a/tests/web_ui_selenium/test_local_page/sample.html b/tests/web_ui_selenium/sample.html similarity index 100% rename from tests/web_ui_selenium/test_local_page/sample.html rename to tests/web_ui_selenium/sample.html diff --git a/tests/web_ui_selenium/test_browserist/home.py b/tests/web_ui_selenium/test_browserist/home.py new file mode 100644 index 0000000..d7ce1b0 --- /dev/null +++ b/tests/web_ui_selenium/test_browserist/home.py @@ -0,0 +1,26 @@ +from browserist import Browser +from guara.transaction import AbstractTransaction + + +class SubmitText(AbstractTransaction): + """ + Submits the text + + Args: + text (str): The text to be submited + + Returns: + str: the label 'It works! {code}!' + """ + + def __init__(self, driver): + super().__init__(driver) + self._driver: Browser + + def do(self, text): + TEXT = '//*[@id="input"]' + BUTTON_TEST = '//*[@id="button"]' + RESULT = '//*[@id="result"]' + self._driver.input.value(TEXT, text) + self._driver.click.button(BUTTON_TEST) + return self._driver.get.text(RESULT) diff --git a/tests/web_ui_selenium/test_browserist/setup.py b/tests/web_ui_selenium/test_browserist/setup.py new file mode 100644 index 0000000..f0cabbc --- /dev/null +++ b/tests/web_ui_selenium/test_browserist/setup.py @@ -0,0 +1,57 @@ +from datetime import datetime +from browserist import Browser +from guara.transaction import AbstractTransaction + + +class OpenApp(AbstractTransaction): + """ + Opens the app + + Args: + url (str): the path where the screenshot is saved. + window_width (int): The width of the browser. Defaults to 1094 + window_height (int): The height of the browser. Defaults t0 765 + implicitly_wait (int): the implicity timeout for an element to be found. + Defaults to 10 (seconds) + Returns: + str: the title of the app + """ + + def __init__(self, driver): + super().__init__(driver) + self._driver: Browser + + def do(self, url, window_width=1094, window_height=765, implicitly_wait=10): + self._driver.window.set.width(window_width) + self._driver.window.set.height(window_height) + self._driver.open.url(url) + self._driver.wait.seconds(implicitly_wait) + return self._driver.get.page_title() + + +class CloseApp(AbstractTransaction): + """ + Closes the app and saves its screenshot (PNG) + + Args: + screenshot_filename (str): the name of the screenshot file. + Defaults to 'guara-{datetime.now()}.png'. + + screenshot_destination (str): the path where the screenshot is saved. + Defaults to './captures'. + + """ + + def __init__(self, driver): + super().__init__(driver) + self._driver: Browser + + def do( + self, + screenshot_destination="./captures", + screenshot_filename="guara-capture", + ): + self._driver.screenshot.complete_page( + f"{screenshot_filename}-{datetime.now()}.png", screenshot_destination + ) + self._driver.quit() diff --git a/tests/web_ui_selenium/test_browserist/test_browserist.py b/tests/web_ui_selenium/test_browserist/test_browserist.py new file mode 100644 index 0000000..affff04 --- /dev/null +++ b/tests/web_ui_selenium/test_browserist/test_browserist.py @@ -0,0 +1,32 @@ +import pathlib +import random +from guara.transaction import Application +from guara import it +from tests.web_ui_selenium.test_browserist import setup +from tests.web_ui_selenium.test_browserist import home +from browserist import Browser, BrowserSettings + + +class TestIntegrateBrowserist: + def setup_method(self, method): + file_path = pathlib.Path(__file__).parent.parent.resolve() + + self._app = Application(Browser(BrowserSettings(headless=True))) + self._app.at( + setup.OpenApp, + url=f"file:///{file_path}/sample.html", + window_width=1094, + window_height=765, + implicitly_wait=0.5, + ).asserts(it.IsEqualTo, "Sample page") + + def teardown_method(self, method): + self._app.at(setup.CloseApp) + + def test_local_page(self): + text = ["cheese", "selenium", "test", "bla", "foo"] + text = text[random.randrange(len(text))] + self._app.at(home.SubmitText, text=text).asserts( + it.IsEqualTo, f"It works! {text}!" + ) + self._app.at(home.SubmitText, text=text).asserts(it.IsNotEqualTo, "Any") diff --git a/tests/web_ui_selenium/test_local_page/home.py b/tests/web_ui_selenium/test_local_page/home.py index a9e094f..4c21d0c 100644 --- a/tests/web_ui_selenium/test_local_page/home.py +++ b/tests/web_ui_selenium/test_local_page/home.py @@ -2,23 +2,6 @@ from guara.transaction import AbstractTransaction -class Navigate(AbstractTransaction): - """ - Navigates to Home page - - Returns: - str: the label 'It works! {code}!' - """ - - def __init__(self, driver): - super().__init__(driver) - - def do(self, **kwargs): - self._driver.find_element(By.CSS_SELECTOR, ".navbar-brand > img").click() - self._driver.find_element(By.CSS_SELECTOR, ".col-md-10").click() - return self._driver.find_element(By.CSS_SELECTOR, "label:nth-child(1)").text - - class SubmitText(AbstractTransaction): """ Submits the text diff --git a/tests/web_ui_selenium/test_local_page/test_local_page.py b/tests/web_ui_selenium/test_local_page/test_local_page.py index dc24839..55d97b5 100644 --- a/tests/web_ui_selenium/test_local_page/test_local_page.py +++ b/tests/web_ui_selenium/test_local_page/test_local_page.py @@ -9,7 +9,7 @@ class TestLocalTransaction: def setup_method(self, method): - file_path = pathlib.Path(__file__).parent.resolve() + file_path = pathlib.Path(__file__).parent.parent.resolve() options = webdriver.ChromeOptions() options.add_argument("--headless=new") self._app = Application(webdriver.Chrome(options=options)) @@ -17,9 +17,9 @@ def setup_method(self, method): setup.OpenApp, url=f"file:///{file_path}/sample.html", window_width=1094, - window_hight=765, + window_height=765, implicitly_wait=0.5, - ) + ).asserts(it.IsEqualTo, "Sample page") def teardown_method(self, method): self._app.at(setup.CloseApp) diff --git a/tests/web_ui_selenium/test_web_page/test_vpm.py b/tests/web_ui_selenium/test_web_page/test_vpm.py index 5ab16af..538808b 100644 --- a/tests/web_ui_selenium/test_web_page/test_vpm.py +++ b/tests/web_ui_selenium/test_web_page/test_vpm.py @@ -57,7 +57,7 @@ def setup_application(): configuration = { "url": "https://vagaspramim.onrender.com/", "window_width": 1094, - "window_hight": 765, + "window_height": 765, "implicitly_wait": 0.5, } options = webdriver.ChromeOptions() From bea2a45a0977580eaad06014b244d2a8e648b77b Mon Sep 17 00:00:00 2001 From: Douglas Cardoso Date: Wed, 8 Jan 2025 01:39:59 -0300 Subject: [PATCH 2/2] Bump version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 2bd749e..e2c2850 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ exclude = ["tests*", ".vscode", ".git*", "dist", "*pytest*"] [project] name = "guara" -version = "0.0.4" +version = "0.0.5" authors = [ { name="Douglas Cardoso", email="noemail@noemail.com" }, ]