DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
# 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/.

from __future__ import absolute_import

import base64
import datetime
import json
import os
import socket
import sys
import time
import traceback

from contextlib import contextmanager

from six import reraise

from . import errors
from . import transport
from .decorators import do_process_check
from .geckoinstance import GeckoInstance
from .keys import Keys
from .timeout import Timeouts

CHROME_ELEMENT_KEY = "chromeelement-9fc5-4b51-a3c8-01716eedeb04"
FRAME_KEY = "frame-075b-4da1-b6ba-e579c2d3230a"
WEB_ELEMENT_KEY = "element-6066-11e4-a52e-4f735466cecf"
WINDOW_KEY = "window-fcc6-11e5-b4f8-330a88ab9d7f"


class HTMLElement(object):
    """Represents a DOM Element."""

    identifiers = (CHROME_ELEMENT_KEY, FRAME_KEY, WINDOW_KEY, WEB_ELEMENT_KEY)

    def __init__(self, marionette, id):
        self.marionette = marionette
        assert(id is not None)
        self.id = id

    def __str__(self):
        return self.id

    def __eq__(self, other_element):
        return self.id == other_element.id

    def find_element(self, method, target):
        """Returns an ``HTMLElement`` instance that matches the specified
        method and target, relative to the current element.

        For more details on this function, see the
        :func:`~marionette_driver.marionette.Marionette.find_element` method
        in the Marionette class.
        """
        return self.marionette.find_element(method, target, self.id)

    def find_elements(self, method, target):
        """Returns a list of all ``HTMLElement`` instances that match the
        specified method and target in the current context.

        For more details on this function, see the
        :func:`~marionette_driver.marionette.Marionette.find_elements` method
        in the Marionette class.
        """
        return self.marionette.find_elements(method, target, self.id)

    def get_attribute(self, name):
        """Returns the requested attribute, or None if no attribute
        is set.
        """
        body = {"id": self.id, "name": name}
        return self.marionette._send_message("WebDriver:GetElementAttribute",
                                             body, key="value")

    def get_property(self, name):
        """Returns the requested property, or None if the property is
        not set.
        """
        try:
            body = {"id": self.id, "name": name}
            return self.marionette._send_message("WebDriver:GetElementProperty",
                                                 body, key="value")
        except errors.UnknownCommandException:
            # Keep backward compatibility for code which uses get_attribute() to
            # also retrieve element properties.
            # Remove when Firefox 55 is stable.
            return self.get_attribute(name)

    def click(self):
        """Simulates a click on the element."""
        self.marionette._send_message("WebDriver:ElementClick",
                                      {"id": self.id})

    def tap(self, x=None, y=None):
        """Simulates a set of tap events on the element.

        :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)

    @property
    def text(self):
        """Returns the visible text of the element, and its child elements."""
        body = {"id": self.id}
        return self.marionette._send_message("WebDriver:GetElementText",
                                             body, key="value")

    def send_keys(self, *strings):
        """Sends the string via synthesized keypresses to the element.
           If an array is passed in like `marionette.send_keys(Keys.SHIFT, "a")` it
           will be joined into a string.
           If an integer is passed in like `marionette.send_keys(1234)` it will be
           coerced into a string.
        """
        keys = Marionette.convert_keys(*strings)
        self.marionette._send_message("WebDriver:ElementSendKeys",
                                      {"id": self.id, "text": keys})

    def clear(self):
        """Clears the input of the element."""
        self.marionette._send_message("WebDriver:ElementClear",
                                      {"id": self.id})

    def is_selected(self):
        """Returns True if the element is selected."""
        body = {"id": self.id}
        return self.marionette._send_message("WebDriver:IsElementSelected",
                                             body, key="value")

    def is_enabled(self):
        """This command will return False if all the following criteria
        are met otherwise return True:

        * A form control is disabled.
        * A ``HTMLElement`` has a disabled boolean attribute.
        """
        body = {"id": self.id}
        return self.marionette._send_message("WebDriver:IsElementEnabled",
                                             body, key="value")

    def is_displayed(self):
        """Returns True if the element is displayed, False otherwise."""
        body = {"id": self.id}
        return self.marionette._send_message("WebDriver:IsElementDisplayed",
                                             body, key="value")

    @property
    def tag_name(self):
        """The tag name of the element."""
        body = {"id": self.id}
        return self.marionette._send_message("WebDriver:GetElementTagName",
                                             body, key="value")

    @property
    def rect(self):
        """Gets the element's bounding rectangle.

        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``.
        """
        return self.marionette._send_message("WebDriver:GetElementRect",
                                             {"id": self.id})

    def value_of_css_property(self, property_name):
        """Gets the value of the specified CSS property name.

        :param property_name: Property name to get the value of.
        """
        body = {"id": self.id, "propertyName": property_name}
        return self.marionette._send_message("WebDriver:GetElementCSSValue",
                                             body, key="value")

    @classmethod
    def _from_json(cls, json, marionette):
        if isinstance(json, dict):
            if WEB_ELEMENT_KEY in json:
                return cls(marionette, json[WEB_ELEMENT_KEY])
            elif CHROME_ELEMENT_KEY in json:
                return cls(marionette, json[CHROME_ELEMENT_KEY])
            elif FRAME_KEY in json:
                return cls(marionette, json[FRAME_KEY])
            elif WINDOW_KEY in json:
                return cls(marionette, json[WINDOW_KEY])
        raise ValueError("Unrecognised web element")


class MouseButton(object):
    """Enum-like class for mouse button constants."""
    LEFT = 0
    MIDDLE = 1
    RIGHT = 2


class Actions(object):
    '''
    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(By.ID, "element1")
        element2 = marionette.find_element(By.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()
    '''

    def __init__(self, marionette):
        self.action_chain = []
        self.marionette = marionette
        self.current_id = None

    def press(self, element, x=None, y=None):
        '''
        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.
        '''
        element = element.id
        self.action_chain.append(['press', element, x, y])
        return self

    def release(self):
        '''
        Sends a 'touchend' event to this element.

        May only be called if :func:`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.
        '''
        self.action_chain.append(['release'])
        return self

    def move(self, element):
        '''
        Sends a 'touchmove' event at the center of the target element.

        :param element: Element to move towards.

        May only be called if :func:`press` has already be called.
        '''
        element = element.id
        self.action_chain.append(['move', element])
        return self

    def move_by_offset(self, x, y):
        '''
        Sends 'touchmove' event to the given x, y coordinates relative to the
        top-left of the currently touched element.

        May only be called if :func:`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.
        '''
        self.action_chain.append(['moveByOffset', x, y])
        return self

    def wait(self, time=None):
        '''
        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.
        '''
        self.action_chain.append(['wait', time])
        return self

    def cancel(self):
        '''
        Sends 'touchcancel' event to the target of the original 'touchstart' event.

        May only be called if :func:`press` has already be called.
        '''
        self.action_chain.append(['cancel'])
        return self

    def tap(self, element, x=None, y=None):
        '''
        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()
        '''
        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):
        '''
        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.
        '''
        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

    def click(self, element, button=MouseButton.LEFT, count=1):
        '''
        Performs a click with additional parameters to allow for double clicking,
        right click, middle click, etc.

        :param element: The element to click.
        :param button: The mouse button to click (indexed from 0, left to right).
        :param count: Optional, the count of clicks to synthesize (for double
                      click events).
        '''
        el = element.id
        self.action_chain.append(['click', el, button, count])
        return self

    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)

    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)

    def flick(self, element, x1, y1, x2, y2, duration=200):
        '''
        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.
        :param x2: Ending x-coordinate of flick, relative to the top left
         corner of the element.
        :param y2: Ending y-coordinate of flick, relative to the top left
         corner of the element.
        :param duration: Time needed for the flick gesture for complete (in
         milliseconds).
        '''
        element = element.id
        elapsed = 0
        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])
        while elapsed < duration:
            elapsed += time_increment
            self.action_chain.append(['moveByOffset', move_x, move_y])
            self.action_chain.append(['wait', time_increment/1000])
        self.action_chain.append(['release'])
        return self

    def long_press(self, element, time_in_seconds, x=None, y=None):
        '''
        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.
        :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()

        '''
        element = element.id
        self.action_chain.append(['press', element, x, y])
        self.action_chain.append(['wait', time_in_seconds])
        self.action_chain.append(['release'])
        return self

    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

    def perform(self):
        """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}
        try:
            self.current_id = self.marionette._send_message("Marionette:ActionChain",
                                                            body, key="value")
        except errors.UnknownCommandException:
            self.current_id = self.marionette._send_message("actionChain",
                                                            body, key="value")
        self.action_chain = []
        return self


