Skip to content

Commit

Permalink
Merge pull request #50 from Python-World/main
Browse files Browse the repository at this point in the history
v0.0.2 stable version
  • Loading branch information
chavarera authored Aug 4, 2021
2 parents efa3b63 + bf9a9cf commit b40c58c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 31 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
- Manage multiple web drivers such as chrome,chromium,firefox.
- Different Utilities
- Retrieve elements with 5 different attributes.
- Perform clicks on element,
- Perform clicks on element.
- Take a full page and element screenshot.
- Hide and show elements.
- Information filling on different form elements such as text,radio,checkbox.
- Retrieves current cookies from the browser.
- Injecting new cookies into browser.
- Retrieve url and web page source.
- Add or modify existing cookies.
- Retrieve current user agent.
- Check Existence of an element on the page.
- Element Parser
- table Information.
Expand Down
95 changes: 68 additions & 27 deletions s_tool/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,25 @@
)


class Browser:
"""Available Browser list"""

CHROME = "chrome"
CHROMIUM = "chromium"
FIREFOX = "firefox"
IE = "ie"


class SeleniumDriver:
"""SeleniumDriver class to manage driver object and all utility functions at one place"""

def __init__(self, browser=None, headless=False):
def __init__(self, browser=None, headless=False, executable_path=None):

self.browser_list = ["chrome", "chromium", "firefox", "ie"]
self.driver = None
self.browser = browser
self.headless = headless
self.executable_path = executable_path

self._load_driver()
self._load_methods()
Expand Down Expand Up @@ -61,39 +72,69 @@ def __enter__(self):
"""
return self._load_driver()

def _exe_path(self, manager, **params):
"""Return Webdriver path
Args:
manager ([ChromeDriver,GeckoDriver,IEDriver]): WebDriver Manager
Returns:
[str]: return an executable path
"""
if self.executable_path:
return self.executable_path
else:
return manager(**params).install()

def _browser(self, browser):
"""Return if an valid browser passed
Args:
browser ([Browser]): Browser Type
Returns:
[bool]: True if browser is valid
"""
available = False
if self.browser.lower() == browser:
available = True

return available

def _load_driver(self):
"""Create Selenium webdriver object"""
self.close()
browser = self.browser.lower()
if browser not in self.browser_list:
raise SToolException(
f"provided browser {browser} doesn't exists. available brower list:{self.browser_list}"
)

# add chrome and firefox different options
options = getattr(
webdriver, browser if browser != "chromium" else "chrome"
).options.Options()
func = partial(self._exe_path)

# Add Browser Options
browser_options = browser if browser != Browser.CHROMIUM else Browser.CHROME
options = getattr(webdriver, browser_options).options.Options()

if self.headless:
options.add_argument("--headless")

if browser in ["chrome", "chromium"]:
browser_type = (
ChromeType.CHROMIUM if browser == "chromium" else ChromeType.GOOGLE
)
self.driver = webdriver.Chrome(
ChromeDriverManager(chrome_type=browser_type).install(), options=options
)

if browser == "firefox":
self.driver = webdriver.Firefox(
executable_path=GeckoDriverManager().install(), options=options
)

if browser == "ie":
self.driver = webdriver.Ie(
executable_path=IEDriverManager().install(), options=options
)
if self._browser(Browser.CHROME):
exe_path = func(ChromeDriverManager, chrome_type=ChromeType.GOOGLE)
_webdriver = webdriver.Chrome

elif self._browser(Browser.CHROMIUM):
exe_path = func(ChromeDriverManager, chrome_type=ChromeType.CHROMIUM)
_webdriver = webdriver.Chrome

elif self._browser(Browser.FIREFOX):
exe_path = func(GeckoDriverManager)
_webdriver = webdriver.Firefox

elif self._browser(Browser.IE):
exe_path = func(IEDriverManager)()
_webdriver = webdriver.Ie

else:
err = f"browser not implemented. available browers :{self.browser_list}"
raise SToolException(err)

self.driver = _webdriver(executable_path=exe_path, options=options)

# Maximize window to give normal browser feel
self.driver.maximize_window()
Expand Down
25 changes: 22 additions & 3 deletions s_tool/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ def get_session(driver: webdriver) -> str:
return driver.session_id


def is_local(path: str) -> str:
"""Return valid URL path for file
Args:
path (str): normal path or URL
Returns:
str: Modified file path if local file
Returns path as it is if URL
"""
import os

URL = path
if os.path.exists(path) or path.startswith("file"):
if not URL.startswith("file"):
URL = f"file://{URL}"
return URL


def visit(driver: webdriver, url: str) -> None:
"""Visit given url
Expand All @@ -32,7 +51,7 @@ def visit(driver: webdriver, url: str) -> None:
Returns:
None
"""
driver.get(url)
driver.get(is_local(url))


def page_source(driver: webdriver) -> str:
Expand Down Expand Up @@ -207,8 +226,8 @@ def take_screenshot(driver: webdriver, element: tuple = None) -> Union[bytes, No
return ele.screenshot_as_png
return None
else:
width = driver.execute_script("return document.body.offsetWidth")
height = driver.execute_script("return document.body.offsetHeight")
width = driver.execute_script("return document.body.parentNode.scrollWidth")
height = driver.execute_script("return document.body.parentNode.scrollHeight")
driver.set_window_size(width, height)
return driver.get_screenshot_as_png()

Expand Down
17 changes: 17 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""
Test related to utilities
"""
import os

from s_tool.utils import is_local


def test_local_path():
"""test local file path or URL"""
URL_PATH = "http://www.google.com"
URL_PATH1 = "www.google.com"
LOCAL_PATH = "tests/index.html"

assert URL_PATH == is_local(URL_PATH)
assert "file" in is_local(os.path.abspath(LOCAL_PATH))
assert URL_PATH1 == is_local(URL_PATH1)

0 comments on commit b40c58c

Please sign in to comment.