Skip to content
Permalink
Browse files

Fixup the Select problems by adding more JS and updating PhantomJS in…

… the travis env
  • Loading branch information
Milan Falešník
Milan Falešník committed Oct 20, 2016
1 parent 3fc1011 commit 9a490223a7f5c9abcd685aca3ea885db5f4da1ce
Showing with 43 additions and 10 deletions.
  1. +9 −0 .travis.yml
  2. +11 −4 src/widgetastic/browser.py
  3. +23 −6 src/widgetastic/widget.py
@@ -17,3 +17,12 @@ deploy:
tags: true
distributions: sdist bdist_wheel
repo: RedHatQE/widgetastic.core
before_install:
- "export PHANTOMJS_VERSION=2.1.1"
- "phantomjs --version"
- "export PATH=$PWD/travis_phantomjs/phantomjs-$PHANTOMJS_VERSION-linux-x86_64/bin:$PATH"
- "phantomjs --version"
- "if [ $(phantomjs --version) != '$PHANTOMJS_VERSION' ]; then rm -rf $PWD/travis_phantomjs; mkdir -p $PWD/travis_phantomjs; fi"
- "if [ $(phantomjs --version) != '$PHANTOMJS_VERSION' ]; then wget https://github.com/Medium/phantomjs/releases/download/v$PHANTOMJS_VERSION/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -O $PWD/travis_phantomjs/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2; fi"
- "if [ $(phantomjs --version) != '$PHANTOMJS_VERSION' ]; then tar -xvf $PWD/travis_phantomjs/phantomjs-$PHANTOMJS_VERSION-linux-x86_64.tar.bz2 -C $PWD/travis_phantomjs; fi"
- "phantomjs --version"
@@ -50,7 +50,6 @@ def logger(self):

def ensure_page_safe(self, timeout='10s'):
# THIS ONE SHOULD ALWAYS USE JAVASCRIPT ONLY, NO OTHER SELENIUM INTERACTION
self.browser.dismiss_any_alerts()

def _check():
result = self.browser.execute_script(self.ENSURE_PAGE_SAFE)
@@ -60,7 +59,7 @@ def _check():
except AttributeError:
return True

wait_for(_check, timeout=timeout, delay=0.2)
wait_for(_check, timeout=timeout, delay=0.2, very_quiet=True)

def after_click(self, element):
"""Invoked after clicking on an element."""
@@ -190,7 +189,9 @@ def _locator_force_visibility_check(locator):
else:
return None

def elements(self, locator, parent=None, check_visibility=False):
def elements(
self, locator, parent=None, check_visibility=False, check_safe=True,
force_check_safe=False):
"""Method that resolves locators into selenium webelements.
Args:
@@ -204,11 +205,17 @@ def elements(self, locator, parent=None, check_visibility=False):
* Any other object that implements ``__locator__``
parent: A parent element identificator. Can be any valid locator.
check_visibility: If set to ``True`` it will filter out elements that are not visible.
check_safe: You can turn off the page safety check. It is turned off automatically when
:py:class:`WebElement` is passed.
force_check_safe: If you want to override the :py:class:`WebElement` detection and force
the page safety check, pass True.
Returns:
A :py:class:`list` of :py:class:`selenium.webdriver.remote.webelement.WebElement`
"""
self.plugin.ensure_page_safe()
if (check_safe and not isinstance(locator, WebElement)) or force_check_safe:
# If we have a webelement so it is pointless to check it
self.plugin.ensure_page_safe()
locator = self._process_locator(locator)
# Get result
if isinstance(locator, WebElement):
@@ -964,6 +964,23 @@ class Select(Widget):
''')

SELECTED_OPTIONS = jsmin('return arguments[0].selectedOptions;')
SELECTED_OPTIONS_TEXT = jsmin('''\
var result_arr = [];
var opt_elements = arguments[0].selectedOptions;
for(var i = 0; i < opt_elements.length; i++){
result_arr.push(opt_elements[i].innerHTML);
}
return result_arr;
''')

SELECTED_OPTIONS_VALUE = jsmin('''\
var result_arr = [];
var opt_elements = arguments[0].selectedOptions;
for(var i = 0; i < opt_elements.length; i++){
result_arr.push(opt_elements[i].getAttribute("value"));
}
return result_arr;
''')

def __init__(self, parent, locator=None, id=None, name=None, logger=None):
Widget.__init__(self, parent, logger=logger)
@@ -1012,21 +1029,21 @@ def all_options(self):
@property
def all_selected_options(self):
"""Returns a list of all selected options as their displayed texts."""
parser = html_parser.HTMLParser()
return [
self.browser.text(option)
normalize_space(parser.unescape(option))
for option
in self.browser.execute_script(self.SELECTED_OPTIONS, self.browser.element(self))]
in self.browser.execute_script(self.SELECTED_OPTIONS_TEXT, self.browser.element(self))]

@property
def all_selected_values(self):
"""Returns a list of all selected options as their values.
If the value is not present, it is ignored.
"""
values = [
self.browser.get_attribute('value', option)
for option
in self.browser.execute_script(self.SELECTED_OPTIONS, self.browser.element(self))]
values = self.browser.execute_script(
self.SELECTED_OPTIONS_VALUE,
self.browser.element(self))
return [value for value in values if value is not None]

@property

0 comments on commit 9a49022

Please sign in to comment.
You can’t perform that action at this time.