class MultiActions(object):
    '''
    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()
    '''

    def __init__(self, marionette):
        self.multi_actions = []
        self.max_length = 0
        self.marionette = marionette

    def add(self, action):
        '''
        Adds a set of actions to perform.

        :param action: An Actions object.
        '''
        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):
        """Perform all the actions added to this object."""
        body = {"value": self.multi_actions, "max_length": self.max_length}
        try:
            self.marionette._send_message("Marionette:MultiAction", body)
        except errors.UnknownCommandException:
            self.marionette._send_message("multiAction", body)


class Alert(object):
    """A class for interacting with alerts.

    ::

        Alert(marionette).accept()
        Alert(marionette).dismiss()
    """

    def __init__(self, marionette):
        self.marionette = marionette

    def accept(self):
        """Accept a currently displayed modal dialog."""
        self.marionette._send_message("WebDriver:AcceptAlert")

    def dismiss(self):
        """Dismiss a currently displayed modal dialog."""
        self.marionette._send_message("WebDriver:DismissAlert")

    @property
    def text(self):
        """Return the currently displayed text in a tab modal."""
        return self.marionette._send_message("WebDriver:GetAlertText",
                                             key="value")

    def send_keys(self, *string):
        """Send keys to the currently displayed text input area in an open
        tab modal dialog."""
        self.marionette._send_message("WebDriver:SendAlertText",
                                      {"text": Marionette.convert_keys(*string)})


