Skip to content

Commit ce397b9

Browse files
committed
completed
1 parent a04d703 commit ce397b9

File tree

12 files changed

+184
-0
lines changed

12 files changed

+184
-0
lines changed

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2020 Mukul Ram
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

dist/selenium-helpers-1.0.1.tar.gz

1 KB
Binary file not shown.
1.99 KB
Binary file not shown.

selenium_helpers.egg-info/PKG-INFO

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Metadata-Version: 2.1
2+
Name: selenium-helpers
3+
Version: 1.0.1
4+
Summary: Tools to make certain selenium tasks more straightforward.
5+
Home-page: https://github.com/ExSidius/selenium_helpers
6+
Author: ExSidius
7+
Author-email: [email protected]
8+
License: MIT
9+
Description: # selenium_helpers
10+
Helpful utilities and wrappers for Selenium work.
11+
12+
Platform: UNKNOWN
13+
Description-Content-Type: text/markdown

selenium_helpers.egg-info/SOURCES.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
README.md
2+
setup.py
3+
selenium_helpers.egg-info/PKG-INFO
4+
selenium_helpers.egg-info/SOURCES.txt
5+
selenium_helpers.egg-info/dependency_links.txt
6+
selenium_helpers.egg-info/not-zip-safe
7+
selenium_helpers.egg-info/requires.txt
8+
selenium_helpers.egg-info/top_level.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
selenium
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

selenium_helpers/__init__.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import datetime
2+
import random
3+
import time
4+
5+
from selenium.common.exceptions import StaleElementReferenceException
6+
from selenium.webdriver.common.by import By
7+
from selenium.webdriver.common.keys import Keys
8+
from selenium.webdriver.support import expected_conditions as EC
9+
from selenium.webdriver.support.ui import WebDriverWait
10+
11+
12+
def click(driver, element):
13+
driver.execute_script("arguments[0].click();", element)
14+
15+
16+
class YouFindPlaceHolder:
17+
def __init__(self, text):
18+
self.text = text
19+
20+
def __call__(self, driver):
21+
element = driver.find_element_by_xpath(f'//input[@placeholder="{self.text}"]')
22+
if element:
23+
return element
24+
else:
25+
return False
26+
27+
28+
class YouFindText:
29+
def __init__(self, text):
30+
self.text = text
31+
32+
def __call__(self, driver):
33+
element = driver.find_element_by_xpath(f'//*[contains(text(), "{self.text}")]')
34+
if element:
35+
return element
36+
else:
37+
return False
38+
39+
40+
class YouFindButtonText:
41+
def __init__(self, text):
42+
self.text = text
43+
44+
def __call__(self, driver):
45+
element = driver.find_element_by_xpath(f'//button[.="{self.text}"]')
46+
if element:
47+
return element
48+
else:
49+
return False
50+
51+
52+
class YouFindAllText:
53+
def __init__(self, text):
54+
self.text = text
55+
56+
def __call__(self, driver):
57+
elements = driver.find_elements_by_xpath(f'//*[contains(text(), "{self.text}")]')
58+
if elements:
59+
return elements
60+
else:
61+
return False
62+
63+
64+
class URLToBe:
65+
def __init__(self, url):
66+
self.url = url
67+
68+
def __call__(self, driver):
69+
try:
70+
res = EC.url_to_be(self.url)(driver)
71+
if res:
72+
return res
73+
except: # pylint: disable=bare-except
74+
pass
75+
return EC.url_to_be(f'{self.url}/')(driver)
76+
77+
78+
def wait_until(driver, expected_condition, value, timeout=10):
79+
conveniences = {
80+
'title is': EC.title_is,
81+
'title contains': EC.title_contains,
82+
'url matches': EC.url_matches,
83+
'url is': URLToBe,
84+
'url contains': EC.url_contains,
85+
'you find': EC.presence_of_element_located,
86+
'you find all': EC.presence_of_all_elements_located,
87+
'you find text': YouFindText,
88+
'you find button text': YouFindButtonText,
89+
'you find all text': YouFindAllText,
90+
'you find placeholder': YouFindPlaceHolder,
91+
"you don't find": EC.staleness_of,
92+
}
93+
ec = conveniences[expected_condition]
94+
msg = f'{expected_condition} {value}'
95+
return WebDriverWait(driver, timeout).until(ec(value), message=msg)
96+
97+
98+
def fill_searchable_select(driver, element, content):
99+
el = wait_until(driver, 'you find', element)
100+
click(driver, el)
101+
el.clear()
102+
el.send_keys(content)
103+
while True:
104+
select_items = wait_until(driver, 'you find all', (By.CLASS_NAME, 'el-select-dropdown__item'))
105+
try:
106+
if len([item for item in select_items if item.text == content]) == 1:
107+
break
108+
except StaleElementReferenceException:
109+
pass
110+
el.send_keys(Keys.ENTER)
111+
112+
113+
def potential_refresh(driver, expected_condition, value, chance=0.99):
114+
if random.uniform(0, 1) > chance:
115+
driver.refresh()
116+
return wait_until(driver, expected_condition, value)
117+
118+
119+
def timeout(duration, exception):
120+
def decorator(f):
121+
def wrapper(*args, **kwargs):
122+
start = datetime.datetime.now()
123+
while True:
124+
if f(*args, **kwargs):
125+
break
126+
127+
if (datetime.datetime.now() - start).seconds // 60 > duration:
128+
raise Exception(exception)
129+
130+
return wrapper
131+
132+
return decorator
Binary file not shown.

setup.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from setuptools import setup
2+
3+
def readme():
4+
with open('README.md', 'r') as file:
5+
return file.read()
6+
7+
setup(
8+
name='selenium-helpers',
9+
version='1.0.1',
10+
description='Tools to make certain selenium tasks more straightforward.',
11+
long_description=readme(),
12+
long_description_content_type='text/markdown',
13+
url='https://github.com/ExSidius/selenium_helpers',
14+
author='ExSidius',
15+
author_email='[email protected]',
16+
license='MIT',
17+
install_requires=['selenium'],
18+
include_package_data=True,
19+
zip_safe=False,
20+
)

0 commit comments

Comments
 (0)