Skip to content

Commit ce0078a

Browse files
authored
Merge pull request #156 from seleniumbase/use-python-standard-library-when-possible
Use Python standard library methods when possible
2 parents aa97671 + fb4b365 commit ce0078a

File tree

5 files changed

+34
-26
lines changed

5 files changed

+34
-26
lines changed

help_docs/method_summary.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ self.remove_element(selector, by=By.CSS_SELECTOR)
114114

115115
self.remove_elements(selector, by=By.CSS_SELECTOR)
116116

117-
self.jq_format(code)
118-
119117
self.get_domain_url(url)
120118

121119
self.safe_execute_script(script)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pytest-xdist==1.22.2
99
six==1.10.0
1010
flake8==3.5.0
1111
requests==2.18.4
12-
BeautifulSoup4==4.6.0
12+
beautifulsoup4==4.6.0
1313
unittest2==1.1.0
1414
chardet==3.0.4
1515
boto==2.48.0

seleniumbase/fixtures/base_case.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def test_anything(self):
2727
import math
2828
import os
2929
import pytest
30+
import re
3031
import sys
3132
import time
3233
import traceback
@@ -549,7 +550,7 @@ def update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,
549550
if (retry and element.get_attribute('value') != new_value and (
550551
not new_value.endswith('\n'))):
551552
logging.debug('update_text_value is falling back to jQuery!')
552-
selector = self.jq_format(selector)
553+
selector = re.escape(selector)
553554
self.set_value(selector, new_value, by=by)
554555
if self.demo_mode:
555556
if self.driver.current_url != pre_action_url:
@@ -731,7 +732,7 @@ def get_property_value(self, selector, property, by=By.CSS_SELECTOR,
731732
raise Exception(
732733
"Exception: Could not convert {%s}(by=%s) to CSS_SELECTOR!" % (
733734
selector, by))
734-
selector = self.jq_format(selector)
735+
selector = re.escape(selector)
735736
script = ("""var $elm = document.querySelector('%s');
736737
$val = window.getComputedStyle($elm).getPropertyValue('%s');
737738
return $val;"""
@@ -755,7 +756,7 @@ def bring_to_front(self, selector, by=By.CSS_SELECTOR):
755756
except Exception:
756757
# Don't run action if can't convert to CSS_Selector for JavaScript
757758
return
758-
selector = self.jq_format(selector)
759+
selector = re.escape(selector)
759760
script = ("""document.querySelector('%s').style.zIndex = "100";"""
760761
% selector)
761762
self.execute_script(script)
@@ -796,11 +797,11 @@ def highlight(self, selector, by=By.CSS_SELECTOR,
796797
o_bs = original_box_shadow
797798

798799
if ":contains" not in selector and ":first" not in selector:
799-
selector = self.jq_format(selector)
800+
selector = re.escape(selector)
800801
self.__highlight_with_js(selector, loops, scroll, o_bs)
801802
else:
802803
selector = self._make_css_match_first_element_only(selector)
803-
selector = self.jq_format(selector)
804+
selector = re.escape(selector)
804805
try:
805806
self.__highlight_with_jquery(selector, loops, scroll, o_bs)
806807
except Exception:
@@ -978,7 +979,8 @@ def remove_elements(self, selector, by=By.CSS_SELECTOR):
978979
self.safe_execute_script(remove_script)
979980

980981
def jq_format(self, code):
981-
return page_utils.jq_format(code)
982+
# DEPRECATED - Use re.escape() instead, which does the action you want.
983+
return page_utils._jq_format(code)
982984

983985
def get_domain_url(self, url):
984986
return page_utils.get_domain_url(url)
@@ -1056,18 +1058,25 @@ def convert_to_css_selector(self, selector, by):
10561058

10571059
def set_value(self, selector, new_value, by=By.CSS_SELECTOR,
10581060
timeout=settings.LARGE_TIMEOUT):
1059-
""" This method uses jQuery to update a text field. """
1061+
""" This method uses jQuery to update a text field.
1062+
Similar to jquery_update_text_value(), but the element
1063+
doesn't need to be officially visible to work. """
10601064
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
10611065
timeout = self._get_new_timeout(timeout)
10621066
if page_utils.is_xpath_selector(selector):
10631067
by = By.XPATH
1068+
orginal_selector = selector
10641069
selector = self.convert_to_css_selector(selector, by=by)
10651070
self._demo_mode_highlight_if_active(selector, by)
10661071
self.scroll_to(selector, by=by, timeout=timeout)
10671072
value = json.dumps(new_value)
10681073
selector = self._make_css_match_first_element_only(selector)
10691074
set_value_script = """jQuery('%s').val(%s)""" % (selector, value)
10701075
self.safe_execute_script(set_value_script)
1076+
if new_value.endswith('\n'):
1077+
element = self.wait_for_element_present(
1078+
orginal_selector, by=by, timeout=timeout)
1079+
element.send_keys(Keys.RETURN)
10711080
self._demo_mode_pause_if_active()
10721081

10731082
def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,
@@ -1087,7 +1096,7 @@ def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,
10871096
selector = self.convert_to_css_selector(selector, by=by)
10881097
selector = self._make_css_match_first_element_only(selector)
10891098
update_text_script = """jQuery('%s').val('%s')""" % (
1090-
selector, self.jq_format(new_value))
1099+
selector, re.escape(new_value))
10911100
self.safe_execute_script(update_text_script)
10921101
if new_value.endswith('\n'):
10931102
element.send_keys('\n')

seleniumbase/fixtures/page_utils.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,6 @@
55
import requests
66

77

8-
def jq_format(code):
9-
"""
10-
Use before throwing raw code such as 'div[tab="advanced"]' into jQuery.
11-
Selectors with quotes inside of quotes would otherwise break jQuery.
12-
This is similar to "json.dumps(value)", but with one less layer of quotes.
13-
"""
14-
code = code.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n')
15-
code = code.replace('\"', '\\\"').replace('\'', '\\\'')
16-
code = code.replace('\v', '\\v').replace('\a', '\\a').replace('\f', '\\f')
17-
code = code.replace('\b', '\\b').replace(r'\u', '\\u').replace('\r', '\\r')
18-
return code
19-
20-
218
def get_domain_url(url):
229
"""
2310
Use this to convert a url like this:
@@ -87,3 +74,17 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):
8774
r = requests.get(file_url)
8875
with open(destination_folder + '/' + file_name, "wb") as code:
8976
code.write(r.content)
77+
78+
79+
def _jq_format(code):
80+
"""
81+
DEPRECATED - Use re.escape() instead, which performs the intended action.
82+
Use before throwing raw code such as 'div[tab="advanced"]' into jQuery.
83+
Selectors with quotes inside of quotes would otherwise break jQuery.
84+
This is similar to "json.dumps(value)", but with one less layer of quotes.
85+
"""
86+
code = code.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n')
87+
code = code.replace('\"', '\\\"').replace('\'', '\\\'')
88+
code = code.replace('\v', '\\v').replace('\a', '\\a').replace('\f', '\\f')
89+
code = code.replace('\b', '\\b').replace(r'\u', '\\u').replace('\r', '\\r')
90+
return code

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
setup(
99
name='seleniumbase',
10-
version='1.8.3',
10+
version='1.8.4',
1111
description='Web Automation & Testing Framework - http://seleniumbase.com',
1212
long_description='Web Automation and Testing Framework - seleniumbase.com',
1313
platforms='Mac * Windows * Linux * Docker',
@@ -28,7 +28,7 @@
2828
'six==1.10.0',
2929
'flake8==3.5.0',
3030
'requests==2.18.4',
31-
'BeautifulSoup4==4.6.0',
31+
'beautifulsoup4==4.6.0',
3232
'unittest2==1.1.0',
3333
'chardet==3.0.4',
3434
'boto==2.48.0',

0 commit comments

Comments
 (0)