Skip to content

Support applications with multiple webviews. Add scrolling to the visible webview. #329

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 7 commits into from
Dec 23, 2021
Merged
22 changes: 22 additions & 0 deletions AppiumLibrary/keywords/_applicationmanagement.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,13 @@ def switch_to_parent_frame(self):
"""
self._current_application().switch_to.parent_frame()

def switch_to_window(self, window_name):
"""
Switch to a new webview window if the application contains multiple webviews
"""
self._current_application().switch_to.window(window_name)


def go_to_url(self, url):
"""
Opens URL in default web browser.
Expand All @@ -360,6 +367,21 @@ def get_capability(self, capability_name):
raise e
return capability

def get_window_title(self):
"""Get the current Webview window title."""
return self._current_application().title

def get_window_url(self):
"""Get the current Webview window URL."""
return self._current_application().current_url


def get_windows(self):
"""Get available Webview windows."""
print(self._current_application().window_handles)
return self._current_application().window_handles


# Private

def _current_application(self):
Expand Down
48 changes: 48 additions & 0 deletions AppiumLibrary/keywords/_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,54 @@ def get_webelement(self, locator):
"""
return self._element_find(locator, True, True)

def scroll_element_into_view(self, locator):
"""Scrolls an element from given ``locator`` into view.
Arguments:
- ``locator``: The locator to find requested element. Key attributes for
arbitrary elements are ``id`` and ``name``. See `introduction` for
details about locating elements.
Examples:
| Scroll Element Into View | css=div.class |
"""
if isinstance(locator, WebElement):
element = locator
else:
self._info("Scrolling element '%s' into view." % locator)
element = self._element_find(locator, True, True)
script = 'arguments[0].scrollIntoView()'
# pylint: disable=no-member
self._current_application().execute_script(script, element)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this apply to both ios and android

Copy link
Contributor Author

@matthew-dahm matthew-dahm Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed to work for scrolling elements in a webview on both Android and iOS.

return element

def get_webelement_in_webelement(self, element, locator):
# def _element_find(self, locator, first_only, required, tag=None):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment pleaes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up the comments

elements = None
if isstr(locator):
_locator = locator
elements = self._element_finder.find(element, _locator, None)
if len(elements) == 0:
raise ValueError("Element locator '" + locator + "' did not match any elements.")
if len(elements) == 0:
return None
return elements[0]
elif isinstance(locator, WebElement):
return locator

def get_webelements_no_error(self, locator):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does no_error means in the name of the method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, I'm new to git and didn't mean to add this this function to the pull request. I thought I could pick and choose commits that would make sense to everyone. the intention though is if the webelement query finds no webelements, instead of throwing an error, it returns an empty array so you can do a get length on it. I still need to experiment if I can accomplish the same thing with a Run Keyword And Continue on Failure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this function entirely. It can be handled via Run Keyword And Continue On Failure

"""Returns list of [http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement|WebElement] objects matching ``locator``.

Example:
| @{elements} | Get Webelements | id=my_element |
| Click Element | @{elements}[2] | |

This keyword was changed in AppiumLibrary 1.4 in following ways:
- Name is changed from `Get Elements` to current one.
- Deprecated argument ``fail_on_error``, use `Run Keyword and Ignore Error` if necessary.

New in AppiumLibrary 1.4.
"""
return self._element_find(locator, False, False)

def get_webelements(self, locator):
"""Returns list of [http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement|WebElement] objects matching ``locator``.

Expand Down