2012-05-21 15:12:37 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-07-17 19:01:36 +04:00
|
|
|
import base64
|
2013-12-20 17:12:40 +04:00
|
|
|
import ConfigParser
|
2014-07-23 01:27:25 +04:00
|
|
|
import json
|
2013-06-22 04:13:35 +04:00
|
|
|
import os
|
2012-03-26 23:40:25 +04:00
|
|
|
import socket
|
2014-06-19 22:17:26 +04:00
|
|
|
import StringIO
|
2013-06-22 04:13:35 +04:00
|
|
|
import traceback
|
2014-07-17 19:01:36 +04:00
|
|
|
import warnings
|
|
|
|
|
2014-11-07 19:35:43 +03:00
|
|
|
from contextlib import contextmanager
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-02-28 01:36:00 +04:00
|
|
|
from decorators import do_crash_check
|
2013-12-17 20:01:29 +04:00
|
|
|
from keys import Keys
|
2015-10-09 13:15:24 +03:00
|
|
|
import marionette_transport as transport
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-06-19 22:17:26 +04:00
|
|
|
from mozrunner import B2GEmulatorRunner
|
|
|
|
|
2013-12-17 20:01:29 +04:00
|
|
|
import geckoinstance
|
2014-06-19 22:17:26 +04:00
|
|
|
import errors
|
2012-11-19 21:32:57 +04:00
|
|
|
|
2015-09-15 22:45:39 +03:00
|
|
|
WEBELEMENT_KEY = "ELEMENT"
|
|
|
|
W3C_WEBELEMENT_KEY = "element-6066-11e4-a52e-4f735466cecf"
|
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
class HTMLElement(object):
|
2013-08-20 21:59:20 +04:00
|
|
|
"""
|
|
|
|
Represents a DOM Element.
|
|
|
|
"""
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def __init__(self, marionette, id):
|
|
|
|
self.marionette = marionette
|
|
|
|
assert(id is not None)
|
|
|
|
self.id = id
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.id
|
|
|
|
|
2012-08-10 00:01:56 +04:00
|
|
|
def __eq__(self, other_element):
|
|
|
|
return self.id == other_element.id
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def find_element(self, method, target):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns an ``HTMLElement`` instance that matches the specified
|
|
|
|
method and target, relative to the current element.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
For more details on this function, see the `find_element` method
|
|
|
|
in the Marionette class.
|
|
|
|
"""
|
2012-03-26 23:40:25 +04:00
|
|
|
return self.marionette.find_element(method, target, self.id)
|
|
|
|
|
|
|
|
def find_elements(self, method, target):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns a list of all ``HTMLElement`` instances that match the
|
|
|
|
specified method and target in the current context.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
For more details on this function, see the find_elements method
|
|
|
|
in the Marionette class.
|
|
|
|
"""
|
2012-03-26 23:40:25 +04:00
|
|
|
return self.marionette.find_elements(method, target, self.id)
|
|
|
|
|
|
|
|
def get_attribute(self, attribute):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns the requested attribute, or None if no attribute
|
|
|
|
is set.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param attribute: The name of the attribute.
|
2015-08-15 17:46:31 +03:00
|
|
|
"""
|
|
|
|
body = {"id": self.id, "name": attribute}
|
|
|
|
return self.marionette._send_message("getElementAttribute", body, key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def click(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
self.marionette._send_message("clickElement", {"id": self.id})
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-04-27 02:18:55 +04:00
|
|
|
def tap(self, x=None, y=None):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Simulates a set of tap events on the element.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
:param x: X coordinate of tap event. If not given, default to
|
|
|
|
the centre of the element.
|
|
|
|
:param y: Y coordinate of tap event. If not given, default to
|
|
|
|
the centre of the element.
|
|
|
|
"""
|
|
|
|
body = {"id": self.id, "x": x, "y": y}
|
|
|
|
self.marionette._send_message("singleTap", body)
|
2013-01-22 23:27:44 +04:00
|
|
|
|
2012-07-28 00:50:00 +04:00
|
|
|
@property
|
2012-03-26 23:40:25 +04:00
|
|
|
def text(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns the visible text of the element, and its child elements."""
|
|
|
|
body = {"id": self.id}
|
|
|
|
return self.marionette._send_message("getElementText", body, key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-08-05 23:49:42 +04:00
|
|
|
def send_keys(self, *string):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Sends the string via synthesized keypresses to the element."""
|
2015-02-11 04:44:52 +03:00
|
|
|
keys = Marionette.convert_keys(*string)
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"id": self.id, "value": keys}
|
|
|
|
self.marionette._send_message("sendKeysToElement", body)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def clear(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Clears the input of the element."""
|
|
|
|
self.marionette._send_message("clearElement", {"id": self.id})
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-08-12 22:54:42 +04:00
|
|
|
def is_selected(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns True if the element is selected."""
|
|
|
|
body = {"id": self.id}
|
|
|
|
return self.marionette._send_message("isElementSelected", body, key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-08-15 05:21:41 +04:00
|
|
|
def is_enabled(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""This command will return False if all the following criteria
|
|
|
|
are met otherwise return True:
|
2014-08-19 03:07:56 +04:00
|
|
|
|
|
|
|
* A form control is disabled.
|
2015-08-15 17:46:31 +03:00
|
|
|
* A ``HTMLElement`` has a disabled boolean attribute.
|
|
|
|
"""
|
|
|
|
body = {"id": self.id}
|
|
|
|
return self.marionette._send_message("isElementEnabled", body, key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-07-11 16:30:46 +04:00
|
|
|
def is_displayed(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns True if the element is displayed, False otherwise."""
|
|
|
|
body = {"id": self.id}
|
|
|
|
return self.marionette._send_message("isElementDisplayed", body, key="value")
|
2012-11-19 21:32:57 +04:00
|
|
|
|
2012-10-20 00:59:15 +04:00
|
|
|
@property
|
|
|
|
def size(self):
|
2015-04-17 19:02:14 +03:00
|
|
|
"""A dictionary with the size of the element."""
|
2014-07-17 19:01:36 +04:00
|
|
|
warnings.warn("The size property has been deprecated and will be removed in a future version. \
|
|
|
|
Please use HTMLElement#rect", DeprecationWarning)
|
2015-04-17 19:02:14 +03:00
|
|
|
rect = self.rect
|
|
|
|
return {"width": rect["width"], "height": rect["height"]}
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-08-09 00:21:50 +04:00
|
|
|
@property
|
|
|
|
def tag_name(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""The tag name of the element."""
|
|
|
|
body = {"id": self.id}
|
|
|
|
return self.marionette._send_message("getElementTagName", body, key="value")
|
2012-08-09 00:21:50 +04:00
|
|
|
|
2012-10-20 00:35:30 +04:00
|
|
|
@property
|
|
|
|
def location(self):
|
2014-01-22 23:04:04 +04:00
|
|
|
"""Get an element's location on the page.
|
|
|
|
|
|
|
|
The returned point will contain the x and y coordinates of the
|
|
|
|
top left-hand corner of the given element. The point (0,0)
|
|
|
|
refers to the upper-left corner of the document.
|
|
|
|
|
|
|
|
:returns: a dictionary containing x and y as entries
|
|
|
|
"""
|
2014-07-17 19:01:36 +04:00
|
|
|
warnings.warn("The location property has been deprecated and will be removed in a future version. \
|
|
|
|
Please use HTMLElement#rect", DeprecationWarning)
|
2015-04-17 19:02:14 +03:00
|
|
|
rect = self.rect
|
|
|
|
return {"x": rect["x"], "y": rect["y"]}
|
2012-10-20 00:35:30 +04:00
|
|
|
|
2014-07-16 23:58:37 +04:00
|
|
|
@property
|
|
|
|
def rect(self):
|
2015-04-17 19:02:14 +03:00
|
|
|
"""Gets the element's bounding rectangle.
|
2015-08-15 17:46:31 +03:00
|
|
|
|
2015-04-17 19:02:14 +03:00
|
|
|
This will return a dictionary with the following:
|
|
|
|
|
|
|
|
* x and y represent the top left coordinates of the ``HTMLElement``
|
|
|
|
relative to top left corner of the document.
|
|
|
|
* height and the width will contain the height and the width
|
|
|
|
of the DOMRect of the ``HTMLElement``.
|
2014-07-16 23:58:37 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"id": self.id}
|
|
|
|
return self.marionette._send_message(
|
|
|
|
"getElementRect", body, key="value" if self.marionette.protocol == 1 else None)
|
2014-07-16 23:58:37 +04:00
|
|
|
|
2013-05-08 01:38:21 +04:00
|
|
|
def value_of_css_property(self, property_name):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Gets the value of the specified CSS property name.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param property_name: Property name to get the value of.
|
2015-08-15 17:46:31 +03:00
|
|
|
"""
|
|
|
|
body = {"id": self.id, "propertyName": property_name}
|
|
|
|
return self.marionette._send_message(
|
|
|
|
"getElementValueOfCssProperty", body, key="value")
|
|
|
|
|
2013-05-08 01:38:21 +04:00
|
|
|
|
2015-02-28 04:30:07 +03:00
|
|
|
class MouseButton(object):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Enum-like class for mouse button constants."""
|
2015-02-28 04:30:07 +03:00
|
|
|
LEFT = 0
|
|
|
|
MIDDLE = 1
|
|
|
|
RIGHT = 2
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
|
2013-03-05 05:09:58 +04:00
|
|
|
class Actions(object):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
An Action object represents a set of actions that are executed in a particular order.
|
|
|
|
|
|
|
|
All action methods (press, etc.) return the Actions object itself, to make
|
|
|
|
it easy to create a chain of events.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
# get html file
|
|
|
|
testAction = marionette.absolute_url("testFool.html")
|
|
|
|
# navigate to the file
|
|
|
|
marionette.navigate(testAction)
|
|
|
|
# find element1 and element2
|
|
|
|
element1 = marionette.find_element("id", "element1")
|
|
|
|
element2 = marionette.find_element("id", "element2")
|
|
|
|
# create action object
|
|
|
|
action = Actions(marionette)
|
|
|
|
# add actions (press, wait, move, release) into the object
|
|
|
|
action.press(element1).wait(5). move(element2).release()
|
|
|
|
# fire all the added events
|
|
|
|
action.perform()
|
|
|
|
'''
|
|
|
|
|
2013-03-05 05:09:58 +04:00
|
|
|
def __init__(self, marionette):
|
|
|
|
self.action_chain = []
|
|
|
|
self.marionette = marionette
|
2013-03-22 22:29:02 +04:00
|
|
|
self.current_id = None
|
2013-03-05 05:09:58 +04:00
|
|
|
|
|
|
|
def press(self, element, x=None, y=None):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Sends a 'touchstart' event to this element.
|
|
|
|
|
|
|
|
If no coordinates are given, it will be targeted at the center of the
|
|
|
|
element. If given, it will be targeted at the (x,y) coordinates
|
|
|
|
relative to the top-left corner of the element.
|
|
|
|
|
|
|
|
:param element: The element to press on.
|
|
|
|
:param x: Optional, x-coordinate to tap, relative to the top-left
|
|
|
|
corner of the element.
|
|
|
|
:param y: Optional, y-coordinate to tap, relative to the top-left
|
|
|
|
corner of the element.
|
|
|
|
'''
|
2013-03-05 05:09:58 +04:00
|
|
|
element=element.id
|
|
|
|
self.action_chain.append(['press', element, x, y])
|
|
|
|
return self
|
|
|
|
|
|
|
|
def release(self):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Sends a 'touchend' event to this element.
|
|
|
|
|
|
|
|
May only be called if press() has already be called on this element.
|
|
|
|
|
|
|
|
If press and release are chained without a move action between them,
|
|
|
|
then it will be processed as a 'tap' event, and will dispatch the
|
|
|
|
expected mouse events ('mousemove' (if necessary), 'mousedown',
|
|
|
|
'mouseup', 'mouseclick') after the touch events. If there is a wait
|
|
|
|
period between press and release that will trigger a contextmenu,
|
|
|
|
then the 'contextmenu' menu event will be fired instead of the
|
|
|
|
touch/mouse events.
|
|
|
|
'''
|
2013-03-05 05:09:58 +04:00
|
|
|
self.action_chain.append(['release'])
|
|
|
|
return self
|
|
|
|
|
|
|
|
def move(self, element):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Sends a 'touchmove' event at the center of the target element.
|
|
|
|
|
|
|
|
:param element: Element to move towards.
|
|
|
|
|
|
|
|
May only be called if press() has already be called.
|
|
|
|
'''
|
2013-03-05 05:09:58 +04:00
|
|
|
element=element.id
|
|
|
|
self.action_chain.append(['move', element])
|
|
|
|
return self
|
|
|
|
|
|
|
|
def move_by_offset(self, x, y):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Sends 'touchmove' event to the given x, y coordinates relative to the top-left of the currently touched element.
|
|
|
|
|
|
|
|
May only be called if press() has already be called.
|
|
|
|
|
|
|
|
:param x: Specifies x-coordinate of move event, relative to the
|
|
|
|
top-left corner of the element.
|
|
|
|
:param y: Specifies y-coordinate of move event, relative to the
|
|
|
|
top-left corner of the element.
|
|
|
|
'''
|
2013-03-05 05:09:58 +04:00
|
|
|
self.action_chain.append(['moveByOffset', x, y])
|
|
|
|
return self
|
|
|
|
|
|
|
|
def wait(self, time=None):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Waits for specified time period.
|
|
|
|
|
|
|
|
:param time: Time in seconds to wait. If time is None then this has no effect for a single action chain. If used inside a multi-action chain, then time being None indicates that we should wait for all other currently executing actions that are part of the chain to complete.
|
|
|
|
'''
|
2013-03-05 05:09:58 +04:00
|
|
|
self.action_chain.append(['wait', time])
|
|
|
|
return self
|
|
|
|
|
2013-03-20 01:59:48 +04:00
|
|
|
def cancel(self):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Sends 'touchcancel' event to the target of the original 'touchstart' event.
|
|
|
|
|
|
|
|
May only be called if press() has already be called.
|
|
|
|
'''
|
2013-03-20 01:59:48 +04:00
|
|
|
self.action_chain.append(['cancel'])
|
|
|
|
return self
|
|
|
|
|
2013-04-27 02:18:55 +04:00
|
|
|
def tap(self, element, x=None, y=None):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Performs a quick tap on the target element.
|
|
|
|
|
|
|
|
:param element: The element to tap.
|
|
|
|
:param x: Optional, x-coordinate of tap, relative to the top-left
|
|
|
|
corner of the element. If not specified, default to center of
|
|
|
|
element.
|
|
|
|
:param y: Optional, y-coordinate of tap, relative to the top-left
|
|
|
|
corner of the element. If not specified, default to center of
|
|
|
|
element.
|
|
|
|
|
|
|
|
This is equivalent to calling:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
action.press(element, x, y).release()
|
|
|
|
'''
|
2013-04-27 02:18:55 +04:00
|
|
|
element=element.id
|
|
|
|
self.action_chain.append(['press', element, x, y])
|
|
|
|
self.action_chain.append(['release'])
|
|
|
|
return self
|
|
|
|
|
|
|
|
def double_tap(self, element, x=None, y=None):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Performs a double tap on the target element.
|
|
|
|
|
|
|
|
:param element: The element to double tap.
|
|
|
|
:param x: Optional, x-coordinate of double tap, relative to the
|
|
|
|
top-left corner of the element.
|
|
|
|
:param y: Optional, y-coordinate of double tap, relative to the
|
|
|
|
top-left corner of the element.
|
|
|
|
'''
|
2013-04-27 02:18:55 +04:00
|
|
|
element=element.id
|
|
|
|
self.action_chain.append(['press', element, x, y])
|
|
|
|
self.action_chain.append(['release'])
|
|
|
|
self.action_chain.append(['press', element, x, y])
|
|
|
|
self.action_chain.append(['release'])
|
|
|
|
return self
|
|
|
|
|
2015-02-28 04:30:07 +03:00
|
|
|
def click(self, element, button=MouseButton.LEFT, count=1):
|
2015-02-28 04:30:07 +03:00
|
|
|
'''
|
|
|
|
Performs a click with additional parameters to allow for double clicking,
|
|
|
|
right click, middle click, etc.
|
|
|
|
|
|
|
|
:param element: The element to click.
|
2015-02-28 04:30:07 +03:00
|
|
|
:param button: The mouse button to click (indexed from 0, left to right).
|
2015-02-28 04:30:07 +03:00
|
|
|
:param count: Optional, the count of clicks to synthesize (for double
|
|
|
|
click events).
|
|
|
|
'''
|
|
|
|
el = element.id
|
2015-02-28 04:30:07 +03:00
|
|
|
self.action_chain.append(['click', el, button, count])
|
2015-02-28 04:30:07 +03:00
|
|
|
return self
|
|
|
|
|
2015-02-28 04:30:07 +03:00
|
|
|
def context_click(self, element):
|
|
|
|
'''
|
|
|
|
Performs a context click on the specified element.
|
|
|
|
|
|
|
|
:param element: The element to context click.
|
|
|
|
'''
|
|
|
|
return self.click(element, button=MouseButton.RIGHT)
|
|
|
|
|
|
|
|
def middle_click(self, element):
|
|
|
|
'''
|
|
|
|
Performs a middle click on the specified element.
|
|
|
|
|
|
|
|
:param element: The element to middle click.
|
|
|
|
'''
|
|
|
|
return self.click(element, button=MouseButton.MIDDLE)
|
|
|
|
|
2015-02-28 04:30:07 +03:00
|
|
|
def double_click(self, element):
|
|
|
|
'''
|
|
|
|
Performs a double click on the specified element.
|
|
|
|
|
|
|
|
:param element: The element to double click.
|
|
|
|
'''
|
|
|
|
return self.click(element, count=2)
|
|
|
|
|
2013-04-26 22:14:50 +04:00
|
|
|
def flick(self, element, x1, y1, x2, y2, duration=200):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Performs a flick gesture on the target element.
|
|
|
|
|
|
|
|
:param element: The element to perform the flick gesture on.
|
|
|
|
:param x1: Starting x-coordinate of flick, relative to the top left
|
|
|
|
corner of the element.
|
|
|
|
:param y1: Starting y-coordinate of flick, relative to the top left
|
|
|
|
corner of the element.
|
2013-12-17 19:57:45 +04:00
|
|
|
:param x2: Ending x-coordinate of flick, relative to the top left
|
2013-08-20 21:59:20 +04:00
|
|
|
corner of the element.
|
2013-12-17 19:57:45 +04:00
|
|
|
:param y2: Ending y-coordinate of flick, relative to the top left
|
2013-08-20 21:59:20 +04:00
|
|
|
corner of the element.
|
|
|
|
:param duration: Time needed for the flick gesture for complete (in
|
|
|
|
milliseconds).
|
|
|
|
'''
|
2013-04-26 22:14:50 +04:00
|
|
|
element = element.id
|
2013-08-13 21:24:33 +04:00
|
|
|
elapsed = 0
|
2013-04-26 22:14:50 +04:00
|
|
|
time_increment = 10
|
|
|
|
if time_increment >= duration:
|
|
|
|
time_increment = duration
|
|
|
|
move_x = time_increment*1.0/duration * (x2 - x1)
|
|
|
|
move_y = time_increment*1.0/duration * (y2 - y1)
|
|
|
|
self.action_chain.append(['press', element, x1, y1])
|
2013-08-13 21:24:33 +04:00
|
|
|
while elapsed < duration:
|
|
|
|
elapsed += time_increment
|
2013-04-26 22:14:50 +04:00
|
|
|
self.action_chain.append(['moveByOffset', move_x, move_y])
|
|
|
|
self.action_chain.append(['wait', time_increment/1000])
|
|
|
|
self.action_chain.append(['release'])
|
|
|
|
return self
|
|
|
|
|
2014-06-08 04:05:00 +04:00
|
|
|
def long_press(self, element, time_in_seconds, x=None, y=None):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Performs a long press gesture on the target element.
|
|
|
|
|
|
|
|
:param element: The element to press.
|
|
|
|
:param time_in_seconds: Time in seconds to wait before releasing the press.
|
2014-06-08 04:05:00 +04:00
|
|
|
:param x: Optional, x-coordinate to tap, relative to the top-left
|
|
|
|
corner of the element.
|
|
|
|
:param y: Optional, y-coordinate to tap, relative to the top-left
|
|
|
|
corner of the element.
|
|
|
|
|
|
|
|
This is equivalent to calling:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
action.press(element, x, y).wait(time_in_seconds).release()
|
|
|
|
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
2013-04-16 00:19:22 +04:00
|
|
|
element = element.id
|
2014-06-08 04:05:00 +04:00
|
|
|
self.action_chain.append(['press', element, x, y])
|
2013-04-16 00:19:22 +04:00
|
|
|
self.action_chain.append(['wait', time_in_seconds])
|
|
|
|
self.action_chain.append(['release'])
|
|
|
|
return self
|
|
|
|
|
2015-02-11 00:55:33 +03:00
|
|
|
def key_down(self, key_code):
|
|
|
|
"""
|
|
|
|
Perform a "keyDown" action for the given key code. Modifier keys are
|
|
|
|
respected by the server for the course of an action chain.
|
|
|
|
|
|
|
|
:param key_code: The key to press as a result of this action.
|
|
|
|
"""
|
|
|
|
self.action_chain.append(['keyDown', key_code])
|
|
|
|
return self
|
|
|
|
|
|
|
|
def key_up(self, key_code):
|
|
|
|
"""
|
|
|
|
Perform a "keyUp" action for the given key code. Modifier keys are
|
|
|
|
respected by the server for the course of an action chain.
|
|
|
|
:param key_up: The key to release as a result of this action.
|
|
|
|
"""
|
|
|
|
self.action_chain.append(['keyUp', key_code])
|
|
|
|
return self
|
|
|
|
|
2013-03-05 05:09:58 +04:00
|
|
|
def perform(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Sends the action chain built so far to the server side for
|
|
|
|
execution and clears the current chain of actions."""
|
|
|
|
body = {"chain": self.action_chain, "nextId": self.current_id}
|
|
|
|
self.current_id = self.marionette._send_message("actionChain", body, key="value")
|
2013-03-22 22:29:02 +04:00
|
|
|
self.action_chain = []
|
|
|
|
return self
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-03-19 00:42:46 +04:00
|
|
|
class MultiActions(object):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
A MultiActions object represents a sequence of actions that may be
|
|
|
|
performed at the same time. Its intent is to allow the simulation
|
|
|
|
of multi-touch gestures.
|
|
|
|
Usage example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
# create multiaction object
|
|
|
|
multitouch = MultiActions(marionette)
|
|
|
|
# create several action objects
|
|
|
|
action_1 = Actions(marionette)
|
|
|
|
action_2 = Actions(marionette)
|
|
|
|
# add actions to each action object/finger
|
|
|
|
action_1.press(element1).move_to(element2).release()
|
|
|
|
action_2.press(element3).wait().release(element3)
|
|
|
|
# fire all the added events
|
|
|
|
multitouch.add(action_1).add(action_2).perform()
|
|
|
|
'''
|
|
|
|
|
2013-03-19 00:42:46 +04:00
|
|
|
def __init__(self, marionette):
|
|
|
|
self.multi_actions = []
|
|
|
|
self.max_length = 0
|
|
|
|
self.marionette = marionette
|
|
|
|
|
|
|
|
def add(self, action):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Adds a set of actions to perform.
|
|
|
|
|
|
|
|
:param action: An Actions object.
|
|
|
|
'''
|
2013-03-19 00:42:46 +04:00
|
|
|
self.multi_actions.append(action.action_chain)
|
|
|
|
if len(action.action_chain) > self.max_length:
|
|
|
|
self.max_length = len(action.action_chain)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def perform(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Perform all the actions added to this object."""
|
|
|
|
body = {"value": self.multi_actions, "max_length": self.max_length}
|
|
|
|
self.marionette._send_message("multiAction", body)
|
|
|
|
|
2013-03-19 00:42:46 +04:00
|
|
|
|
2015-02-11 04:44:52 +03:00
|
|
|
class Alert(object):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""A class for interacting with alerts.
|
2015-02-11 04:44:52 +03:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
Alert(marionette).accept()
|
|
|
|
Alert(merionette).dismiss()
|
|
|
|
"""
|
2015-02-11 04:44:52 +03:00
|
|
|
|
|
|
|
def __init__(self, marionette):
|
|
|
|
self.marionette = marionette
|
|
|
|
|
|
|
|
def accept(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Accept a currently displayed modal dialog."""
|
|
|
|
self.marionette._send_message("acceptDialog")
|
2015-02-11 04:44:52 +03:00
|
|
|
|
|
|
|
def dismiss(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Dismiss a currently displayed modal dialog."""
|
|
|
|
self.marionette._send_message("dismissDialog")
|
2015-02-11 04:44:52 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def text(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Return the currently displayed text in a tab modal."""
|
|
|
|
return self.marionette._send_message("getTextFromDialog", key="value")
|
2015-02-11 04:44:52 +03:00
|
|
|
|
|
|
|
def send_keys(self, *string):
|
|
|
|
"""Send keys to the currently displayed text input area in an open
|
2015-08-15 17:46:31 +03:00
|
|
|
tab modal dialog."""
|
|
|
|
body = {"value": Marionette.convert_keys(*string)}
|
|
|
|
self.marionette._send_message("sendKeysToDialog", body)
|
2015-02-11 04:44:52 +03:00
|
|
|
|
2014-02-28 01:36:00 +04:00
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
class Marionette(object):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Represents a Marionette connection to a browser or device."""
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-08-20 21:59:20 +04:00
|
|
|
CONTEXT_CHROME = 'chrome' # non-browser content: windows, dialogs, etc.
|
|
|
|
CONTEXT_CONTENT = 'content' # browser content: iframes, divs, etc.
|
2013-01-11 23:33:58 +04:00
|
|
|
TIMEOUT_SEARCH = 'implicit'
|
|
|
|
TIMEOUT_SCRIPT = 'script'
|
|
|
|
TIMEOUT_PAGE = 'page load'
|
2015-09-04 01:41:58 +03:00
|
|
|
DEFAULT_STARTUP_TIMEOUT = 60
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-08-07 19:46:25 +04:00
|
|
|
def __init__(self, host='localhost', port=2828, app=None, app_args=None, bin=None,
|
2015-07-18 11:53:10 +03:00
|
|
|
profile=None, addons=None, emulator=None, sdcard=None, emulator_img=None,
|
2014-06-19 22:17:26 +04:00
|
|
|
emulator_binary=None, emulator_res=None, connect_to_running_emulator=False,
|
|
|
|
gecko_log=None, homedir=None, baseurl=None, no_window=False, logdir=None,
|
2014-07-05 01:33:00 +04:00
|
|
|
busybox=None, symbols_path=None, timeout=None, socket_timeout=360,
|
2014-12-11 17:44:00 +03:00
|
|
|
device_serial=None, adb_path=None, process_args=None,
|
2015-09-04 01:41:58 +03:00
|
|
|
adb_host=None, adb_port=None, prefs=None, startup_timeout=None):
|
2012-03-26 23:40:25 +04:00
|
|
|
self.host = host
|
2014-11-12 22:39:59 +03:00
|
|
|
self.port = self.local_port = port
|
2012-05-23 14:10:54 +04:00
|
|
|
self.bin = bin
|
2014-12-04 01:08:04 +03:00
|
|
|
self.profile = profile
|
2015-07-18 11:53:10 +03:00
|
|
|
self.addons = addons
|
2012-08-07 02:08:45 +04:00
|
|
|
self.instance = None
|
2012-03-26 23:40:25 +04:00
|
|
|
self.session = None
|
2014-11-11 18:55:23 +03:00
|
|
|
self.session_id = None
|
2012-03-26 23:40:25 +04:00
|
|
|
self.window = None
|
2014-12-12 20:08:31 +03:00
|
|
|
self.chrome_window = None
|
2014-06-19 22:17:26 +04:00
|
|
|
self.runner = None
|
2012-03-26 23:40:25 +04:00
|
|
|
self.emulator = None
|
2012-05-10 00:39:19 +04:00
|
|
|
self.extra_emulators = []
|
2012-03-26 23:40:25 +04:00
|
|
|
self.baseurl = baseurl
|
2014-06-19 22:17:26 +04:00
|
|
|
self.no_window = no_window
|
2012-11-01 02:36:57 +04:00
|
|
|
self._test_name = None
|
2013-05-23 02:22:22 +04:00
|
|
|
self.timeout = timeout
|
2014-12-11 17:44:00 +03:00
|
|
|
self.socket_timeout = socket_timeout
|
2013-09-12 15:19:51 +04:00
|
|
|
self.device_serial = device_serial
|
2014-12-11 17:44:00 +03:00
|
|
|
self.adb_host = adb_host
|
|
|
|
self.adb_port = adb_port
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2015-09-04 01:41:58 +03:00
|
|
|
startup_timeout = startup_timeout or self.DEFAULT_STARTUP_TIMEOUT
|
|
|
|
|
2012-05-23 14:10:54 +04:00
|
|
|
if bin:
|
2014-11-12 22:39:59 +03:00
|
|
|
port = int(self.port)
|
2012-12-14 21:21:46 +04:00
|
|
|
if not Marionette.is_port_available(port, host=self.host):
|
|
|
|
ex_msg = "%s:%d is unavailable." % (self.host, port)
|
2014-06-19 22:17:26 +04:00
|
|
|
raise errors.MarionetteException(message=ex_msg)
|
2013-04-06 12:46:29 +04:00
|
|
|
if app:
|
|
|
|
# select instance class for the given app
|
|
|
|
try:
|
|
|
|
instance_class = geckoinstance.apps[app]
|
|
|
|
except KeyError:
|
|
|
|
msg = 'Application "%s" unknown (should be one of %s)'
|
|
|
|
raise NotImplementedError(msg % (app, geckoinstance.apps.keys()))
|
|
|
|
else:
|
2013-12-20 17:12:40 +04:00
|
|
|
try:
|
|
|
|
config = ConfigParser.RawConfigParser()
|
|
|
|
config.read(os.path.join(os.path.dirname(bin), 'application.ini'))
|
|
|
|
app = config.get('App', 'Name')
|
|
|
|
instance_class = geckoinstance.apps[app.lower()]
|
|
|
|
except (ConfigParser.NoOptionError,
|
|
|
|
ConfigParser.NoSectionError,
|
|
|
|
KeyError):
|
|
|
|
instance_class = geckoinstance.GeckoInstance
|
2014-11-12 22:39:59 +03:00
|
|
|
self.instance = instance_class(host=self.host, port=self.port,
|
2014-12-04 01:08:04 +03:00
|
|
|
bin=self.bin, profile=self.profile,
|
2014-05-17 14:32:00 +04:00
|
|
|
app_args=app_args, symbols_path=symbols_path,
|
2015-07-18 11:53:10 +03:00
|
|
|
gecko_log=gecko_log, prefs=prefs,
|
|
|
|
addons=self.addons)
|
2012-05-23 14:10:54 +04:00
|
|
|
self.instance.start()
|
2015-07-07 22:26:54 +03:00
|
|
|
assert(self.wait_for_port(timeout=startup_timeout)), "Timed out waiting for port!"
|
2012-10-25 18:09:19 +04:00
|
|
|
|
2014-11-12 22:39:59 +03:00
|
|
|
if emulator:
|
2014-06-19 22:17:26 +04:00
|
|
|
self.runner = B2GEmulatorRunner(b2g_home=homedir,
|
|
|
|
no_window=self.no_window,
|
|
|
|
logdir=logdir,
|
|
|
|
arch=emulator,
|
|
|
|
sdcard=sdcard,
|
|
|
|
symbols_path=symbols_path,
|
|
|
|
binary=emulator_binary,
|
|
|
|
userdata=emulator_img,
|
|
|
|
resolution=emulator_res,
|
2014-12-04 01:08:04 +03:00
|
|
|
profile=self.profile,
|
2015-07-18 11:53:10 +03:00
|
|
|
addons=self.addons,
|
2014-07-17 11:02:00 +04:00
|
|
|
adb_path=adb_path,
|
|
|
|
process_args=process_args)
|
2014-06-19 22:17:26 +04:00
|
|
|
self.emulator = self.runner.device
|
2012-03-26 23:40:25 +04:00
|
|
|
self.emulator.start()
|
2014-11-12 22:39:59 +03:00
|
|
|
self.port = self.emulator.setup_port_forwarding(remote_port=self.port)
|
2014-06-19 22:17:26 +04:00
|
|
|
assert(self.emulator.wait_for_port(self.port)), "Timed out waiting for port!"
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-11-12 22:39:59 +03:00
|
|
|
if connect_to_running_emulator:
|
2014-06-19 22:17:26 +04:00
|
|
|
self.runner = B2GEmulatorRunner(b2g_home=homedir,
|
2014-07-17 11:02:00 +04:00
|
|
|
logdir=logdir,
|
|
|
|
process_args=process_args)
|
2014-06-19 22:17:26 +04:00
|
|
|
self.emulator = self.runner.device
|
2012-03-26 23:40:25 +04:00
|
|
|
self.emulator.connect()
|
2014-11-12 22:39:59 +03:00
|
|
|
self.port = self.emulator.setup_port_forwarding(remote_port=self.port)
|
2014-06-19 22:17:26 +04:00
|
|
|
assert(self.emulator.wait_for_port(self.port)), "Timed out waiting for port!"
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-10-19 00:19:21 +04:00
|
|
|
if emulator:
|
2014-06-19 22:17:26 +04:00
|
|
|
if busybox:
|
|
|
|
self.emulator.install_busybox(busybox=busybox)
|
|
|
|
self.emulator.wait_for_system_message(self)
|
2012-10-19 00:19:21 +04:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
# for callbacks from a protocol level 2 or lower remote,
|
|
|
|
# we store the callback ID so it can be used by _send_emulator_result
|
|
|
|
self.emulator_callback_id = None
|
|
|
|
|
2014-01-15 23:58:54 +04:00
|
|
|
def cleanup(self):
|
2014-01-22 19:44:26 +04:00
|
|
|
if self.session:
|
2014-01-31 00:14:04 +04:00
|
|
|
try:
|
|
|
|
self.delete_session()
|
2014-06-19 22:17:26 +04:00
|
|
|
except (errors.MarionetteException, socket.error, IOError):
|
2014-01-31 00:14:04 +04:00
|
|
|
# These exceptions get thrown if the Marionette server
|
|
|
|
# hit an exception/died or the connection died. We can
|
|
|
|
# do no further server-side cleanup in this case.
|
|
|
|
pass
|
|
|
|
self.session = None
|
2014-06-19 22:17:26 +04:00
|
|
|
if self.runner:
|
|
|
|
self.runner.cleanup()
|
2012-09-20 22:13:42 +04:00
|
|
|
if self.instance:
|
2012-05-23 14:10:54 +04:00
|
|
|
self.instance.close()
|
2012-05-10 00:39:19 +04:00
|
|
|
for qemu in self.extra_emulators:
|
|
|
|
qemu.emulator.close()
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-01-15 23:58:54 +04:00
|
|
|
def __del__(self):
|
|
|
|
self.cleanup()
|
|
|
|
|
2012-12-14 21:21:46 +04:00
|
|
|
@staticmethod
|
|
|
|
def is_port_available(port, host=''):
|
|
|
|
port = int(port)
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2015-07-12 16:32:00 +03:00
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2012-12-14 21:21:46 +04:00
|
|
|
try:
|
|
|
|
s.bind((host, port))
|
|
|
|
return True
|
|
|
|
except socket.error:
|
|
|
|
return False
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
2013-11-25 23:05:43 +04:00
|
|
|
def wait_for_port(self, timeout=60):
|
2015-10-09 13:15:24 +03:00
|
|
|
return transport.wait_for_port(self.host, self.port, timeout=timeout)
|
2013-01-04 22:41:34 +04:00
|
|
|
|
2014-02-28 01:36:00 +04:00
|
|
|
@do_crash_check
|
2015-10-09 13:15:24 +03:00
|
|
|
def _send_message(self, name, params=None, key=None):
|
|
|
|
if not self.session_id and name != "newSession":
|
2014-06-19 22:17:26 +04:00
|
|
|
raise errors.MarionetteException("Please start a session")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
try:
|
|
|
|
if self.protocol < 3:
|
|
|
|
data = {"name": name}
|
|
|
|
if params:
|
|
|
|
data["parameters"] = params
|
|
|
|
self.client.send(data)
|
|
|
|
msg = self.client.receive()
|
2015-08-15 17:46:31 +03:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
else:
|
|
|
|
msg = self.client.request(name, params)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2015-07-13 22:29:21 +03:00
|
|
|
except IOError:
|
|
|
|
if self.instance and not hasattr(self.instance, 'detached'):
|
|
|
|
# If we've launched the binary we've connected to, wait
|
|
|
|
# for it to shut down.
|
2015-09-25 17:33:11 +03:00
|
|
|
returncode = self.instance.runner.wait(timeout=self.DEFAULT_STARTUP_TIMEOUT)
|
|
|
|
raise IOError("process died with returncode %s" % returncode)
|
2015-07-13 22:29:21 +03:00
|
|
|
raise
|
2014-02-28 01:36:00 +04:00
|
|
|
except socket.timeout:
|
2012-03-26 23:40:25 +04:00
|
|
|
self.session = None
|
|
|
|
self.window = None
|
|
|
|
self.client.close()
|
2015-04-02 22:07:20 +03:00
|
|
|
raise errors.TimeoutException("Connection timed out")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
if isinstance(msg, transport.Command):
|
|
|
|
if msg.name == "runEmulatorCmd":
|
|
|
|
self.emulator_callback_id = msg.params.get("id")
|
|
|
|
msg = self._emulator_cmd(msg.params["emulator_cmd"])
|
|
|
|
elif msg.name == "runEmulatorShell":
|
|
|
|
self.emulator_callback_id = msg.params.get("id")
|
|
|
|
msg = self._emulator_shell(msg.params["emulator_shell"])
|
|
|
|
else:
|
|
|
|
raise IOError("Unknown command: %s" % msg)
|
2015-08-15 17:46:31 +03:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
res, err = msg.result, msg.error
|
|
|
|
if err:
|
|
|
|
self._handle_error(err)
|
2015-08-15 17:46:31 +03:00
|
|
|
|
|
|
|
if key is not None:
|
2015-10-09 13:15:24 +03:00
|
|
|
return self._unwrap_response(res.get(key))
|
2015-09-12 01:55:59 +03:00
|
|
|
else:
|
2015-10-09 13:15:24 +03:00
|
|
|
return self._unwrap_response(res)
|
2015-09-12 01:55:59 +03:00
|
|
|
|
|
|
|
def _unwrap_response(self, value):
|
|
|
|
if isinstance(value, dict) and \
|
2015-09-15 22:45:39 +03:00
|
|
|
(WEBELEMENT_KEY in value or W3C_WEBELEMENT_KEY in value):
|
|
|
|
if value.get(WEBELEMENT_KEY):
|
|
|
|
return HTMLElement(self, value.get(WEBELEMENT_KEY))
|
2015-09-12 01:55:59 +03:00
|
|
|
else:
|
2015-09-15 22:45:39 +03:00
|
|
|
return HTMLElement(self, value.get(W3C_WEBELEMENT_KEY))
|
2015-09-12 01:55:59 +03:00
|
|
|
elif isinstance(value, list):
|
|
|
|
return list(self._unwrap_response(item) for item in value)
|
|
|
|
else:
|
|
|
|
return value
|
2015-08-15 17:46:31 +03:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
def _emulator_cmd(self, cmd):
|
2015-08-15 17:46:31 +03:00
|
|
|
if not self.emulator:
|
2014-06-19 22:17:26 +04:00
|
|
|
raise errors.MarionetteException(
|
2014-02-07 19:21:49 +04:00
|
|
|
"No emulator in this test to run command against")
|
2015-08-15 17:46:31 +03:00
|
|
|
payload = cmd.encode("ascii")
|
|
|
|
result = self.emulator._run_telnet(payload)
|
2015-10-09 13:15:24 +03:00
|
|
|
return self._send_emulator_result(result)
|
2012-05-19 00:30:43 +04:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
def _emulator_shell(self, args):
|
2015-08-21 05:41:55 +03:00
|
|
|
if not isinstance(args, list) or not self.emulator:
|
2014-06-19 22:17:26 +04:00
|
|
|
raise errors.MarionetteException(
|
2014-03-20 09:01:25 +04:00
|
|
|
"No emulator in this test to run shell command against")
|
2014-06-19 22:17:26 +04:00
|
|
|
buf = StringIO.StringIO()
|
|
|
|
self.emulator.dm.shell(args, buf)
|
|
|
|
result = str(buf.getvalue()[0:-1]).rstrip().splitlines()
|
|
|
|
buf.close()
|
2015-10-09 13:15:24 +03:00
|
|
|
return self._send_emulator_result(result)
|
|
|
|
|
|
|
|
def _send_emulator_result(self, result):
|
|
|
|
if self.protocol < 3:
|
|
|
|
body = {"name": "emulatorCmdResult",
|
|
|
|
"id": self.emulator_callback_id,
|
|
|
|
"result": result}
|
|
|
|
self.client.send(body)
|
|
|
|
return self.client.receive()
|
|
|
|
else:
|
|
|
|
return self.client.respond(result)
|
2014-03-20 09:01:25 +04:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
def _handle_error(self, obj):
|
2015-08-15 17:46:31 +03:00
|
|
|
if self.protocol == 1:
|
2015-10-09 13:15:24 +03:00
|
|
|
if "error" not in obj or not isinstance(obj["error"], dict):
|
2015-08-15 17:46:31 +03:00
|
|
|
raise errors.MarionetteException(
|
2015-10-09 13:15:24 +03:00
|
|
|
"Malformed packet, expected key 'error' to be a dict: %s" % obj)
|
|
|
|
error = obj["error"].get("status")
|
|
|
|
message = obj["error"].get("message")
|
|
|
|
stacktrace = obj["error"].get("stacktrace")
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
else:
|
2015-10-09 13:15:24 +03:00
|
|
|
error = obj["error"]
|
|
|
|
message = obj["message"]
|
|
|
|
stacktrace = obj["stacktrace"]
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
raise errors.lookup(error)(message, stacktrace=stacktrace)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-07-23 01:27:25 +04:00
|
|
|
def _reset_timeouts(self):
|
|
|
|
if self.timeout is not None:
|
|
|
|
self.timeouts(self.TIMEOUT_SEARCH, self.timeout)
|
|
|
|
self.timeouts(self.TIMEOUT_SCRIPT, self.timeout)
|
|
|
|
self.timeouts(self.TIMEOUT_PAGE, self.timeout)
|
|
|
|
else:
|
|
|
|
self.timeouts(self.TIMEOUT_PAGE, 30000)
|
|
|
|
|
2012-10-23 02:00:54 +04:00
|
|
|
def check_for_crash(self):
|
|
|
|
returncode = None
|
|
|
|
name = None
|
2013-03-26 17:50:00 +04:00
|
|
|
crashed = False
|
2014-06-19 22:17:26 +04:00
|
|
|
if self.runner:
|
2014-11-12 22:39:59 +03:00
|
|
|
if self.runner.check_for_crashes(test_name=self.test_name):
|
2012-10-23 02:00:54 +04:00
|
|
|
returncode = self.emulator.proc.returncode
|
|
|
|
name = 'emulator'
|
2014-11-12 22:39:59 +03:00
|
|
|
crashed = True
|
2012-10-23 02:00:54 +04:00
|
|
|
elif self.instance:
|
2014-11-12 22:39:59 +03:00
|
|
|
if self.instance.runner.check_for_crashes(
|
|
|
|
test_name=self.test_name):
|
|
|
|
crashed = True
|
2012-10-23 02:00:54 +04:00
|
|
|
if returncode is not None:
|
2014-11-12 22:39:59 +03:00
|
|
|
print ('PROCESS-CRASH | %s | abnormal termination with exit code %d' %
|
|
|
|
(name, returncode))
|
2013-03-26 17:50:00 +04:00
|
|
|
return crashed
|
2012-10-23 02:00:54 +04:00
|
|
|
|
2015-02-11 04:44:52 +03:00
|
|
|
@staticmethod
|
|
|
|
def convert_keys(*string):
|
|
|
|
typing = []
|
|
|
|
for val in string:
|
|
|
|
if isinstance(val, Keys):
|
|
|
|
typing.append(val)
|
|
|
|
elif isinstance(val, int):
|
|
|
|
val = str(val)
|
|
|
|
for i in range(len(val)):
|
|
|
|
typing.append(val[i])
|
|
|
|
else:
|
|
|
|
for i in range(len(val)):
|
|
|
|
typing.append(val[i])
|
|
|
|
return typing
|
|
|
|
|
2015-05-14 22:08:17 +03:00
|
|
|
def get_permission(self, perm):
|
|
|
|
with self.using_context('content'):
|
|
|
|
value = self.execute_script("""
|
|
|
|
let value = {
|
|
|
|
'url': document.nodePrincipal.URI.spec,
|
|
|
|
'appId': document.nodePrincipal.appId,
|
|
|
|
'isInBrowserElement': document.nodePrincipal.isInBrowserElement,
|
|
|
|
'type': arguments[0]
|
|
|
|
};
|
|
|
|
return value;
|
|
|
|
""", script_args=[perm], sandbox='system')
|
|
|
|
|
|
|
|
with self.using_context('chrome'):
|
|
|
|
permission = self.execute_script("""
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
let perm = arguments[0];
|
|
|
|
let secMan = Services.scriptSecurityManager;
|
2015-08-17 12:03:19 +03:00
|
|
|
let attrs = {appId: perm.appId, inBrowser: perm.isInBrowserElement};
|
|
|
|
let principal = secMan.createCodebasePrincipal(
|
2015-05-14 22:08:17 +03:00
|
|
|
Services.io.newURI(perm.url, null, null),
|
2015-08-17 12:03:19 +03:00
|
|
|
attrs);
|
2015-05-14 22:08:17 +03:00
|
|
|
let testPerm = Services.perms.testPermissionFromPrincipal(
|
|
|
|
principal, perm.type);
|
|
|
|
return testPerm;
|
|
|
|
""", script_args=[value])
|
|
|
|
return permission
|
|
|
|
|
|
|
|
def push_permission(self, perm, allow):
|
2015-05-04 19:49:08 +03:00
|
|
|
with self.using_context('content'):
|
|
|
|
perm = self.execute_script("""
|
|
|
|
let allow = arguments[0];
|
2015-05-14 22:08:17 +03:00
|
|
|
if (typeof(allow) == "boolean") {
|
|
|
|
if (allow) {
|
|
|
|
allow = Components.interfaces.nsIPermissionManager.ALLOW_ACTION;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
allow = Components.interfaces.nsIPermissionManager.DENY_ACTION;
|
|
|
|
}
|
2015-04-23 23:39:38 +03:00
|
|
|
}
|
2015-05-04 19:49:08 +03:00
|
|
|
let perm_type = arguments[1];
|
|
|
|
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
window.wrappedJSObject.permChanged = false;
|
|
|
|
window.wrappedJSObject.permObserver = function(subject, topic, data) {
|
|
|
|
if (topic == "perm-changed") {
|
|
|
|
let permission = subject.QueryInterface(Components.interfaces.nsIPermission);
|
|
|
|
if (perm_type == permission.type) {
|
|
|
|
Services.obs.removeObserver(window.wrappedJSObject.permObserver, "perm-changed");
|
|
|
|
window.wrappedJSObject.permChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Services.obs.addObserver(window.wrappedJSObject.permObserver,
|
|
|
|
"perm-changed", false);
|
|
|
|
|
|
|
|
let value = {
|
|
|
|
'url': document.nodePrincipal.URI.spec,
|
|
|
|
'appId': document.nodePrincipal.appId,
|
|
|
|
'isInBrowserElement': document.nodePrincipal.isInBrowserElement,
|
|
|
|
'type': perm_type,
|
|
|
|
'action': allow
|
|
|
|
};
|
|
|
|
return value;
|
2015-05-14 22:08:17 +03:00
|
|
|
""", script_args=[allow, perm], sandbox='system')
|
|
|
|
|
|
|
|
current_perm = self.get_permission(perm['type'])
|
|
|
|
if current_perm == perm['action']:
|
|
|
|
with self.using_context('content'):
|
|
|
|
self.execute_script("""
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
Services.obs.removeObserver(window.wrappedJSObject.permObserver, "perm-changed");
|
|
|
|
""", sandbox='system')
|
|
|
|
return
|
2015-05-04 19:49:08 +03:00
|
|
|
|
|
|
|
with self.using_context('chrome'):
|
2015-05-14 22:08:17 +03:00
|
|
|
self.execute_script("""
|
2015-05-04 19:49:08 +03:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
let perm = arguments[0];
|
|
|
|
let secMan = Services.scriptSecurityManager;
|
2015-08-17 12:03:19 +03:00
|
|
|
let attrs = {appId: perm.appId, inBrowser: perm.isInBrowserElement};
|
|
|
|
let principal = secMan.createCodebasePrincipal(Services.io.newURI(perm.url, null, null),
|
|
|
|
attrs);
|
2015-05-04 19:49:08 +03:00
|
|
|
Services.perms.addFromPrincipal(principal, perm.type, perm.action);
|
|
|
|
return true;
|
|
|
|
""", script_args=[perm])
|
|
|
|
|
|
|
|
with self.using_context('content'):
|
2015-05-14 22:08:17 +03:00
|
|
|
self.execute_async_script("""
|
|
|
|
waitFor(marionetteScriptFinished, function() {
|
|
|
|
return window.wrappedJSObject.permChanged;
|
|
|
|
});
|
|
|
|
""", sandbox='system')
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def using_permissions(self, perms):
|
|
|
|
'''
|
|
|
|
Sets permissions for code being executed in a `with` block,
|
|
|
|
and restores them on exit.
|
|
|
|
|
|
|
|
:param perms: A dict containing one or more perms and their
|
|
|
|
values to be set.
|
|
|
|
|
|
|
|
Usage example::
|
|
|
|
|
|
|
|
with marionette.using_permissions({'systemXHR': True}):
|
|
|
|
... do stuff ...
|
|
|
|
'''
|
|
|
|
original_perms = {}
|
|
|
|
for perm in perms:
|
|
|
|
original_perms[perm] = self.get_permission(perm)
|
|
|
|
self.push_permission(perm, perms[perm])
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
for perm in original_perms:
|
|
|
|
self.push_permission(perm, original_perms[perm])
|
2015-05-04 19:49:08 +03:00
|
|
|
|
2015-09-30 11:26:58 +03:00
|
|
|
def get_pref(self, pref):
|
|
|
|
'''Gets the preference value.
|
|
|
|
|
|
|
|
:param pref: Name of the preference.
|
|
|
|
|
|
|
|
Usage example::
|
|
|
|
|
|
|
|
marionette.get_pref('browser.tabs.warnOnClose')
|
|
|
|
|
|
|
|
'''
|
|
|
|
with self.using_context(self.CONTEXT_CONTENT):
|
|
|
|
pref_value = self.execute_script("""
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
let pref = arguments[0];
|
|
|
|
let type = Services.prefs.getPrefType(pref);
|
|
|
|
switch (type) {
|
|
|
|
case Services.prefs.PREF_STRING:
|
|
|
|
return Services.prefs.getCharPref(pref);
|
|
|
|
case Services.prefs.PREF_INT:
|
|
|
|
return Services.prefs.getIntPref(pref);
|
|
|
|
case Services.prefs.PREF_BOOL:
|
|
|
|
return Services.prefs.getBoolPref(pref);
|
|
|
|
case Services.prefs.PREF_INVALID:
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
""", script_args=[pref], sandbox='system')
|
|
|
|
return pref_value
|
|
|
|
|
|
|
|
def clear_pref(self, pref):
|
|
|
|
with self.using_context(self.CONTEXT_CHROME):
|
|
|
|
self.execute_script("""
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
let pref = arguments[0];
|
|
|
|
Services.prefs.clearUserPref(pref);
|
|
|
|
""", script_args=[pref])
|
|
|
|
|
|
|
|
def set_pref(self, pref, value):
|
|
|
|
with self.using_context(self.CONTEXT_CHROME):
|
|
|
|
if value is None:
|
|
|
|
self.clear_pref(pref)
|
|
|
|
return
|
|
|
|
|
|
|
|
if isinstance(value, bool):
|
|
|
|
func = 'setBoolPref'
|
|
|
|
elif isinstance(value, (int, long)):
|
|
|
|
func = 'setIntPref'
|
|
|
|
elif isinstance(value, basestring):
|
|
|
|
func = 'setCharPref'
|
|
|
|
else:
|
|
|
|
raise errors.MarionetteException(
|
|
|
|
"Unsupported preference type: %s" % type(value))
|
|
|
|
|
|
|
|
self.execute_script("""
|
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
|
|
|
let pref = arguments[0];
|
|
|
|
let value = arguments[1];
|
|
|
|
Services.prefs.%s(pref, value);
|
|
|
|
""" % func, script_args=[pref, value])
|
|
|
|
|
|
|
|
def set_prefs(self, prefs):
|
|
|
|
'''Sets preferences.
|
|
|
|
|
|
|
|
If the value of the preference to be set is None, reset the preference
|
|
|
|
to its default value. If no default value exists, the preference will
|
|
|
|
cease to exist.
|
|
|
|
|
|
|
|
:param prefs: A dict containing one or more preferences and their values
|
|
|
|
to be set.
|
|
|
|
|
|
|
|
Usage example::
|
|
|
|
|
|
|
|
marionette.set_prefs({'browser.tabs.warnOnClose': True})
|
|
|
|
|
|
|
|
'''
|
|
|
|
for pref, value in prefs.items():
|
|
|
|
self.set_pref(pref, value)
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def using_prefs(self, prefs):
|
|
|
|
'''Sets preferences for code being executed in a `with` block,
|
|
|
|
and restores them on exit.
|
|
|
|
|
|
|
|
:param prefs: A dict containing one or more preferences and their values
|
|
|
|
to be set.
|
|
|
|
|
|
|
|
Usage example::
|
|
|
|
|
|
|
|
with marionette.using_prefs({'browser.tabs.warnOnClose': True}):
|
|
|
|
# ... do stuff ...
|
|
|
|
|
|
|
|
'''
|
|
|
|
original_prefs = {p: self.get_pref(p) for p in prefs}
|
|
|
|
self.set_prefs(prefs)
|
|
|
|
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self.set_prefs(original_prefs)
|
|
|
|
|
2014-07-23 01:27:25 +04:00
|
|
|
def enforce_gecko_prefs(self, prefs):
|
|
|
|
"""
|
|
|
|
Checks if the running instance has the given prefs. If not, it will kill the
|
|
|
|
currently running instance, and spawn a new instance with the requested preferences.
|
|
|
|
|
|
|
|
: param prefs: A dictionary whose keys are preference names.
|
|
|
|
"""
|
|
|
|
if not self.instance:
|
|
|
|
raise errors.MarionetteException("enforce_gecko_prefs can only be called " \
|
|
|
|
"on gecko instances launched by Marionette")
|
|
|
|
pref_exists = True
|
|
|
|
self.set_context(self.CONTEXT_CHROME)
|
|
|
|
for pref, value in prefs.iteritems():
|
|
|
|
if type(value) is not str:
|
|
|
|
value = json.dumps(value)
|
|
|
|
pref_exists = self.execute_script("""
|
|
|
|
let prefInterface = Components.classes["@mozilla.org/preferences-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIPrefBranch);
|
|
|
|
let pref = '%s';
|
|
|
|
let value = '%s';
|
|
|
|
let type = prefInterface.getPrefType(pref);
|
|
|
|
switch(type) {
|
|
|
|
case prefInterface.PREF_STRING:
|
|
|
|
return value == prefInterface.getCharPref(pref).toString();
|
|
|
|
case prefInterface.PREF_BOOL:
|
|
|
|
return value == prefInterface.getBoolPref(pref).toString();
|
|
|
|
case prefInterface.PREF_INT:
|
|
|
|
return value == prefInterface.getIntPref(pref).toString();
|
|
|
|
case prefInterface.PREF_INVALID:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
""" % (pref, value))
|
|
|
|
if not pref_exists:
|
|
|
|
break
|
|
|
|
self.set_context(self.CONTEXT_CONTENT)
|
|
|
|
if not pref_exists:
|
|
|
|
self.delete_session()
|
|
|
|
self.instance.restart(prefs)
|
|
|
|
assert(self.wait_for_port()), "Timed out waiting for port!"
|
|
|
|
self.start_session()
|
|
|
|
self._reset_timeouts()
|
|
|
|
|
2015-03-07 04:39:26 +03:00
|
|
|
def restart(self, clean=False, in_app=False):
|
2014-07-23 01:27:25 +04:00
|
|
|
"""
|
|
|
|
This will terminate the currently running instance, and spawn a new instance
|
2014-12-04 01:08:04 +03:00
|
|
|
with the same profile and then reuse the session id when creating a session again.
|
2014-07-23 01:27:25 +04:00
|
|
|
|
2015-03-07 04:39:26 +03:00
|
|
|
: param clean: If False the same profile will be used after the restart. Note
|
|
|
|
that the in app initiated restart always maintains the same
|
|
|
|
profile.
|
|
|
|
: param in_app: If True, marionette will cause a restart from within the
|
|
|
|
browser. Otherwise the browser will be restarted immediately
|
|
|
|
by killing the process.
|
2014-07-23 01:27:25 +04:00
|
|
|
"""
|
|
|
|
if not self.instance:
|
2014-12-04 01:08:04 +03:00
|
|
|
raise errors.MarionetteException("restart can only be called " \
|
2014-07-23 01:27:25 +04:00
|
|
|
"on gecko instances launched by Marionette")
|
2015-03-07 04:39:26 +03:00
|
|
|
|
|
|
|
if in_app:
|
|
|
|
if clean:
|
|
|
|
raise ValueError
|
|
|
|
# Values here correspond to constants in nsIAppStartup.
|
|
|
|
# See https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIAppStartup
|
|
|
|
restart_flags = [
|
|
|
|
"eForceQuit",
|
|
|
|
"eRestart",
|
|
|
|
]
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("quitApplication", {"flags": restart_flags})
|
2015-03-11 20:17:33 +03:00
|
|
|
self.client.close()
|
2015-06-19 00:26:53 +03:00
|
|
|
# The instance is restarting itself; we will no longer be able to
|
|
|
|
# track it by pid, so mark it as 'detached'.
|
|
|
|
self.instance.detached = True
|
2015-03-07 04:39:26 +03:00
|
|
|
else:
|
|
|
|
self.delete_session()
|
|
|
|
self.instance.restart(clean=clean)
|
2014-07-23 01:27:25 +04:00
|
|
|
assert(self.wait_for_port()), "Timed out waiting for port!"
|
2014-12-04 01:08:04 +03:00
|
|
|
self.start_session(session_id=self.session_id)
|
2014-07-23 01:27:25 +04:00
|
|
|
self._reset_timeouts()
|
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def absolute_url(self, relative_url):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
Returns an absolute url for files served from Marionette's www directory.
|
|
|
|
|
|
|
|
:param relative_url: The url of a static file, relative to Marionette's www directory.
|
|
|
|
'''
|
2012-03-26 23:40:25 +04:00
|
|
|
return "%s%s" % (self.baseurl, relative_url)
|
|
|
|
|
2014-11-18 16:31:34 +03:00
|
|
|
def start_session(self, desired_capabilities=None, session_id=None, timeout=60):
|
2014-02-07 19:21:49 +04:00
|
|
|
"""Create a new Marionette session.
|
|
|
|
|
|
|
|
This method must be called before performing any other action.
|
|
|
|
|
2014-11-18 16:31:34 +03:00
|
|
|
:param desired_capabilities: An optional dict of desired
|
2014-02-07 19:21:49 +04:00
|
|
|
capabilities. This is currently ignored.
|
2014-11-18 16:31:34 +03:00
|
|
|
:param timeout: Timeout in seconds for the server to be ready.
|
2015-01-05 15:05:00 +03:00
|
|
|
:param session_id: unique identifier for the session. If no session id is
|
|
|
|
passed in then one will be generated by the marionette server.
|
2014-02-07 19:21:49 +04:00
|
|
|
|
2014-07-25 11:03:00 +04:00
|
|
|
:returns: A dict of the capabilities offered."""
|
2015-06-19 00:26:53 +03:00
|
|
|
if self.instance:
|
|
|
|
returncode = self.instance.runner.process_handler.proc.returncode
|
|
|
|
if returncode is not None:
|
|
|
|
# We're managing a binary which has terminated, so restart it.
|
|
|
|
self.instance.restart()
|
2015-08-15 17:46:31 +03:00
|
|
|
|
2015-10-09 13:15:24 +03:00
|
|
|
self.client = transport.TcpTransport(
|
|
|
|
self.host,
|
|
|
|
self.port,
|
|
|
|
self.socket_timeout)
|
2015-08-15 17:46:31 +03:00
|
|
|
self.protocol, _ = self.client.connect()
|
2014-11-18 16:31:34 +03:00
|
|
|
self.wait_for_port(timeout=timeout)
|
2015-08-15 17:46:31 +03:00
|
|
|
|
|
|
|
body = {"capabilities": desired_capabilities, "sessionId": session_id}
|
|
|
|
resp = self._send_message("newSession", body)
|
|
|
|
|
|
|
|
self.session_id = resp["sessionId"]
|
|
|
|
self.session = resp["value"] if self.protocol == 1 else resp["capabilities"]
|
|
|
|
self.b2g = "b2g" in self.session
|
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
return self.session
|
|
|
|
|
2012-11-01 02:36:57 +04:00
|
|
|
@property
|
|
|
|
def test_name(self):
|
|
|
|
return self._test_name
|
|
|
|
|
|
|
|
@test_name.setter
|
|
|
|
def test_name(self, test_name):
|
2015-08-15 17:46:31 +03:00
|
|
|
if self._send_message("setTestName", {"value": test_name}):
|
2012-11-01 02:36:57 +04:00
|
|
|
self._test_name = test_name
|
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def delete_session(self):
|
2014-07-25 11:03:00 +04:00
|
|
|
"""Close the current session and disconnect from the server."""
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("deleteSession")
|
2014-11-11 18:55:23 +03:00
|
|
|
self.session_id = None
|
2012-03-26 23:40:25 +04:00
|
|
|
self.session = None
|
|
|
|
self.window = None
|
|
|
|
self.client.close()
|
|
|
|
|
2012-08-10 22:51:35 +04:00
|
|
|
@property
|
|
|
|
def session_capabilities(self):
|
2013-08-20 21:59:20 +04:00
|
|
|
'''
|
|
|
|
A JSON dictionary representing the capabilities of the current session.
|
|
|
|
'''
|
2015-02-16 18:57:10 +03:00
|
|
|
return self.session
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def set_script_timeout(self, timeout):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Sets the maximum number of ms that an asynchronous script is
|
|
|
|
allowed to run.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
If a script does not return in the specified amount of time,
|
|
|
|
a ScriptTimeoutException is raised.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param timeout: The maximum number of milliseconds an asynchronous
|
2015-08-15 17:46:31 +03:00
|
|
|
script can run without causing an ScriptTimeoutException to
|
|
|
|
be raised
|
|
|
|
"""
|
|
|
|
self._send_message("setScriptTimeout", {"ms": timeout})
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def set_search_timeout(self, timeout):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Sets a timeout for the find methods.
|
|
|
|
|
|
|
|
When searching for an element using
|
|
|
|
either :class:`Marionette.find_element` or
|
|
|
|
:class:`Marionette.find_elements`, the method will continue
|
|
|
|
trying to locate the element for up to timeout ms. This can be
|
|
|
|
useful if, for example, the element you're looking for might
|
|
|
|
not exist immediately, because it belongs to a page which is
|
2013-08-20 21:59:20 +04:00
|
|
|
currently being loaded.
|
|
|
|
|
|
|
|
:param timeout: Timeout in milliseconds.
|
2015-08-15 17:46:31 +03:00
|
|
|
"""
|
|
|
|
self._send_message("setSearchTimeout", {"ms": timeout})
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-07-11 23:31:07 +04:00
|
|
|
@property
|
|
|
|
def current_window_handle(self):
|
2014-01-21 20:40:20 +04:00
|
|
|
"""Get the current window's handle.
|
|
|
|
|
2014-12-12 20:08:31 +03:00
|
|
|
Returns an opaque server-assigned identifier to this window
|
2014-01-21 20:40:20 +04:00
|
|
|
that uniquely identifies it within this Marionette instance.
|
|
|
|
This can be used to switch to this window at a later point.
|
|
|
|
|
|
|
|
:returns: unique window handle
|
|
|
|
:rtype: string
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self.window = self._send_message("getWindowHandle", key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
return self.window
|
2012-09-28 00:47:17 +04:00
|
|
|
|
2014-12-12 20:08:31 +03:00
|
|
|
@property
|
2015-01-09 03:13:30 +03:00
|
|
|
def current_chrome_window_handle(self):
|
2014-12-12 20:08:31 +03:00
|
|
|
"""Get the current chrome window's handle. Corresponds to
|
|
|
|
a chrome window that may itself contain tabs identified by
|
|
|
|
window_handles.
|
|
|
|
|
|
|
|
Returns an opaque server-assigned identifier to this window
|
|
|
|
that uniquely identifies it within this Marionette instance.
|
|
|
|
This can be used to switch to this window at a later point.
|
|
|
|
|
|
|
|
:returns: unique window handle
|
|
|
|
:rtype: string
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self.chrome_window = self._send_message(
|
|
|
|
"getCurrentChromeWindowHandle", key="value")
|
2014-12-12 20:08:31 +03:00
|
|
|
return self.chrome_window
|
|
|
|
|
2014-09-15 13:48:56 +04:00
|
|
|
def get_window_position(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Get the current window's position.
|
|
|
|
|
|
|
|
:returns: a dictionary with x and y
|
2014-09-15 13:48:56 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message(
|
|
|
|
"getWindowPosition", key="value" if self.protocol == 1 else None)
|
2014-09-15 13:48:56 +04:00
|
|
|
|
|
|
|
def set_window_position(self, x, y):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Set the position of the current window
|
|
|
|
|
|
|
|
:param x: x coordinate for the top left of the window
|
|
|
|
:param y: y coordinate for the top left of the window
|
2014-09-15 13:48:56 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("setWindowPosition", {"x": x, "y": y})
|
2014-09-15 13:48:56 +04:00
|
|
|
|
2012-07-11 00:30:21 +04:00
|
|
|
@property
|
|
|
|
def title(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Current title of the active window."""
|
|
|
|
return self._send_message("getTitle", key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2012-07-11 23:31:07 +04:00
|
|
|
@property
|
|
|
|
def window_handles(self):
|
2014-01-24 17:30:10 +04:00
|
|
|
"""Get list of windows in the current context.
|
|
|
|
|
|
|
|
If called in the content context it will return a list of
|
|
|
|
references to all available browser windows. Called in the
|
|
|
|
chrome context, it will list all available windows, not just
|
|
|
|
browser windows (e.g. not just navigator.browser).
|
|
|
|
|
|
|
|
Each window handle is assigned by the server, and the list of
|
|
|
|
strings returned does not have a guaranteed ordering.
|
|
|
|
|
|
|
|
:returns: unordered list of unique window handles as strings
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message(
|
|
|
|
"getWindowHandles", key="value" if self.protocol == 1 else None)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-12-12 20:08:31 +03:00
|
|
|
@property
|
|
|
|
def chrome_window_handles(self):
|
|
|
|
"""Get a list of currently open chrome windows.
|
|
|
|
|
|
|
|
Each window handle is assigned by the server, and the list of
|
|
|
|
strings returned does not have a guaranteed ordering.
|
|
|
|
|
|
|
|
:returns: unordered list of unique window handles as strings
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message(
|
|
|
|
"getChromeWindowHandles", key="value" if self.protocol == 1 else None)
|
2014-12-12 20:08:31 +03:00
|
|
|
|
2012-08-09 23:31:12 +04:00
|
|
|
@property
|
|
|
|
def page_source(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""A string representation of the DOM."""
|
|
|
|
return self._send_message("getPageSource", key="value")
|
|
|
|
|
2014-01-17 19:03:43 +04:00
|
|
|
def close(self):
|
|
|
|
"""Close the current window, ending the session if it's the last
|
|
|
|
window currently open.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2014-01-17 19:03:43 +04:00
|
|
|
On B2G this method is a noop and will return immediately.
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("close")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2015-01-09 03:13:30 +03:00
|
|
|
def close_chrome_window(self):
|
|
|
|
"""Close the currently selected chrome window, ending the session
|
|
|
|
if it's the last window open.
|
|
|
|
|
|
|
|
On B2G this method is a noop and will return immediately.
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("closeChromeWindow")
|
2015-01-09 03:13:30 +03:00
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def set_context(self, context):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Sets the context that Marionette commands are running in.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param context: Context, may be one of the class properties
|
2015-08-15 17:46:31 +03:00
|
|
|
`CONTEXT_CHROME` or `CONTEXT_CONTENT`.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2014-11-07 19:35:43 +03:00
|
|
|
Usage example::
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
marionette.set_context(marionette.CONTEXT_CHROME)
|
|
|
|
"""
|
|
|
|
if context not in [self.CONTEXT_CHROME, self.CONTEXT_CONTENT]:
|
|
|
|
raise ValueError("Unknown context: %s" % context)
|
|
|
|
self._send_message("setContext", {"value": context})
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2014-11-07 19:35:43 +03:00
|
|
|
@contextmanager
|
|
|
|
def using_context(self, context):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Sets the context that Marionette commands are running in using
|
2014-11-07 19:35:43 +03:00
|
|
|
a `with` statement. The state of the context on the server is
|
|
|
|
saved before entering the block, and restored upon exiting it.
|
|
|
|
|
|
|
|
:param context: Context, may be one of the class properties
|
2015-08-15 17:46:31 +03:00
|
|
|
`CONTEXT_CHROME` or `CONTEXT_CONTENT`.
|
2014-11-07 19:35:43 +03:00
|
|
|
|
|
|
|
Usage example::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
with marionette.using_context(marionette.CONTEXT_CHROME):
|
|
|
|
# chrome scope
|
|
|
|
... do stuff ...
|
|
|
|
"""
|
|
|
|
scope = self._send_message("getContext", key="value")
|
2014-11-07 19:35:43 +03:00
|
|
|
self.set_context(context)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
self.set_context(scope)
|
|
|
|
|
2015-02-11 04:44:52 +03:00
|
|
|
def switch_to_alert(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns an Alert object for interacting with a currently
|
|
|
|
displayed alert.
|
2015-02-11 04:44:52 +03:00
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
alert = self.marionette.switch_to_alert()
|
|
|
|
text = alert.text
|
|
|
|
alert.accept()
|
2015-08-15 17:46:31 +03:00
|
|
|
"""
|
2015-02-11 04:44:52 +03:00
|
|
|
return Alert(self)
|
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def switch_to_window(self, window_id):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Switch to the specified window; subsequent commands will be
|
|
|
|
directed at the new window.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param window_id: The id or name of the window to switch to.
|
2015-08-15 17:46:31 +03:00
|
|
|
"""
|
|
|
|
self._send_message("switchToWindow", {"name": window_id})
|
2012-03-26 23:40:25 +04:00
|
|
|
self.window = window_id
|
|
|
|
|
2013-07-29 21:46:01 +04:00
|
|
|
def get_active_frame(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns an HTMLElement representing the frame Marionette is
|
|
|
|
currently acting on."""
|
|
|
|
element = self._send_message("getActiveFrame", key="value")
|
|
|
|
if element:
|
|
|
|
return HTMLElement(self, element)
|
2013-07-29 21:46:01 +04:00
|
|
|
return None
|
|
|
|
|
2013-09-10 18:18:00 +04:00
|
|
|
def switch_to_default_content(self):
|
2015-04-24 19:55:22 +03:00
|
|
|
"""Switch the current context to page's default content."""
|
2013-09-10 18:18:00 +04:00
|
|
|
return self.switch_to_frame()
|
|
|
|
|
2015-10-19 23:39:48 +03:00
|
|
|
def switch_to_parent_frame(self):
|
|
|
|
"""
|
|
|
|
Switch to the Parent Frame
|
|
|
|
"""
|
|
|
|
self._send_message("switchToParentFrame")
|
|
|
|
|
2012-12-17 13:45:28 +04:00
|
|
|
def switch_to_frame(self, frame=None, focus=True):
|
2015-04-24 19:55:22 +03:00
|
|
|
"""Switch the current context to the specified frame. Subsequent
|
|
|
|
commands will operate in the context of the specified frame,
|
|
|
|
if applicable.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-04-24 19:55:22 +03:00
|
|
|
:param frame: A reference to the frame to switch to. This can
|
|
|
|
be an ``HTMLElement``, an integer index, string name, or an
|
|
|
|
ID attribute. If you call ``switch_to_frame`` without an
|
|
|
|
argument, it will switch to the top-level frame.
|
|
|
|
|
|
|
|
:param focus: A boolean value which determins whether to focus
|
|
|
|
the frame that we just switched to.
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"focus": focus}
|
2012-03-26 23:40:25 +04:00
|
|
|
if isinstance(frame, HTMLElement):
|
2015-08-15 17:46:31 +03:00
|
|
|
body["element"] = frame.id
|
2015-04-24 19:55:22 +03:00
|
|
|
elif frame is not None:
|
2015-08-15 17:46:31 +03:00
|
|
|
body["id"] = frame
|
|
|
|
self._send_message("switchToFrame", body)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2015-08-28 23:43:54 +03:00
|
|
|
def switch_to_shadow_root(self, host=None):
|
|
|
|
"""Switch the current context to the specified host's Shadow DOM.
|
|
|
|
Subsequent commands will operate in the context of the specified Shadow
|
|
|
|
DOM, if applicable.
|
|
|
|
|
|
|
|
:param host: A reference to the host element containing Shadow DOM.
|
|
|
|
This can be an ``HTMLElement``. If you call
|
|
|
|
``switch_to_shadow_root`` without an argument, it will switch to the
|
|
|
|
parent Shadow DOM or the top-level frame.
|
|
|
|
"""
|
|
|
|
body = {}
|
|
|
|
if isinstance(host, HTMLElement):
|
|
|
|
body["id"] = host.id
|
|
|
|
return self._send_message("switchToShadowRoot", body)
|
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def get_url(self):
|
2014-01-21 22:31:31 +04:00
|
|
|
"""Get a string representing the current URL.
|
|
|
|
|
|
|
|
On Desktop this returns a string representation of the URL of
|
|
|
|
the current top level browsing context. This is equivalent to
|
|
|
|
document.location.href.
|
|
|
|
|
|
|
|
When in the context of the chrome, this returns the canonical
|
|
|
|
URL of the current resource.
|
|
|
|
|
|
|
|
:returns: string representation of URL
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message("getCurrentUrl", key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-05-25 00:14:29 +04:00
|
|
|
def get_window_type(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Gets the windowtype attribute of the window Marionette is
|
|
|
|
currently acting on.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
This command only makes sense in a chrome context. You might use this
|
|
|
|
method to distinguish a browser window from an editor window.
|
2015-08-15 17:46:31 +03:00
|
|
|
"""
|
|
|
|
return self._send_message("getWindowType", key="value")
|
2013-05-25 00:14:29 +04:00
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def navigate(self, url):
|
2015-05-04 13:25:03 +03:00
|
|
|
"""Navigate to given `url`.
|
2014-01-24 17:39:23 +04:00
|
|
|
|
2015-05-04 13:25:03 +03:00
|
|
|
Navigates the current top-level browsing context's content
|
|
|
|
frame to the given URL and waits for the document to load or
|
|
|
|
the session's page timeout duration to elapse before returning.
|
2014-01-24 17:39:23 +04:00
|
|
|
|
2015-05-04 13:25:03 +03:00
|
|
|
The command will return with a failure if there is an error
|
|
|
|
loading the document or the URL is blocked. This can occur if
|
|
|
|
it fails to reach the host, the URL is malformed, the page is
|
|
|
|
restricted (about:* pages), or if there is a certificate issue
|
|
|
|
to name some examples.
|
2014-01-24 17:39:23 +04:00
|
|
|
|
2015-05-04 13:25:03 +03:00
|
|
|
The document is considered successfully loaded when the
|
|
|
|
`DOMContentLoaded` event on the frame element associated with the
|
|
|
|
`window` triggers and `document.readState` is "complete".
|
2014-01-24 17:39:23 +04:00
|
|
|
|
2015-05-04 13:25:03 +03:00
|
|
|
In chrome context it will change the current `window`'s location
|
|
|
|
to the supplied URL and wait until `document.readState` equals
|
|
|
|
"complete" or the page timeout duration has elapsed.
|
2014-01-24 17:39:23 +04:00
|
|
|
|
2015-05-04 13:25:03 +03:00
|
|
|
:param url: The URL to navigate to.
|
2014-01-24 17:39:23 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("get", {"url": url})
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-01-11 23:33:58 +04:00
|
|
|
def timeouts(self, timeout_type, ms):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""An interface for managing timeout behaviour of a Marionette
|
|
|
|
instance.
|
|
|
|
|
|
|
|
Setting timeouts specifies the type and amount of time the
|
|
|
|
Marionette instance should wait during requests.
|
|
|
|
|
|
|
|
There are three types of timeouts that can be set: implicit,
|
|
|
|
script and page load.
|
|
|
|
|
|
|
|
* An implicit timeout specifies the amount of time a Marionette
|
|
|
|
instance should wait when searching for elements. Here, marionette
|
|
|
|
polls a page until an element is found or the timeout expires,
|
|
|
|
whichever occurs first. When searching for multiple elements,
|
|
|
|
the driver should poll the page until at least one element is
|
|
|
|
found or the timeout expires, at which point it should return
|
|
|
|
an empty list.
|
|
|
|
|
|
|
|
* A script timeout specifies the amount of time the Marionette
|
|
|
|
instance should wait after calling executeAsyncScript for the
|
|
|
|
callback to have executed before returning a timeout response.
|
|
|
|
|
|
|
|
* A page load timeout specifies the amount of time the Marionette
|
|
|
|
instance should wait for a page load operation to complete. If
|
|
|
|
this limit is exceeded, the Marionette instance will return a
|
|
|
|
"timeout" response status.
|
|
|
|
|
|
|
|
:param timeout_type: A string value specifying the timeout
|
|
|
|
type. This must be one of three types: 'implicit', 'script'
|
|
|
|
or 'page load'
|
|
|
|
:param ms: A number value specifying the timeout length in
|
|
|
|
milliseconds (ms)
|
2015-01-30 15:37:11 +03:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
if timeout_type not in [self.TIMEOUT_SEARCH, self.TIMEOUT_SCRIPT, self.TIMEOUT_PAGE]:
|
|
|
|
raise ValueError("Unknown timeout type: %s" % timeout_type)
|
|
|
|
body = {"type": timeout_type, "ms": ms}
|
|
|
|
self._send_message("timeouts", body)
|
2013-01-11 23:33:58 +04:00
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def go_back(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Causes the browser to perform a back navigation."""
|
|
|
|
self._send_message("goBack")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def go_forward(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Causes the browser to perform a forward navigation."""
|
|
|
|
self._send_message("goForward")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def refresh(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Causes the browser to perform to refresh the current page."""
|
|
|
|
self._send_message("refresh")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def wrapArguments(self, args):
|
|
|
|
if isinstance(args, list):
|
|
|
|
wrapped = []
|
|
|
|
for arg in args:
|
|
|
|
wrapped.append(self.wrapArguments(arg))
|
|
|
|
elif isinstance(args, dict):
|
|
|
|
wrapped = {}
|
|
|
|
for arg in args:
|
|
|
|
wrapped[arg] = self.wrapArguments(args[arg])
|
|
|
|
elif type(args) == HTMLElement:
|
2015-09-15 22:45:39 +03:00
|
|
|
wrapped = {W3C_WEBELEMENT_KEY: args.id,
|
|
|
|
WEBELEMENT_KEY: args.id}
|
2012-03-26 23:40:25 +04:00
|
|
|
elif (isinstance(args, bool) or isinstance(args, basestring) or
|
2012-05-16 00:20:57 +04:00
|
|
|
isinstance(args, int) or isinstance(args, float) or args is None):
|
2012-03-26 23:40:25 +04:00
|
|
|
wrapped = args
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
def unwrapValue(self, value):
|
|
|
|
if isinstance(value, list):
|
|
|
|
unwrapped = []
|
|
|
|
for item in value:
|
|
|
|
unwrapped.append(self.unwrapValue(item))
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
unwrapped = {}
|
|
|
|
for key in value:
|
2015-09-15 22:45:39 +03:00
|
|
|
if key == W3C_WEBELEMENT_KEY:
|
2012-03-26 23:40:25 +04:00
|
|
|
unwrapped = HTMLElement(self, value[key])
|
2015-03-06 02:01:05 +03:00
|
|
|
break
|
2015-09-15 22:45:39 +03:00
|
|
|
elif key == WEBELEMENT_KEY:
|
2015-03-06 02:01:05 +03:00
|
|
|
unwrapped = HTMLElement(self, value[key])
|
|
|
|
break
|
2012-03-26 23:40:25 +04:00
|
|
|
else:
|
|
|
|
unwrapped[key] = self.unwrapValue(value[key])
|
|
|
|
else:
|
|
|
|
unwrapped = value
|
|
|
|
return unwrapped
|
|
|
|
|
2013-08-28 21:06:03 +04:00
|
|
|
def execute_js_script(self, script, script_args=None, async=True,
|
2015-05-19 02:36:15 +03:00
|
|
|
new_sandbox=True, script_timeout=None,
|
|
|
|
inactivity_timeout=None, filename=None,
|
|
|
|
sandbox='default'):
|
2012-03-26 23:40:25 +04:00
|
|
|
if script_args is None:
|
|
|
|
script_args = []
|
|
|
|
args = self.wrapArguments(script_args)
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"script": script,
|
|
|
|
"args": args,
|
|
|
|
"async": async,
|
|
|
|
"newSandbox": new_sandbox,
|
|
|
|
"scriptTimeout": script_timeout,
|
|
|
|
"inactivityTimeout": inactivity_timeout,
|
|
|
|
"filename": filename,
|
|
|
|
"line": None}
|
|
|
|
rv = self._send_message("executeJSScript", body, key="value")
|
|
|
|
return self.unwrapValue(rv)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-08-28 21:06:03 +04:00
|
|
|
def execute_script(self, script, script_args=None, new_sandbox=True,
|
2015-08-15 17:46:31 +03:00
|
|
|
sandbox="default", script_timeout=None):
|
|
|
|
"""Executes a synchronous JavaScript script, and returns the
|
|
|
|
result (or None if the script does return a value).
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
The script is executed in the context set by the most recent
|
|
|
|
set_context() call, or to the CONTEXT_CONTENT context if set_context()
|
|
|
|
has not been called.
|
|
|
|
|
|
|
|
:param script: A string containing the JavaScript to execute.
|
|
|
|
:param script_args: A list of arguments to pass to the script.
|
2015-08-15 17:46:31 +03:00
|
|
|
:param sandbox: A tag referring to the sandbox you wish to use;
|
|
|
|
if you specify a new tag, a new sandbox will be created.
|
|
|
|
If you use the special tag `system`, the sandbox will
|
|
|
|
be created using the system principal which has elevated
|
|
|
|
privileges.
|
|
|
|
:param new_sandbox: If False, preserve global variables from
|
|
|
|
the last execute_*script call. This is True by default, in which
|
|
|
|
case no globals are preserved.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
Simple usage example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
result = marionette.execute_script("return 1;")
|
|
|
|
assert result == 1
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
You can use the `script_args` parameter to pass arguments to the
|
|
|
|
script:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
result = marionette.execute_script("return arguments[0] + arguments[1];",
|
|
|
|
script_args=[2, 3])
|
|
|
|
assert result == 5
|
|
|
|
some_element = marionette.find_element("id", "someElement")
|
|
|
|
sid = marionette.execute_script("return arguments[0].id;", script_args=[some_element])
|
|
|
|
assert some_element.get_attribute("id") == sid
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
Scripts wishing to access non-standard properties of the window
|
|
|
|
object must use window.wrappedJSObject:
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
result = marionette.execute_script('''
|
|
|
|
window.wrappedJSObject.test1 = "foo";
|
|
|
|
window.wrappedJSObject.test2 = "bar";
|
|
|
|
return window.wrappedJSObject.test1 + window.wrappedJSObject.test2;
|
|
|
|
''')
|
|
|
|
assert result == "foobar"
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
Global variables set by individual scripts do not persist between
|
2015-08-15 17:46:31 +03:00
|
|
|
script calls by default. If you wish to persist data between
|
|
|
|
script calls, you can set new_sandbox to False on your next call,
|
|
|
|
and add any new variables to a new 'global' object like this:
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
marionette.execute_script("global.test1 = 'foo';")
|
|
|
|
result = self.marionette.execute_script("return global.test1;", new_sandbox=False)
|
|
|
|
assert result == "foo"
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
"""
|
2012-03-26 23:40:25 +04:00
|
|
|
if script_args is None:
|
|
|
|
script_args = []
|
|
|
|
args = self.wrapArguments(script_args)
|
2013-06-22 04:13:35 +04:00
|
|
|
stack = traceback.extract_stack()
|
|
|
|
frame = stack[-2:-1][0] # grab the second-to-last frame
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"script": script,
|
|
|
|
"args": args,
|
|
|
|
"newSandbox": new_sandbox,
|
|
|
|
"sandbox": sandbox,
|
|
|
|
"scriptTimeout": script_timeout,
|
|
|
|
"line": int(frame[1]),
|
|
|
|
"filename": os.path.basename(frame[0])}
|
|
|
|
rv = self._send_message("executeScript", body, key="value")
|
|
|
|
return self.unwrapValue(rv)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2015-04-23 23:39:38 +03:00
|
|
|
def execute_async_script(self, script, script_args=None, new_sandbox=True,
|
2015-08-15 17:46:31 +03:00
|
|
|
sandbox="default", script_timeout=None,
|
2015-05-19 02:36:15 +03:00
|
|
|
debug_script=False):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Executes an asynchronous JavaScript script, and returns the
|
|
|
|
result (or None if the script does return a value).
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
The script is executed in the context set by the most recent
|
2015-08-15 17:46:31 +03:00
|
|
|
set_context() call, or to the CONTEXT_CONTENT context if
|
|
|
|
set_context() has not been called.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param script: A string containing the JavaScript to execute.
|
|
|
|
:param script_args: A list of arguments to pass to the script.
|
2015-04-23 23:39:38 +03:00
|
|
|
:param sandbox: A tag referring to the sandbox you wish to use; if
|
2015-08-15 17:46:31 +03:00
|
|
|
you specify a new tag, a new sandbox will be created. If you
|
|
|
|
use the special tag `system`, the sandbox will be created
|
|
|
|
using the system principal which has elevated privileges.
|
|
|
|
:param new_sandbox: If False, preserve global variables from
|
|
|
|
the last execute_*script call. This is True by default,
|
|
|
|
in which case no globals are preserved.
|
2015-02-07 23:22:26 +03:00
|
|
|
:param debug_script: Capture javascript exceptions when in
|
2015-08-15 17:46:31 +03:00
|
|
|
`CONTEXT_CHROME` context.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
Usage example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
marionette.set_script_timeout(10000) # set timeout period of 10 seconds
|
|
|
|
result = self.marionette.execute_async_script('''
|
|
|
|
// this script waits 5 seconds, and then returns the number 1
|
|
|
|
setTimeout(function() {
|
|
|
|
marionetteScriptFinished(1);
|
|
|
|
}, 5000);
|
|
|
|
''')
|
|
|
|
assert result == 1
|
|
|
|
"""
|
2012-03-26 23:40:25 +04:00
|
|
|
if script_args is None:
|
|
|
|
script_args = []
|
|
|
|
args = self.wrapArguments(script_args)
|
2013-06-22 04:13:35 +04:00
|
|
|
stack = traceback.extract_stack()
|
|
|
|
frame = stack[-2:-1][0] # grab the second-to-last frame
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"script": script,
|
|
|
|
"args": args,
|
|
|
|
"newSandbox": new_sandbox,
|
|
|
|
"sandbox": sandbox,
|
|
|
|
"scriptTimeout": script_timeout,
|
|
|
|
"line": int(frame[1]),
|
|
|
|
"filename": os.path.basename(frame[0]),
|
|
|
|
"debug_script": debug_script}
|
|
|
|
rv = self._send_message("executeAsyncScript", body, key="value")
|
|
|
|
return self.unwrapValue(rv)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def find_element(self, method, target, id=None):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns an HTMLElement instances that matches the specified
|
|
|
|
method and target in the current context.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
An HTMLElement instance may be used to call other methods on the
|
|
|
|
element, such as click(). If no element is immediately found, the
|
|
|
|
attempt to locate an element will be repeated for up to the amount of
|
|
|
|
time set by set_search_timeout(). If multiple elements match the given
|
|
|
|
criteria, only the first is returned. If no element matches, a
|
|
|
|
NoSuchElementException will be raised.
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
:param method: The method to use to locate the element; one of:
|
|
|
|
"id", "name", "class name", "tag name", "css selector",
|
|
|
|
"link text", "partial link text", "xpath", "anon" and "anon
|
|
|
|
attribute". Note that the "name", "link text" and "partial
|
|
|
|
link test" methods are not supported in the chrome DOM.
|
2013-08-20 21:59:20 +04:00
|
|
|
:param target: The target of the search. For example, if method =
|
2015-08-15 17:46:31 +03:00
|
|
|
"tag", target might equal "div". If method = "id", target would
|
|
|
|
be an element id.
|
2013-08-20 21:59:20 +04:00
|
|
|
:param id: If specified, search for elements only inside the element
|
2015-08-15 17:46:31 +03:00
|
|
|
with the specified id.
|
|
|
|
"""
|
|
|
|
body = {"value": target, "using": method}
|
2012-03-26 23:40:25 +04:00
|
|
|
if id:
|
2015-08-15 17:46:31 +03:00
|
|
|
body["element"] = id
|
2015-09-12 01:55:59 +03:00
|
|
|
return self._send_message("findElement", body, key="value")
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def find_elements(self, method, target, id=None):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns a list of all HTMLElement instances that match the
|
|
|
|
specified method and target in the current context.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
An HTMLElement instance may be used to call other methods on the
|
2015-08-15 17:46:31 +03:00
|
|
|
element, such as click(). If no element is immediately found,
|
|
|
|
the attempt to locate an element will be repeated for up to the
|
|
|
|
amount of time set by set_search_timeout().
|
|
|
|
|
|
|
|
:param method: The method to use to locate the elements; one
|
|
|
|
of: "id", "name", "class name", "tag name", "css selector",
|
|
|
|
"link text", "partial link text", "xpath", "anon" and "anon
|
|
|
|
attribute". Note that the "name", "link text" and "partial link
|
|
|
|
test" methods are not supported in the chrome DOM.
|
2013-08-20 21:59:20 +04:00
|
|
|
:param target: The target of the search. For example, if method =
|
2015-08-15 17:46:31 +03:00
|
|
|
"tag", target might equal "div". If method = "id", target would be
|
|
|
|
an element id.
|
2013-08-20 21:59:20 +04:00
|
|
|
:param id: If specified, search for elements only inside the element
|
2015-08-15 17:46:31 +03:00
|
|
|
with the specified id.
|
|
|
|
"""
|
|
|
|
body = {"value": target, "using": method}
|
2012-03-26 23:40:25 +04:00
|
|
|
if id:
|
2015-08-15 17:46:31 +03:00
|
|
|
body["element"] = id
|
2015-09-12 01:55:59 +03:00
|
|
|
return self._send_message(
|
2015-08-15 17:46:31 +03:00
|
|
|
"findElements", body, key="value" if self.protocol == 1 else None)
|
2015-09-12 01:55:59 +03:00
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
|
2013-01-14 18:57:54 +04:00
|
|
|
def get_active_element(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
el = self._send_message("getActiveElement", key="value")
|
|
|
|
return HTMLElement(self, el)
|
2013-01-14 18:57:54 +04:00
|
|
|
|
2012-03-26 23:40:25 +04:00
|
|
|
def log(self, msg, level=None):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Stores a timestamped log message in the Marionette server
|
|
|
|
for later retrieval.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param msg: String with message to log.
|
|
|
|
:param level: String with log level (e.g. "INFO" or "DEBUG"). If None,
|
2015-08-15 17:46:31 +03:00
|
|
|
defaults to "INFO".
|
|
|
|
"""
|
|
|
|
body = {"value": msg, "level": level}
|
|
|
|
self._send_message("log", body)
|
2012-03-26 23:40:25 +04:00
|
|
|
|
|
|
|
def get_logs(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Returns the list of logged messages.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
Each log message is an array with three string elements: the level,
|
|
|
|
the message, and a date.
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
Usage example::
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
marionette.log("I AM INFO")
|
|
|
|
marionette.log("I AM ERROR", "ERROR")
|
|
|
|
logs = marionette.get_logs()
|
|
|
|
assert logs[0][1] == "I AM INFO"
|
|
|
|
assert logs[1][1] == "I AM ERROR"
|
|
|
|
"""
|
|
|
|
return self._send_message("getLogs",
|
|
|
|
key="value" if self.protocol == 1 else None)
|
2012-06-04 21:50:06 +04:00
|
|
|
|
2012-10-26 19:23:52 +04:00
|
|
|
def import_script(self, js_file):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Imports a script into the scope of the execute_script and
|
|
|
|
execute_async_script calls.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
This is particularly useful if you wish to import your own
|
|
|
|
libraries.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param js_file: Filename of JavaScript file to import.
|
|
|
|
|
|
|
|
For example, Say you have a script, importfunc.js, that contains:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
let testFunc = function() { return "i'm a test function!";};
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
Assuming this file is in the same directory as the test, you
|
|
|
|
could do something like:
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
js = os.path.abspath(os.path.join(__file__, os.path.pardir, "importfunc.js"))
|
|
|
|
marionette.import_script(js)
|
|
|
|
assert "i'm a test function!" == self.marionette.execute_script("return testFunc();")
|
|
|
|
"""
|
|
|
|
js = ""
|
|
|
|
with open(js_file, "r") as f:
|
2012-10-26 19:23:52 +04:00
|
|
|
js = f.read()
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"script": js}
|
|
|
|
self._send_message("importScript", body)
|
2012-08-24 02:07:16 +04:00
|
|
|
|
2013-10-09 00:11:45 +04:00
|
|
|
def clear_imported_scripts(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Clears all imported scripts in this context, ie: calling
|
|
|
|
clear_imported_scripts in chrome context will clear only scripts
|
|
|
|
you imported in chrome, and will leave the scripts you imported
|
|
|
|
in content context.
|
|
|
|
"""
|
|
|
|
self._send_message("clearImportedScripts")
|
2013-10-09 00:11:45 +04:00
|
|
|
|
2012-11-22 19:53:44 +04:00
|
|
|
def add_cookie(self, cookie):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Adds a cookie to your current session.
|
2012-11-22 19:53:44 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
:param cookie: A dictionary object, with required keys - "name"
|
|
|
|
and "value"; optional keys - "path", "domain", "secure",
|
|
|
|
"expiry".
|
2012-11-22 19:53:44 +04:00
|
|
|
|
2013-08-20 21:59:20 +04:00
|
|
|
Usage example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
driver.add_cookie({"name": "foo", "value": "bar"})
|
|
|
|
driver.add_cookie({"name": "foo", "value": "bar", "path": "/"})
|
|
|
|
driver.add_cookie({"name": "foo", "value": "bar", "path": "/",
|
|
|
|
"secure": True})
|
2012-11-22 19:53:44 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"cookie": cookie}
|
|
|
|
self._send_message("addCookie", body)
|
2012-11-22 19:53:44 +04:00
|
|
|
|
|
|
|
def delete_all_cookies(self):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Delete all cookies in the scope of the current session.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
Usage example:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
driver.delete_all_cookies()
|
2012-11-22 19:53:44 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("deleteAllCookies")
|
2012-11-22 19:53:44 +04:00
|
|
|
|
|
|
|
def delete_cookie(self, name):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Delete a cookie by its name.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
|
|
|
:param name: Name of cookie to delete.
|
|
|
|
|
|
|
|
Usage example:
|
|
|
|
|
|
|
|
::
|
2012-11-22 19:53:44 +04:00
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
driver.delete_cookie("foo")
|
2012-11-22 19:53:44 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
self._send_message("deleteCookie", {"name": name})
|
2012-11-22 19:53:44 +04:00
|
|
|
|
|
|
|
def get_cookie(self, name):
|
2015-08-15 17:46:31 +03:00
|
|
|
"""Get a single cookie by name. Returns the cookie if found,
|
|
|
|
None if not.
|
2012-11-22 19:53:44 +04:00
|
|
|
|
2013-08-20 21:59:20 +04:00
|
|
|
:param name: Name of cookie to get.
|
2012-11-22 19:53:44 +04:00
|
|
|
"""
|
|
|
|
cookies = self.get_cookies()
|
|
|
|
for cookie in cookies:
|
2015-08-15 17:46:31 +03:00
|
|
|
if cookie["name"] == name:
|
2012-11-22 19:53:44 +04:00
|
|
|
return cookie
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_cookies(self):
|
2014-01-21 20:40:20 +04:00
|
|
|
"""Get all the cookies for the current domain.
|
|
|
|
|
|
|
|
This is the equivalent of calling `document.cookie` and
|
|
|
|
parsing the result.
|
|
|
|
|
2015-08-15 17:46:31 +03:00
|
|
|
:returns: A list of cookies for the current domain.
|
2014-01-21 20:40:20 +04:00
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message("getCookies", key="value" if self.protocol == 1 else None)
|
2012-11-22 19:53:44 +04:00
|
|
|
|
2015-02-02 15:57:00 +03:00
|
|
|
def screenshot(self, element=None, highlights=None, format="base64",
|
|
|
|
full=True):
|
2014-01-24 17:45:58 +04:00
|
|
|
"""Takes a screenshot of a web element or the current frame.
|
2013-08-20 21:59:20 +04:00
|
|
|
|
2014-01-24 17:45:58 +04:00
|
|
|
The screen capture is returned as a lossless PNG image encoded
|
2014-05-12 18:57:53 +04:00
|
|
|
as a base 64 string by default. If the `element` argument is defined the
|
2014-01-24 17:45:58 +04:00
|
|
|
capture area will be limited to the bounding box of that
|
|
|
|
element. Otherwise, the capture area will be the bounding box
|
|
|
|
of the current frame.
|
|
|
|
|
|
|
|
:param element: The element to take a screenshot of. If None, will
|
|
|
|
take a screenshot of the current frame.
|
|
|
|
|
|
|
|
:param highlights: A list of HTMLElement objects to draw a red
|
|
|
|
box around in the returned screenshot.
|
2014-06-08 04:05:00 +04:00
|
|
|
|
2014-05-12 18:57:53 +04:00
|
|
|
:param format: if "base64" (the default), returns the screenshot
|
|
|
|
as a base64-string. If "binary", the data is decoded and
|
|
|
|
returned as raw binary.
|
2014-01-24 17:45:58 +04:00
|
|
|
|
2015-02-02 15:57:00 +03:00
|
|
|
:param full: If True (the default), the capture area will be the
|
|
|
|
complete frame. Else only the viewport is captured. Only applies
|
|
|
|
when `element` is None.
|
2014-01-24 17:45:58 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
if element:
|
2012-12-01 03:25:26 +04:00
|
|
|
element = element.id
|
2013-09-04 12:05:43 +04:00
|
|
|
lights = None
|
2014-01-24 17:45:58 +04:00
|
|
|
if highlights:
|
|
|
|
lights = [highlight.id for highlight in highlights]
|
2015-08-15 17:46:31 +03:00
|
|
|
|
|
|
|
body = {"id": element,
|
|
|
|
"highlights": lights,
|
|
|
|
"full": full}
|
|
|
|
data = self._send_message("takeScreenshot", body, key="value")
|
|
|
|
|
|
|
|
if format == "base64":
|
|
|
|
return data
|
|
|
|
elif format == "binary":
|
|
|
|
return base64.b64decode(data.encode("ascii"))
|
2014-05-12 18:57:53 +04:00
|
|
|
else:
|
|
|
|
raise ValueError("format parameter must be either 'base64'"
|
|
|
|
" or 'binary', not {0}".format(repr(format)))
|
2013-12-17 20:01:29 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def orientation(self):
|
|
|
|
"""Get the current browser orientation.
|
|
|
|
|
|
|
|
Will return one of the valid primary orientation values
|
|
|
|
portrait-primary, landscape-primary, portrait-secondary, or
|
|
|
|
landscape-secondary.
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message("getScreenOrientation", key="value")
|
2013-12-17 20:01:29 +04:00
|
|
|
|
|
|
|
def set_orientation(self, orientation):
|
|
|
|
"""Set the current browser orientation.
|
|
|
|
|
|
|
|
The supplied orientation should be given as one of the valid
|
|
|
|
orientation values. If the orientation is unknown, an error
|
|
|
|
will be raised.
|
|
|
|
|
|
|
|
Valid orientations are "portrait" and "landscape", which fall
|
|
|
|
back to "portrait-primary" and "landscape-primary"
|
|
|
|
respectively, and "portrait-secondary" as well as
|
|
|
|
"landscape-secondary".
|
|
|
|
|
|
|
|
:param orientation: The orientation to lock the screen in.
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"orientation": orientation}
|
|
|
|
self._send_message("setScreenOrientation", body)
|
2013-12-17 20:01:29 +04:00
|
|
|
if self.emulator:
|
2014-06-19 22:17:26 +04:00
|
|
|
self.emulator.screen.orientation = orientation.lower()
|
2014-09-08 17:05:49 +04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def window_size(self):
|
|
|
|
"""Get the current browser window size.
|
|
|
|
|
|
|
|
Will return the current browser window size in pixels. Refers to
|
|
|
|
window outerWidth and outerHeight values, which include scroll bars,
|
|
|
|
title bars, etc.
|
|
|
|
|
|
|
|
:returns: dictionary representation of current window width and height
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message("getWindowSize",
|
|
|
|
key="value" if self.protocol == 1 else None)
|
2014-09-08 17:05:49 +04:00
|
|
|
|
|
|
|
def set_window_size(self, width, height):
|
|
|
|
"""Resize the browser window currently in focus.
|
|
|
|
|
|
|
|
The supplied width and height values refer to the window outerWidth
|
|
|
|
and outerHeight values, which include scroll bars, title bars, etc.
|
|
|
|
|
|
|
|
An error will be returned if the requested window size would result
|
|
|
|
in the window being in the maximised state.
|
|
|
|
|
|
|
|
:param width: The width to resize the window to.
|
|
|
|
:param height: The height to resize the window to.
|
|
|
|
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
body = {"width": width, "height": height}
|
|
|
|
self._send_message("setWindowSize", body)
|
2014-09-19 13:39:28 +04:00
|
|
|
|
|
|
|
def maximize_window(self):
|
|
|
|
""" Resize the browser window currently receiving commands. The action
|
|
|
|
should be equivalent to the user pressing the the maximize button
|
|
|
|
"""
|
2015-08-15 17:46:31 +03:00
|
|
|
return self._send_message("maximizeWindow")
|