Skip to content

Commit

Permalink
fix(fmt): correct file formatting.
Browse files Browse the repository at this point in the history
Correctly format file using docformatter, black, isort.
Ensure PEP compliancy using flake8

Signed-off-by: Lionel Hubaut <lionel.hubaut@tessares.net>
  • Loading branch information
lion24 committed Apr 8, 2024
1 parent 3fb893f commit 338f677
Show file tree
Hide file tree
Showing 9 changed files with 273 additions and 217 deletions.
48 changes: 27 additions & 21 deletions lib/browser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
"""
browser is the module handling the different browser logic and their
implementation in order to be used with selenium.
"""
"""Browser is the module handling the different browser logic and their
implementation in order to be used with selenium."""

import abc

from selenium.webdriver.remote.webdriver import WebDriver


class BrowserInterface(metaclass=abc.ABCMeta):
"""BrowserInterface is a generic interface each browser subclass will need
to implement in order to correctly configure the selenium webdriver.
This interface ensures that all subclasses provide a specific method
to load and configure a Selenium WebDriver instance appropriate for
the browser they represent.
"""
BrowserInterface is a generic interface each browser subclass will need to
implement in order to correctly configure the selenium webdriver. This interface
ensures that all subclasses provide a specific method to load and configure
a Selenium WebDriver instance appropriate for the browser they represent.
"""

@classmethod
def __subclasshook__(cls, subclass):
return (hasattr(subclass, 'load_driver') and
callable(subclass.load_driver) or
NotImplemented)
return (
hasattr(subclass, "load_driver")
and callable(subclass.load_driver)
or NotImplemented
)

@classmethod
@abc.abstractmethod
def load_driver(cls) -> WebDriver:
"""
Loads and returns a configured instance of Selenium WebDriver specific to the browser.
"""Loads and returns a configured instance of Selenium WebDriver
specific to the browser.
This method must be implemented by subclasses to provide a
ready-to-use WebDriver instance that is appropriately configured
for the browser the subclass represents. The configuration may
include setting browser options, capabilities, and webdriver
paths.
This method must be implemented by subclasses to provide a ready-to-use WebDriver
instance that is appropriately configured for the browser the subclass represents.
The configuration may include setting browser options, capabilities, and webdriver paths.
Returns: WebDriver: An instance of a Selenium WebDriver
ready for browser automation tasks.
Returns:
WebDriver: An instance of a Selenium WebDriver ready for browser automation tasks.
Raises: NotImplementedError: If the subclass does not
implement this method.
Raises:
NotImplementedError: If the subclass does not implement this method.
"""
raise NotImplementedError
83 changes: 46 additions & 37 deletions lib/browser/chromium.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,74 @@
"""
This module defines the ChromiumBrower class, which is an implementation of the BrowserInterface
for creating and configuring a Selenium WebDriver specific to Chromium-based browsers. Currently,
the implementation focuses on Google Chrome, with the intention to extend support to other
Chromium-based browsers in the future.
Key Components:
- BrowserInterface: An abstract base class that defines a generic interface for browser subclasses.
- ChromiumBrower: A concrete class that implements the BrowserInterface for the Chrome browser,
providing a method to load and configure a Selenium WebDriver with Chromium-specific options.
"""This module defines the ChromiumBrower class, which is an implementation of
the BrowserInterface for creating and configuring a Selenium WebDriver specific
to Chromium- based browsers. Currently, the implementation focuses on Google
Chrome, with the intention to extend support to other Chromium-based browsers
in the future.
Key Components: - BrowserInterface: An abstract base class that defines
a generic interface for browser subclasses. - ChromiumBrower: A concrete
class that implements the BrowserInterface for the Chrome browser,
providing a method to load and configure a Selenium WebDriver with
Chromium-specific options.
"""

from selenium import webdriver
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.chromium.service import ChromiumService
from selenium.webdriver.chromium.options import ChromiumOptions
from selenium.webdriver.chromium.service import ChromiumService
from selenium.webdriver.remote.webdriver import WebDriver

from . import BrowserInterface

BINARY_PATH = "/snap/chromium/2805/usr/lib/chromium-browser/chrome"


@BrowserInterface.register
class ChromiumBrower:
"""
ChromiumBrower implements the BrowserInterface to provide a method for loading
and configuring a WebDriver instance specifically for Chromium browsers.
"""ChromiumBrower implements the BrowserInterface to provide a method for
loading and configuring a WebDriver instance specifically for Chromium
browsers.
This class currently supports Chrome browser, with plans to extend support
to other Chromium-based browsers. It demonstrates how to set up a Selenium
WebDriver with specific options tailored for a Chromium browser instance,
including setting the binary location, window size, and disabling GPU acceleration.
This class currently supports Chrome browser, with plans to extend
support to other Chromium-based browsers. It demonstrates how to set
up a Selenium WebDriver with specific options tailored for a
Chromium browser instance, including setting the binary location,
window size, and disabling GPU acceleration.
Methods: load_driver(): Creates and returns a configured
Selenium WebDriver instance for the Chromium
browser.
Methods:
load_driver(): Creates and returns a configured Selenium WebDriver instance
for the Chromium browser.
"""

