Skip to content

Use Python standard library methods when possible #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions help_docs/method_summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ self.remove_element(selector, by=By.CSS_SELECTOR)

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

self.jq_format(code)

self.get_domain_url(url)

self.safe_execute_script(script)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pytest-xdist==1.22.2
six==1.10.0
flake8==3.5.0
requests==2.18.4
BeautifulSoup4==4.6.0
beautifulsoup4==4.6.0
unittest2==1.1.0
chardet==3.0.4
boto==2.48.0
Expand Down
25 changes: 17 additions & 8 deletions seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_anything(self):
import math
import os
import pytest
import re
import sys
import time
import traceback
Expand Down Expand Up @@ -549,7 +550,7 @@ def update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,
if (retry and element.get_attribute('value') != new_value and (
not new_value.endswith('\n'))):
logging.debug('update_text_value is falling back to jQuery!')
selector = self.jq_format(selector)
selector = re.escape(selector)
self.set_value(selector, new_value, by=by)
if self.demo_mode:
if self.driver.current_url != pre_action_url:
Expand Down Expand Up @@ -731,7 +732,7 @@ def get_property_value(self, selector, property, by=By.CSS_SELECTOR,
raise Exception(
"Exception: Could not convert {%s}(by=%s) to CSS_SELECTOR!" % (
selector, by))
selector = self.jq_format(selector)
selector = re.escape(selector)
script = ("""var $elm = document.querySelector('%s');
$val = window.getComputedStyle($elm).getPropertyValue('%s');
return $val;"""
Expand All @@ -755,7 +756,7 @@ def bring_to_front(self, selector, by=By.CSS_SELECTOR):
except Exception:
# Don't run action if can't convert to CSS_Selector for JavaScript
return
selector = self.jq_format(selector)
selector = re.escape(selector)
script = ("""document.querySelector('%s').style.zIndex = "100";"""
% selector)
self.execute_script(script)
Expand Down Expand Up @@ -796,11 +797,11 @@ def highlight(self, selector, by=By.CSS_SELECTOR,
o_bs = original_box_shadow

if ":contains" not in selector and ":first" not in selector:
selector = self.jq_format(selector)
selector = re.escape(selector)
self.__highlight_with_js(selector, loops, scroll, o_bs)
else:
selector = self._make_css_match_first_element_only(selector)
selector = self.jq_format(selector)
selector = re.escape(selector)
try:
self.__highlight_with_jquery(selector, loops, scroll, o_bs)
except Exception:
Expand Down Expand Up @@ -978,7 +979,8 @@ def remove_elements(self, selector, by=By.CSS_SELECTOR):
self.safe_execute_script(remove_script)

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

def get_domain_url(self, url):
return page_utils.get_domain_url(url)
Expand Down Expand Up @@ -1056,18 +1058,25 @@ def convert_to_css_selector(self, selector, by):

def set_value(self, selector, new_value, by=By.CSS_SELECTOR,
timeout=settings.LARGE_TIMEOUT):
""" This method uses jQuery to update a text field. """
""" This method uses jQuery to update a text field.
Similar to jquery_update_text_value(), but the element
doesn't need to be officially visible to work. """
if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
timeout = self._get_new_timeout(timeout)
if page_utils.is_xpath_selector(selector):
by = By.XPATH
orginal_selector = selector
selector = self.convert_to_css_selector(selector, by=by)
self._demo_mode_highlight_if_active(selector, by)
self.scroll_to(selector, by=by, timeout=timeout)
value = json.dumps(new_value)
selector = self._make_css_match_first_element_only(selector)
set_value_script = """jQuery('%s').val(%s)""" % (selector, value)
self.safe_execute_script(set_value_script)
if new_value.endswith('\n'):
element = self.wait_for_element_present(
orginal_selector, by=by, timeout=timeout)
element.send_keys(Keys.RETURN)
self._demo_mode_pause_if_active()

def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,
Expand All @@ -1087,7 +1096,7 @@ def jquery_update_text_value(self, selector, new_value, by=By.CSS_SELECTOR,
selector = self.convert_to_css_selector(selector, by=by)
selector = self._make_css_match_first_element_only(selector)
update_text_script = """jQuery('%s').val('%s')""" % (
selector, self.jq_format(new_value))
selector, re.escape(new_value))
self.safe_execute_script(update_text_script)
if new_value.endswith('\n'):
element.send_keys('\n')
Expand Down
27 changes: 14 additions & 13 deletions seleniumbase/fixtures/page_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,6 @@
import requests


def jq_format(code):
"""
Use before throwing raw code such as 'div[tab="advanced"]' into jQuery.
Selectors with quotes inside of quotes would otherwise break jQuery.
This is similar to "json.dumps(value)", but with one less layer of quotes.
"""
code = code.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n')
code = code.replace('\"', '\\\"').replace('\'', '\\\'')
code = code.replace('\v', '\\v').replace('\a', '\\a').replace('\f', '\\f')
code = code.replace('\b', '\\b').replace(r'\u', '\\u').replace('\r', '\\r')
return code


def get_domain_url(url):
"""
Use this to convert a url like this:
Expand Down Expand Up @@ -87,3 +74,17 @@ def _download_file_to(file_url, destination_folder, new_file_name=None):
r = requests.get(file_url)
with open(destination_folder + '/' + file_name, "wb") as code:
code.write(r.content)


def _jq_format(code):
"""
DEPRECATED - Use re.escape() instead, which performs the intended action.
Use before throwing raw code such as 'div[tab="advanced"]' into jQuery.
Selectors with quotes inside of quotes would otherwise break jQuery.
This is similar to "json.dumps(value)", but with one less layer of quotes.
"""
code = code.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n')
code = code.replace('\"', '\\\"').replace('\'', '\\\'')
code = code.replace('\v', '\\v').replace('\a', '\\a').replace('\f', '\\f')
code = code.replace('\b', '\\b').replace(r'\u', '\\u').replace('\r', '\\r')
return code
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name='seleniumbase',
version='1.8.3',
version='1.8.4',
description='Web Automation & Testing Framework - http://seleniumbase.com',
long_description='Web Automation and Testing Framework - seleniumbase.com',
platforms='Mac * Windows * Linux * Docker',
Expand All @@ -28,7 +28,7 @@
'six==1.10.0',
'flake8==3.5.0',
'requests==2.18.4',
'BeautifulSoup4==4.6.0',
'beautifulsoup4==4.6.0',
'unittest2==1.1.0',
'chardet==3.0.4',
'boto==2.48.0',
Expand Down