class Marionette(object):
    """Represents a Marionette connection to a browser or device."""

    CONTEXT_CHROME = "chrome"  # non-browser content: windows, dialogs, etc.
    CONTEXT_CONTENT = "content"  # browser content: iframes, divs, etc.
    DEFAULT_STARTUP_TIMEOUT = 120
    DEFAULT_SHUTDOWN_TIMEOUT = 120  # Firefox will kill hanging threads after 60s

    # Bug 1336953 - Until we can remove the socket timeout parameter it has to be
    # set a default value which is larger than the longest timeout as defined by the
    # WebDriver spec. In that case its 300s for page load. Also add another minute
    # so that slow builds have enough time to send the timeout error to the client.
    DEFAULT_SOCKET_TIMEOUT = 360

    def __init__(self, host="127.0.0.1", port=2828, app=None, bin=None,
                 baseurl=None, socket_timeout=None,
                 startup_timeout=None, **instance_args):
        """Construct a holder for the Marionette connection.

        Remember to call ``start_session`` in order to initiate the
        connection and start a Marionette session.

        :param host: Host where the Marionette server listens.
            Defaults to 127.0.0.1.
        :param port: Port where the Marionette server listens.
            Defaults to port 2828.
        :param baseurl: Where to look for files served from Marionette's
            www directory.
        :param socket_timeout: Timeout for Marionette socket operations.
        :param startup_timeout: Seconds to wait for a connection with
            binary.
        :param bin: Path to browser binary.  If any truthy value is given
            this will attempt to start a Gecko instance with the specified
            `app`.
        :param app: Type of ``instance_class`` to use for managing app
            instance. See ``marionette_driver.geckoinstance``.
        :param instance_args: Arguments to pass to ``instance_class``.

        """
        self.host = "127.0.0.1"  # host
        self.port = self.local_port = int(port)
        self.bin = bin
        self.client = None
        self.instance = None
        self.session = None
        self.session_id = None
        self.process_id = None
        self.profile = None
        self.window = None
        self.chrome_window = None
        self.baseurl = baseurl
        self._test_name = None
        self.crashed = 0

        if socket_timeout is None:
            self.socket_timeout = self.DEFAULT_SOCKET_TIMEOUT
        else:
            self.socket_timeout = float(socket_timeout)

        if startup_timeout is None:
            self.startup_timeout = self.DEFAULT_STARTUP_TIMEOUT
        else:
            self.startup_timeout = int(startup_timeout)

        if self.bin:
            self.instance = GeckoInstance.create(
                app, host=self.host, port=self.port, bin=self.bin, **instance_args)
            self.start_binary(self.startup_timeout)

        self.timeout = Timeouts(self)

    @property
    def profile_path(self):
        if self.instance and self.instance.profile:
            return self.instance.profile.profile

    def start_binary(self, timeout):
        if not self.is_port_available(self.port, host=self.host):
            raise IOError("Port {0}:{1} is unavailable.".format(self.host, self.port))

        try:
            self.instance.start()
            self.raise_for_port(timeout=timeout)
        except socket.timeout:
            # Something went wrong with starting up Marionette server. Given
            # that the process will not quit itself, force a shutdown immediately.
            self.cleanup()

            msg = "Process killed after {}s because no connection to Marionette "\
                  "server could be established. Check gecko.log for errors"
            _, _, tb = sys.exc_info()
            reraise(IOError, msg.format(timeout), tb)

    def cleanup(self):
        if self.session is not None:
            try:
                self.delete_session()
            except (errors.MarionetteException, IOError):
                # 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
        if self.instance:
            # stop application and, if applicable, stop emulator
            self.instance.close(clean=True)
            if self.instance.unresponsive_count >= 3:
                raise errors.UnresponsiveInstanceException(
                    "Application clean-up has failed >2 consecutive times.")

    def __del__(self):
        self.cleanup()

    @staticmethod
    def is_port_available(port, host=''):
        port = int(port)
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        try:
            s.bind((host, port))
            return True
        except socket.error:
            return False
        finally:
            s.close()

    def raise_for_port(self, timeout=None):
        """Raise socket.timeout if no connection can be established.

        :param timeout: Timeout in seconds for the server to be ready.

        """
        if timeout is None:
            timeout = self.DEFAULT_STARTUP_TIMEOUT

        runner = None
        if self.instance is not None:
            runner = self.instance.runner

        poll_interval = 0.1
        starttime = datetime.datetime.now()
        timeout_time = starttime + datetime.timedelta(seconds=timeout)

        client = transport.TcpTransport(self.host, self.port, 0.5)

        connected = False
        while datetime.datetime.now() < timeout_time:
            # If the instance we want to connect to is not running return immediately
            if runner is not None and not runner.is_running():
                break

            try:
                client.connect()
                return True
            except socket.error:
                pass
            finally:
                client.close()

            time.sleep(poll_interval)

        if not connected:
            raise socket.timeout("Timed out waiting for connection on {0}:{1}!".format(
                self.host, self.port))

    @do_process_check
    def _send_message(self, name, params=None, key=None):
        """Send a blocking message to the server.

        Marionette provides an asynchronous, non-blocking interface and
        this attempts to paper over this by providing a synchronous API
        to the user.

        :param name: Requested command key.
        :param params: Optional dictionary of key/value arguments.
        :param key: Optional key to extract from response.

        :returns: Full response from the server, or if `key` is given,
            the value of said key in the response.
        """
        if not self.session_id and name != "WebDriver:NewSession":
            raise errors.InvalidSessionIdException("Please start a session")

        try:
            msg = self.client.request(name, params)

        except IOError:
            self.delete_session(send_request=False)
            raise

        res, err = msg.result, msg.error
        if err:
            self._handle_error(err)

        if key is not None:
            return self._unwrap_response(res.get(key))
        else:
            return self._unwrap_response(res)

    def _unwrap_response(self, value):
        if isinstance(value, dict) and any(k in value.keys() for k in HTMLElement.identifiers):
            return HTMLElement._from_json(value, self)
        elif isinstance(value, list):
            return list(self._unwrap_response(item) for item in value)
        else:
            return value

    def _handle_error(self, obj):
        error = obj["error"]
        message = obj["message"]
        stacktrace = obj["stacktrace"]

        raise errors.lookup(error)(message, stacktrace=stacktrace)

    def check_for_crash(self):
        """Check if the process crashed.

        :returns: True, if a crash happened since the method has been called the last time.
        """
        crash_count = 0

        if self.instance:
            name = self.test_name or 'marionette.py'
            crash_count = self.instance.runner.check_for_crashes(test_name=name)
            self.crashed = self.crashed + crash_count

        return crash_count > 0

    def _handle_socket_failure(self):
        """Handle socket failures for the currently connected application.

        If the application crashed then clean-up internal states, or in case of a content
        crash also kill the process. If there are other reasons for a socket failure,
        wait for the process to shutdown itself, or force kill it.

        Please note that the method expects an exception to be handled on the current stack
        frame, and is only called via the `@do_process_check` decorator.

        """
        exc, val, tb = sys.exc_info()

        # If the application hasn't been launched by Marionette no further action can be done.
        # In such cases we simply re-throw the exception.
        if not self.instance:
            reraise(exc, val, tb)

        else:
            # Somehow the socket disconnected. Give the application some time to shutdown
            # itself before killing the process.
            returncode = self.instance.runner.wait(timeout=self.DEFAULT_SHUTDOWN_TIMEOUT)

            if returncode is None:
                message = ('Process killed because the connection to Marionette server is '
                           'lost. Check gecko.log for errors')
                # This will force-close the application without sending any other message.
                self.cleanup()
            else:
                # If Firefox quit itself check if there was a crash
                crash_count = self.check_for_crash()

                if crash_count > 0:
                    if returncode == 0:
                        message = 'Content process crashed'
                    else:
                        message = 'Process crashed (Exit code: {returncode})'
                else:
                    message = 'Process has been unexpectedly closed (Exit code: {returncode})'

                self.delete_session(send_request=False)

            message += ' (Reason: {reason})'

            reraise(IOError, message.format(returncode=returncode, reason=val), tb)

    @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 "".join(typing)

    def clear_pref(self, pref):
        """Clear the user-defined value from the specified preference.

        :param pref: Name of the preference.
        """
        with self.using_context(self.CONTEXT_CHROME):
            self.execute_script("""
               Components.utils.import("resource://gre/modules/Preferences.jsm");
               Preferences.reset(arguments[0]);
               """, script_args=(pref,))

    def get_pref(self, pref, default_branch=False, value_type="unspecified"):
        """Get the value of the specified preference.

        :param pref: Name of the preference.
        :param default_branch: Optional, if `True` the preference value will be read
                               from the default branch. Otherwise the user-defined
                               value if set is returned. Defaults to `False`.
        :param value_type: Optional, XPCOM interface of the pref's complex value.
                           Possible values are: `nsIFile` and
                           `nsIPrefLocalizedString`.

        Usage example::

            marionette.get_pref("browser.tabs.warnOnClose")

        """
        with self.using_context(self.CONTEXT_CHROME):
            pref_value = self.execute_script("""
                Components.utils.import("resource://gre/modules/Preferences.jsm");

                let pref = arguments[0];
                let defaultBranch = arguments[1];
                let valueType = arguments[2];

                prefs = new Preferences({defaultBranch: defaultBranch});
                return prefs.get(pref, null, Components.interfaces[valueType]);
                """, script_args=(pref, default_branch, value_type))
            return pref_value

    def set_pref(self, pref, value, default_branch=False):
        """Set the value of the specified preference.

        :param pref: Name of the preference.
        :param value: The value to set the preference to. If the value is None,
                      reset the preference to its default value. If no default
                      value exists, the preference will cease to exist.
        :param default_branch: Optional, if `True` the preference value will
                       be written to the default branch, and will remain until
                       the application gets restarted. Otherwise a user-defined
                       value is set. Defaults to `False`.

        Usage example::

            marionette.set_pref("browser.tabs.warnOnClose", True)

        """
        with self.using_context(self.CONTEXT_CHROME):
            if value is None:
                self.clear_pref(pref)
                return

            self.execute_script("""
                Components.utils.import("resource://gre/modules/Preferences.jsm");

                let pref = arguments[0];
                let value = arguments[1];
                let defaultBranch = arguments[2];

                prefs = new Preferences({defaultBranch: defaultBranch});
                prefs.set(pref, value);
                """, script_args=(pref, value, default_branch))

    def set_prefs(self, prefs, default_branch=False):
        """Set the value of a list of preferences.

        :param prefs: A dict containing one or more preferences and their values
                      to be set. See :func:`set_pref` for further details.
        :param default_branch: Optional, if `True` the preference value will
                       be written to the default branch, and will remain until
                       the application gets restarted. Otherwise a user-defined
                       value is set. Defaults to `False`.

        Usage example::

            marionette.set_prefs({"browser.tabs.warnOnClose": True})

        """
        for pref, value in prefs.items():
            self.set_pref(pref, value, default_branch=default_branch)

    @contextmanager
    def using_prefs(self, prefs, default_branch=False):
        """Set preferences for code 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. See :func:`set_prefs` for further details.
        :param default_branch: Optional, if `True` the preference value will
                       be written to the default branch, and will remain until
                       the application gets restarted. Otherwise a user-defined
                       value is set. Defaults to `False`.

        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, default_branch=default_branch)

        try:
            yield
        finally:
            self.set_prefs(original_prefs, default_branch=default_branch)

    @do_process_check
    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
        with self.using_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 = '{0}';
                let value = '{1}';
                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;
                }}
                """.format(pref, value))
                if not pref_exists:
                    break

        if not pref_exists:
            context = self._send_message("Marionette:GetContext",
                                         key="value")
            self.delete_session()
            self.instance.restart(prefs)
            self.raise_for_port()
            self.start_session()

            # Restore the context as used before the restart
            self.set_context(context)

    def _request_in_app_shutdown(self, *shutdown_flags):
        """Attempt to quit the currently running instance from inside the
        application.

        Duplicate entries in `shutdown_flags` are removed, and
        `"eForceQuit"` is added if no other `*Quit` flags are given.
        This provides backwards compatible behaviour with earlier
        Firefoxen.

        This method effectively calls `Services.startup.quit` in Gecko.
        Possible flag values are listed at http://mzl.la/1X0JZsC.

        :param shutdown_flags: Optional additional quit masks to include.
            Duplicates are removed, and `"eForceQuit"` is added if no
            flags ending with `"Quit"` are present.

        :throws InvalidArgumentException: If there are multiple
            `shutdown_flags` ending with `"Quit"`.

        :returns: The cause of shutdown.
        """

        # The vast majority of this function was implemented inside
        # the quit command as part of bug 1337743, and can be
        # removed from here in Firefox 55 at the earliest.

        # remove duplicates
        flags = set(shutdown_flags)

        # add eForceQuit if there are no *Quits
        if not any(flag.endswith("Quit") for flag in flags):
            flags = flags | set(("eForceQuit",))

        # Trigger a quit-application-requested observer notification
        # so that components can safely shutdown before quitting the
        # application.
        with self.using_context("chrome"):
            canceled = self.execute_script("""
                Components.utils.import("resource://gre/modules/Services.jsm");
                let cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"]
                    .createInstance(Components.interfaces.nsISupportsPRBool);
                Services.obs.notifyObservers(cancelQuit, "quit-application-requested", null);
                return cancelQuit.data;
                """)
            if canceled:
                raise errors.MarionetteException(
                    "Something cancelled the quit application request")

        body = None
        if len(flags) > 0:
            body = {"flags": list(flags)}

        return self._send_message("Marionette:Quit",
                                  body, key="cause")

    @do_process_check
    def quit(self, clean=False, in_app=False, callback=None):
        """Terminate the currently running instance.

        This command will delete the active marionette session. It also allows
        manipulation of eg. the profile data while the application is not running.
        To start the application again, :func:`start_session` has to be called.

        :param clean: If False the same profile will be used after the next start of
                      the application. Note that the in app initiated restart always
                      maintains the same profile.
        :param in_app: If True, marionette will cause a quit from within the
                       browser. Otherwise the browser will be quit immediately
                       by killing the process.
        :param callback: If provided and `in_app` is True, the callback will
                         be used to trigger the shutdown.
        """
        if not self.instance:
            raise errors.MarionetteException("quit() can only be called "
                                             "on Gecko instances launched by Marionette")

        cause = None
        if in_app:
            if callback is not None:
                if not callable(callback):
                    raise ValueError("Specified callback '{}' is not callable".format(callback))

                self._send_message("Marionette:AcceptConnections",
                                   {"value": False})
                callback()
            else:
                cause = self._request_in_app_shutdown()

            self.delete_session(send_request=False)

            # Give the application some time to shutdown
            returncode = self.instance.runner.wait(timeout=self.DEFAULT_SHUTDOWN_TIMEOUT)
            if returncode is None:
                # This will force-close the application without sending any other message.
                self.cleanup()

                message = ("Process killed because a requested application quit did not happen "
                           "within {}s. Check gecko.log for errors.")
                raise IOError(message.format(self.DEFAULT_SHUTDOWN_TIMEOUT))

        else:
            self.delete_session()
            self.instance.close(clean=clean)

        if cause not in (None, "shutdown"):
            raise errors.MarionetteException("Unexpected shutdown reason '{}' for "
                                             "quitting the process.".format(cause))

    @do_process_check
    def restart(self, clean=False, in_app=False, callback=None):
        """
        This will terminate the currently running instance, and spawn a new instance
        with the same profile and then reuse the session id when creating a session again.

        :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.
        :param callback: If provided and `in_app` is True, the callback will be
                         used to trigger the restart.
        """
        if not self.instance:
            raise errors.MarionetteException("restart() can only be called "
                                             "on Gecko instances launched by Marionette")
        context = self._send_message("Marionette:GetContext",
                                     key="value")

        cause = None
        if in_app:
            if clean:
                raise ValueError("An in_app restart cannot be triggered with the clean flag set")

            if callback is not None:
                if not callable(callback):
                    raise ValueError("Specified callback '{}' is not callable".format(callback))

                self._send_message("Marionette:AcceptConnections",
                                   {"value": False})
                callback()
            else:
                cause = self._request_in_app_shutdown("eRestart")

            self.delete_session(send_request=False)

            try:
                timeout = self.DEFAULT_SHUTDOWN_TIMEOUT + self.DEFAULT_STARTUP_TIMEOUT
                self.raise_for_port(timeout=timeout)
            except socket.timeout:
                if self.instance.runner.returncode is not None:
                    exc, val, tb = sys.exc_info()
                    self.cleanup()
                    reraise(exc, "Requested restart of the application was aborted", tb)

        else:
            self.delete_session()
            self.instance.restart(clean=clean)
            self.raise_for_port(timeout=self.DEFAULT_STARTUP_TIMEOUT)

        if cause not in (None, "restart"):
            raise errors.MarionetteException("Unexpected shutdown reason '{}' for "
                                             "restarting the process".format(cause))

        self.start_session()
        # Restore the context as used before the restart
        self.set_context(context)

        if in_app and self.process_id:
            # In some cases Firefox restarts itself by spawning into a new process group.
            # As long as mozprocess cannot track that behavior (bug 1284864) we assist by
            # informing about the new process id.
            self.instance.runner.process_handler.check_for_detached(self.process_id)

    def absolute_url(self, relative_url):
        '''
        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.
        '''
        return "{0}{1}".format(self.baseurl, relative_url)

    @do_process_check
    def start_session(self, capabilities=None, timeout=None):
        """Create a new WebDriver session.
        This method must be called before performing any other action.

        :param capabilities: An optional dictionary of
            Marionette-recognised capabilities.  It does not
            accept a WebDriver conforming capabilities dictionary
            (including alwaysMatch, firstMatch, desiredCapabilities,
            or requriedCapabilities), and only recognises extension
            capabilities that are specific to Marionette.
        :param timeout: Optional timeout in seconds for the server to be ready.
        :returns: A dictionary of the capabilities offered.
        """
        if timeout is None:
            timeout = self.startup_timeout

        self.crashed = 0

        if self.instance:
            returncode = self.instance.runner.returncode
            # We're managing a binary which has terminated. Start it again
            # and implicitely wait for the Marionette server to be ready.
            if returncode is not None:
                self.start_binary(timeout)

        else:
            # In the case when Marionette doesn't manage the binary wait until
            # its server component has been started.
            self.raise_for_port(timeout=timeout)

        self.client = transport.TcpTransport(
            self.host,
            self.port,
            self.socket_timeout)
        self.protocol, _ = self.client.connect()

        body = capabilities
        if body is None:
            body = {}

        # Duplicate capabilities object so the body we end up
        # sending looks like this:
        #
        #     {acceptInsecureCerts: true, {capabilities: {acceptInsecureCerts: true}}}
        #
        # We do this because geckodriver sends the capabilities at the
        # top-level, and after bug 1388424 removed support for overriding
        # the session ID, we also do this with this client.  However,
        # because this client is used with older Firefoxen (through upgrade
        # tests et al.) we need to preserve backwards compatibility until
        # Firefox 60.
        if "capabilities" not in body and capabilities is not None:
            body["capabilities"] = dict(capabilities)

        resp = self._send_message("WebDriver:NewSession",
                                  body)
        self.session_id = resp["sessionId"]
        self.session = resp["capabilities"]
        # fallback to processId can be removed in Firefox 55
        self.process_id = self.session.get("moz:processID", self.session.get("processId"))
        self.profile = self.session.get("moz:profile")

        return self.session

    @property
    def test_name(self):
        return self._test_name

    @test_name.setter
    def test_name(self, test_name):
        self._test_name = test_name

    def delete_session(self, send_request=True):
        """Close the current session and disconnect from the server.

        :param send_request: Optional, if `True` a request to close the session on
            the server side will be sent. Use `False` in case of eg. in_app restart()
            or quit(), which trigger a deletion themselves. Defaults to `True`.
        """
        try:
            if send_request:
                try:
                    self._send_message("WebDriver:DeleteSession")
                except errors.InvalidSessionIdException:
                    pass
        finally:
            self.process_id = None
            self.profile = None
            self.session = None
            self.session_id = None
            self.window = None

            if self.client is not None:
                self.client.close()

    @property
    def session_capabilities(self):
        """A JSON dictionary representing the capabilities of the
        current session.

        """
        return self.session

    @property
    def current_window_handle(self):
        """Get the current window's handle.

        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
        """
        self.window = self._send_message("WebDriver:GetWindowHandle",
                                         key="value")
        return self.window

    @property
    def current_chrome_window_handle(self):
        """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
        """
        self.chrome_window = self._send_message("WebDriver:GetChromeWindowHandle",
                                                key="value")

        return self.chrome_window

    def set_window_rect(self, x=None, y=None, height=None, width=None):
        """Set the position and size of the current window.

        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 x: x coordinate for the top left of the window
        :param y: y coordinate for the top left of the window
        :param width: The width to resize the window to.
        :param height: The height to resize the window to.
        """
        if (x is None and y is None) and (height is None and width is None):
            raise errors.InvalidArgumentException("x and y or height and width need values")

        body = {"x": x, "y": y, "height": height, "width": width}
        return self._send_message("WebDriver:SetWindowRect",
                                  body)

    @property
    def window_rect(self):
        return self._send_message("WebDriver:GetWindowRect")

    @property
    def title(self):
        """Current title of the active window."""
        return self._send_message("WebDriver:GetTitle",
                                  key="value")

    @property
    def window_handles(self):
        """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
        """
        return self._send_message("WebDriver:GetWindowHandles")

    @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 chrome window handles as strings
        """
        return self._send_message("WebDriver:GetChromeWindowHandles")

    @property
    def page_source(self):
        """A string representation of the DOM."""
        return self._send_message("WebDriver:GetPageSource",
                                  key="value")

    def close(self):
        """Close the current window, ending the session if it's the last
        window currently open.

        :returns: Unordered list of remaining unique window handles as strings
        """
        return self._send_message("WebDriver:CloseWindow")

    def close_chrome_window(self):
        """Close the currently selected chrome window, ending the session
        if it's the last window open.

        :returns: Unordered list of remaining unique chrome window handles as strings
        """
        return self._send_message("WebDriver:CloseChromeWindow")

    def set_context(self, context):
        """Sets the context that Marionette commands are running in.

        :param context: Context, may be one of the class properties
            `CONTEXT_CHROME` or `CONTEXT_CONTENT`.

        Usage example::

            marionette.set_context(marionette.CONTEXT_CHROME)
        """
        if context not in [self.CONTEXT_CHROME, self.CONTEXT_CONTENT]:
            raise ValueError("Unknown context: {}".format(context))

        self._send_message("Marionette:SetContext",
                           {"value": context})

    @contextmanager
    def using_context(self, context):
        """Sets the context that Marionette commands are running in using
        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
            `CONTEXT_CHROME` or `CONTEXT_CONTENT`.

        Usage example::

            with marionette.using_context(marionette.CONTEXT_CHROME):
                # chrome scope
                ... do stuff ...
        """
        scope = self._send_message("Marionette:GetContext",
                                   key="value")
        self.set_context(context)
        try:
            yield
        finally:
            self.set_context(scope)

    def switch_to_alert(self):
        """Returns an :class:`~marionette_driver.marionette.Alert` object for
        interacting with a currently displayed alert.

        ::

            alert = self.marionette.switch_to_alert()
            text = alert.text
            alert.accept()
        """
        return Alert(self)

    def switch_to_window(self, window_id, focus=True):
        """Switch to the specified window; subsequent commands will be
        directed at the new window.

        :param window_id: The id or name of the window to switch to.

        :param focus: A boolean value which determins whether to focus
            the window that we just switched to.
        """
        self._send_message("WebDriver:SwitchToWindow",
                           {"focus": focus, "name": window_id})
        self.window = window_id

    def get_active_frame(self):
        """Returns an :class:`~marionette_driver.marionette.HTMLElement`
        representing the frame Marionette is currently acting on."""
        return self._send_message("WebDriver:GetActiveFrame",
                                  key="value")

    def switch_to_default_content(self):
        """Switch the current context to page's default content."""
        return self.switch_to_frame()

    def switch_to_parent_frame(self):
        """
           Switch to the Parent Frame
        """
        self._send_message("WebDriver:SwitchToParentFrame")

    def switch_to_frame(self, frame=None, focus=True):
        """Switch the current context to the specified frame. Subsequent
        commands will operate in the context of the specified frame,
        if applicable.

        :param frame: A reference to the frame to switch to.  This can
            be an :class:`~marionette_driver.marionette.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.
        """
        body = {"focus": focus}
        if isinstance(frame, HTMLElement):
            body["element"] = frame.id
        elif frame is not None:
            body["id"] = frame

        self._send_message("WebDriver:SwitchToFrame",
                           body)

    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 :class:`~marionette_driver.marionette.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("WebDriver:SwitchToShadowRoot",
                                  body)

    def get_url(self):
        """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
        """
        return self._send_message("WebDriver:GetCurrentURL",
                                  key="value")

    def get_window_type(self):
        """Gets the windowtype attribute of the window Marionette is
        currently acting on.

        This command only makes sense in a chrome context. You might use this
        method to distinguish a browser window from an editor window.
        """
        try:
            return self._send_message("Marionette:GetWindowType",
                                      key="value")
        except errors.UnknownCommandException:
            return self._send_message("getWindowType", key="value")

    def navigate(self, url):
        """Navigate to given `url`.

        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.

        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.

        The document is considered successfully loaded when the
        `DOMContentLoaded` event on the frame element associated with the
        `window` triggers and `document.readyState` is "complete".

        In chrome context it will change the current `window`'s location
        to the supplied URL and wait until `document.readyState` equals
        "complete" or the page timeout duration has elapsed.

        :param url: The URL to navigate to.
        """
        self._send_message("WebDriver:Navigate",
                           {"url": url})

    def go_back(self):
        """Causes the browser to perform a back navigation."""
        self._send_message("WebDriver:Back")

    def go_forward(self):
        """Causes the browser to perform a forward navigation."""
        self._send_message("WebDriver:Forward")

    def refresh(self):
        """Causes the browser to perform to refresh the current page."""
        self._send_message("WebDriver:Refresh")

    def _to_json(self, args):
        if isinstance(args, list) or isinstance(args, tuple):
            wrapped = []
            for arg in args:
                wrapped.append(self._to_json(arg))
        elif isinstance(args, dict):
            wrapped = {}
            for arg in args:
                wrapped[arg] = self._to_json(args[arg])
        elif type(args) == HTMLElement:
            wrapped = {WEB_ELEMENT_KEY: args.id,
                       CHROME_ELEMENT_KEY: args.id}
        elif (isinstance(args, bool) or isinstance(args, basestring) or
              isinstance(args, int) or isinstance(args, float) or args is None):
            wrapped = args
        return wrapped

    def _from_json(self, value):
        if isinstance(value, list):
            unwrapped = []
            for item in value:
                unwrapped.append(self._from_json(item))
            return unwrapped
        elif isinstance(value, dict):
            unwrapped = {}
            for key in value:
                if key in HTMLElement.identifiers:
                    return HTMLElement._from_json(value[key], self)
                else:
                    unwrapped[key] = self._from_json(value[key])
            return unwrapped
        else:
            return value

    def execute_script(self, script, script_args=(), new_sandbox=True,
                       sandbox="default", script_timeout=None):
        """Executes a synchronous JavaScript script, and returns the
        result (or None if the script does return a value).

        The script is executed in the context set by the most recent
        :func:`set_context` call, or to the CONTEXT_CONTENT context if
        :func:`set_context` has not been called.

        :param script: A string containing the JavaScript to execute.
        :param script_args: An interable of arguments to pass to the script.
        :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.

        Simple usage example:

        ::

            result = marionette.execute_script("return 1;")
            assert result == 1

        You can use the `script_args` parameter to pass arguments to the
        script:

        ::

            result = marionette.execute_script("return arguments[0] + arguments[1];",
                                               script_args=(2, 3,))
            assert result == 5
            some_element = marionette.find_element(By.ID, "someElement")
            sid = marionette.execute_script("return arguments[0].id;", script_args=(some_element,))
            assert some_element.get_attribute("id") == sid

        Scripts wishing to access non-standard properties of the window
        object must use window.wrappedJSObject:

        ::

            result = marionette.execute_script('''
              window.wrappedJSObject.test1 = "foo";
              window.wrappedJSObject.test2 = "bar";
              return window.wrappedJSObject.test1 + window.wrappedJSObject.test2;
              ''')
            assert result == "foobar"

        Global variables set by individual scripts do not persist between
        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:

        ::

            marionette.execute_script("global.test1 = 'foo';")
            result = self.marionette.execute_script("return global.test1;", new_sandbox=False)
            assert result == "foo"

        """
        args = self._to_json(script_args)
        stack = traceback.extract_stack()
        frame = stack[-2:-1][0]  # grab the second-to-last frame
        filename = frame[0] if sys.platform == "win32" else os.path.relpath(frame[0])
        body = {"script": script.strip(),
                "args": args,
                "newSandbox": new_sandbox,
                "sandbox": sandbox,
                "scriptTimeout": script_timeout,
                "line": int(frame[1]),
                "filename": filename}
        rv = self._send_message("WebDriver:ExecuteScript", body, key="value")
        return self._from_json(rv)

    def execute_async_script(self, script, script_args=(), new_sandbox=True,
                             sandbox="default", script_timeout=None):
        """Executes an asynchronous JavaScript script, and returns the
        result (or None if the script does return a value).

        The script is executed in the context set by the most recent
        :func:`set_context` call, or to the CONTEXT_CONTENT context if
        :func:`set_context` has not been called.

        :param script: A string containing the JavaScript to execute.
        :param script_args: An interable of arguments to pass to the script.
        :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.

        Usage example:

        ::

            marionette.timeout.script = 10
            result = self.marionette.execute_async_script('''
              // this script waits 5 seconds, and then returns the number 1
              let [resolve] = arguments;
              setTimeout(function() {
                resolve(1);
              }, 5000);
            ''')
            assert result == 1
        """
        args = self._to_json(script_args)
        stack = traceback.extract_stack()
        frame = stack[-2:-1][0]  # grab the second-to-last frame
        filename = frame[0] if sys.platform == "win32" else os.path.relpath(frame[0])
        body = {"script": script.strip(),
                "args": args,
                "newSandbox": new_sandbox,
                "sandbox": sandbox,
                "scriptTimeout": script_timeout,
                "line": int(frame[1]),
                "filename": filename}

        rv = self._send_message("WebDriver:ExecuteAsyncScript", body, key="value")
        return self._from_json(rv)

    def find_element(self, method, target, id=None):
        """Returns an :class:`~marionette_driver.marionette.HTMLElement`
        instance that matches the specified method and target in the current
        context.

        An :class:`~marionette_driver.marionette.HTMLElement` instance may be
        used to call other methods on the element, such as
        :func:`~marionette_driver.marionette.HTMLElement.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
        :attr:`marionette_driver.timeout.Timeouts.implicit`. If multiple
        elements match the given criteria, only the first is returned. If no
        element matches, a ``NoSuchElementException`` will be raised.

        :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.
        :param target: The target of the search.  For example, if method =
            "tag", target might equal "div".  If method = "id", target would
            be an element id.
        :param id: If specified, search for elements only inside the element
            with the specified id.
        """
        body = {"value": target, "using": method}
        if id:
            body["element"] = id

        return self._send_message("WebDriver:FindElement",
                                  body, key="value")

    def find_elements(self, method, target, id=None):
        """Returns a list of all
        :class:`~marionette_driver.marionette.HTMLElement` instances that match
        the specified method and target in the current context.

        An :class:`~marionette_driver.marionette.HTMLElement` instance may be
        used to call other methods on the element, such as
        :func:`~marionette_driver.marionette.HTMLElement.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
        :attr:`marionette_driver.timeout.Timeouts.implicit`.

        :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.
        :param target: The target of the search.  For example, if method =
            "tag", target might equal "div".  If method = "id", target would be
            an element id.
        :param id: If specified, search for elements only inside the element
            with the specified id.
        """
        body = {"value": target, "using": method}
        if id:
            body["element"] = id

        return self._send_message("WebDriver:FindElements",
                                  body)

    def get_active_element(self):
        el_or_ref = self._send_message("WebDriver:GetActiveElement",
                                       key="value")
        return el_or_ref

    def add_cookie(self, cookie):
        """Adds a cookie to your current session.

        :param cookie: A dictionary object, with required keys - "name"
            and "value"; optional keys - "path", "domain", "secure",
            "expiry".

        Usage example:

        ::

            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})
        """
        self._send_message("WebDriver:AddCookie",
                           {"cookie": cookie})

    def delete_all_cookies(self):
        """Delete all cookies in the scope of the current session.

        Usage example:

        ::

            driver.delete_all_cookies()
        """
        self._send_message("WebDriver:DeleteAllCookies")

    def delete_cookie(self, name):
        """Delete a cookie by its name.

        :param name: Name of cookie to delete.

        Usage example:

        ::

            driver.delete_cookie("foo")
        """
        self._send_message("WebDriver:DeleteCookie",
                           {"name": name})

    def get_cookie(self, name):
        """Get a single cookie by name. Returns the cookie if found,
        None if not.

        :param name: Name of cookie to get.
        """
        cookies = self.get_cookies()
        for cookie in cookies:
            if cookie["name"] == name:
                return cookie
        return None

    def get_cookies(self):
        """Get all the cookies for the current domain.

        This is the equivalent of calling `document.cookie` and
        parsing the result.

        :returns: A list of cookies for the current domain.
        """
        return self._send_message("WebDriver:GetCookies")

    def screenshot(self, element=None, highlights=None, format="base64",
                   full=True, scroll=True):
        """Takes a screenshot of a web element or the current frame.

        The screen capture is returned as a lossless PNG image encoded
        as a base 64 string by default. If the `element` argument is defined the
        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
            :class:`~marionette_driver.marionette.HTMLElement` objects to draw
            a red box around in the returned screenshot.

        :param format: if "base64" (the default), returns the screenshot
            as a base64-string. If "binary", the data is decoded and
            returned as raw binary. If "hash", the data is hashed using
            the SHA-256 algorithm and the result is returned as a hex digest.

        :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.

        :param scroll: When `element` is provided, scroll to it before
            taking the screenshot (default).  Otherwise, avoid scrolling
            `element` into view.
        """

        if element:
            element = element.id
        lights = None
        if highlights:
            lights = [highlight.id for highlight in highlights]

        body = {"id": element,
                "highlights": lights,
                "full": full,
                "hash": False,
                "scroll": scroll}
        if format == "hash":
            body["hash"] = True

        data = self._send_message("WebDriver:TakeScreenshot",
                                  body, key="value")

        if format == "base64" or format == "hash":
            return data
        elif format == "binary":
            return base64.b64decode(data.encode("ascii"))
        else:
            raise ValueError("format parameter must be either 'base64'"
                             " or 'binary', not {0}".format(repr(format)))

    @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.
        """
        try:
            return self._send_message("Marionette:GetScreenOrientation",
                                      key="value")
        except errors.UnknownCommandException:
            return self._send_message("getScreenOrientation", key="value")

    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.
        """
        body = {"orientation": orientation}
        try:
            self._send_message("Marionette:SetScreenOrientation", body)
        except errors.UnknownCommandException:
            self._send_message("setScreenOrientation", body)

    def minimize_window(self):
        """Iconify the browser window currently receiving commands.
        The action should be equivalent to the user pressing the minimize
        button in the OS window.

        Note that this command is not available on Fennec.  It may also
        not be available in certain window managers.

        :returns Window rect.
        """
        return self._send_message("WebDriver:MinimizeWindow")

    def maximize_window(self):
        """Resize the browser window currently receiving commands.
        The action should be equivalent to the user pressing the maximize
        button in the OS window.


        Note that this command is not available on Fennec.  It may also
        not be available in certain window managers.

        :returns: Window rect.
        """
        return self._send_message("WebDriver:MaximizeWindow")

    def fullscreen(self):
        """Synchronously sets the user agent window to full screen as
        if the user had done "View > Enter Full Screen",  or restores
        it if it is already in full screen.

        :returns: Window rect.
        """
        return self._send_message("WebDriver:FullscreenWindow")