def load_driver(self) -> WebDriver:
"""
Initializes and returns a Selenium WebDriver instance configured for the Chrome browser.
"""Initializes and returns a Selenium WebDriver instance configured for
the Chrome browser.
This method sets up a ChromiumService and configures ChromiumOptions to specify
the binary location of the Chrome browser, set the window size, disable GPU acceleration,
and set the browser language. These options ensure that the WebDriver instance is
ready for web automation tasks with Chrome.
This method sets up a ChromiumService and configures ChromiumOptions
to specify the binary location of the Chrome browser, set the window
size, disable GPU acceleration, and set the browser language. These
options ensure that the WebDriver instance is ready for web automation
tasks with Chrome.
Note: While this implementation currently supports Chromium, there is a plan to expand
support to other browsers.
Note: While this implementation currently supports Chromium, there is a
plan to expand support to other browsers.
Returns:
WebDriver: A configured instance of Selenium WebDriver for the Chromium browser.
WebDriver: A configured instance of Selenium WebDriver for the
Chromium browser.
Example:
>>> chromium_browser = ChromiumBrower()
>>> driver = chromium_browser.load_driver()
# Now `driver` can be used to automate web interactions using Chromium.
"""
service = ChromiumService()
options = ChromiumOptions()
options.binary_location = '/snap/chromium/2805/usr/lib/chromium-browser/chrome'
#options.add_argument('--headless')
options.add_argument('--window-size=1400x900')
options.add_argument('--disable-gpu')
options.add_argument('--lang=en_US')
options.binary_location = BINARY_PATH
# options.add_argument('--headless')
options.add_argument("--window-size=1400x900")
options.add_argument("--disable-gpu")
options.add_argument("--lang=en_US")
driver = webdriver.Chrome(service=service, options=options)
return driver
# As using selenium api > 2.x, this call should block until
Expand Down
70 changes: 33 additions & 37 deletions lib/provider/__init__.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
# coding: utf-8
# Disable broad-except for now, will refine later.
# pylint: disable=broad-except
"""
Generic Provider class which provides an abstraction for the different drivers
we would like to use.
"""
"""Generic Provider class which provides an abstraction for the different
drivers we would like to use."""

import sys
from abc import ABC, abstractmethod

from browser import BrowserInterface
from selenium.common.exceptions import (
NoSuchElementException,
ElementNotInteractableException,
TimeoutException
NoSuchElementException,
TimeoutException,
)
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


class Provider(ABC):
"""Each driver will be derive from this Abstract provider class.
This class also contains generic methods which needs to be
implemented in the concrete provider classes.
"""
Each driver will be derive from this Abstract provider class.
This class also contains generic methods which needs to be implemented
in the concrete provider classes.
"""

def __init__(self, browser: BrowserInterface):
try:
self.driver = browser.load_driver()
except Exception as e:
print("browser.load_driver() exception: ", e)

def wait_to_be_visible(self, element: WebElement, timeout=90):
"""
Method that wait until an element is present and clickable in the DOM.
"""
"""Method that wait until an element is present and clickable in the
DOM."""
print("wait_to_be_visible...")
errors = [NoSuchElementException, ElementNotInteractableException]
try:
wait = WebDriverWait(
self.driver,
timeout=timeout,
poll_frequency=.2,
ignored_exceptions=errors)
poll_frequency=0.2,
ignored_exceptions=errors,
)

res = wait.until(lambda d: element.is_displayed() or False)
print(f"res: {res}")
Expand All @@ -55,11 +56,11 @@ def wait_to_be_visible(self, element: WebElement, timeout=90):
return False

def wait_for_element(self, locator, timeout=120) -> WebElement:
"""
Wait for an element to be visible and returns it
If not found, NoSuchElementException is raised.
"""Wait for an element to be visible and returns it If not found,
NoSuchElementException is raised.
:param locator: A tuple of (By, locator) to find the element.
"""
try:
element = WebDriverWait(self.driver, timeout).until(
Expand All @@ -68,17 +69,16 @@ def wait_for_element(self, locator, timeout=120) -> WebElement:
return element
except TimeoutException as e:
raise NoSuchElementException(
f"Element with locator {locator} was not visible after {timeout} second") from e
f"Element {locator} was not visible after {timeout} second"
) from e

def wait_for_button_clickable(self, locator, timeout=120) -> WebElement:
"""
Wait for a button to be visible and clickable
If not found, NoSuchElementException is raised.
"""Wait for a button to be visible and clickable If not found,
NoSuchElementException is raised.
:param locator: A tuple of (By, locator) to find the element.
Returns: WebElement: The button identified by locator
Returns:
WebElement: The button identified by locator
"""
try:
element = WebDriverWait(self.driver, timeout).until(
Expand All @@ -87,12 +87,11 @@ def wait_for_button_clickable(self, locator, timeout=120) -> WebElement:
return element
except TimeoutException as e:
raise NoSuchElementException(
f"Element with locator {locator} was not clickable after {timeout} second") from e
f"Element {locator} was not clickable after {timeout} second"
) from e

def cleanup(self, errno=0):
"""
Cleanup every reserved resources.
"""
"""Cleanup every reserved resources."""
if self.driver:
self.driver.close()
self.driver.quit()
Expand All @@ -103,14 +102,11 @@ def cleanup(self, errno=0):

@abstractmethod
def run(self, filename):
"""
Actual method that would trigger the test for the given provider.
"""
"""Actual method that would trigger the test for the given provider."""
raise NotImplementedError("Should be implemented in daughter class")

@abstractmethod
def parse_results(self):
"""
Method that would gather results from the speedtest for the given provider
"""
"""Method that would gather results from the speedtest for the given
provider."""
raise NotImplementedError("Should be implemented in daughter class")
Loading

0 comments on commit 338f677

Please sign in to comment.