testing/marionette/harness/marionette_harness/tests/unit/test_navigation.py
author Andreas Tolfsen <ato@mozilla.com>
Tue, 03 Jan 2017 18:45:35 +0000
changeset 480399 55fa23c4fe239af7f1d94155ce4457cd56951622
parent 468787 d859990cc3113332b24cc0088c8d75fbe5471775
child 480400 80a2d38b0b2216e3ef89975f27e2d0a764d7fb24
permissions -rw-r--r--
Bug 1322862 - Compare unicode strings in Python test. r=whimboo, a=test-only It has been known to happen that Python occassionally trips up string comparison on Windows and that this is causing intermittent issues. This patch uses unicode strings consistently so that this should not occur. MozReview-Commit-ID: BAdHfiLZzuQ

# 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/.

import contextlib
import time
import urllib

from marionette_driver import errors, By, Wait
from marionette_harness import (
    MarionetteTestCase,
    run_if_manage_instance,
    skip,
    skip_if_mobile,
    WindowManagerMixin,
)


def inline(doc):
    return "data:text/html;charset=utf-8,%s" % urllib.quote(doc)


class TestNavigate(WindowManagerMixin, MarionetteTestCase):

    def setUp(self):
        super(TestNavigate, self).setUp()

        self.marionette.navigate("about:")
        self.test_doc = self.marionette.absolute_url("test.html")
        self.iframe_doc = self.marionette.absolute_url("test_iframe.html")

    def tearDown(self):
        self.close_all_windows()

        super(TestNavigate, self).tearDown()

    @property
    def location_href(self):
        # Windows 8 has recently seen a proliferation of intermittent
        # test failures to do with failing to compare "about:blank" ==
        # u"about:blank". For the sake of consistenty, we encode the
        # returned URL as Unicode here to ensure that the values are
        # absolutely of the same type.
        #
        # (https://bugzilla.mozilla.org/show_bug.cgi?id=1322862)
        return self.marionette.execute_script("return window.location.href").encode("utf-8")

    def test_set_location_through_execute_script(self):
        self.marionette.execute_script(
            "window.location.href = '%s'" % self.test_doc)
        Wait(self.marionette).until(
            lambda _: self.test_doc == self.location_href)
        self.assertEqual("Marionette Test", self.marionette.title)

    def test_navigate(self):
        self.marionette.navigate(self.test_doc)
        self.assertNotEqual("about:", self.location_href)
        self.assertEqual("Marionette Test", self.marionette.title)

    def test_navigate_chrome_error(self):
        with self.marionette.using_context("chrome"):
            self.assertRaises(errors.UnsupportedOperationException,
                              self.marionette.navigate, "about:blank")
            self.assertRaises(errors.UnsupportedOperationException, self.marionette.go_back)
            self.assertRaises(errors.UnsupportedOperationException, self.marionette.go_forward)
            self.assertRaises(errors.UnsupportedOperationException, self.marionette.refresh)

    def test_get_current_url_returns_top_level_browsing_context_url(self):
        self.marionette.navigate(self.iframe_doc)
        self.assertEqual(self.iframe_doc, self.location_href)
        frame = self.marionette.find_element(By.CSS_SELECTOR, "#test_iframe")
        self.marionette.switch_to_frame(frame)
        self.assertEqual(self.iframe_doc, self.marionette.get_url())

    def test_get_current_url(self):
        self.marionette.navigate(self.test_doc)
        self.assertEqual(self.test_doc, self.marionette.get_url())
        self.marionette.navigate("about:blank")
        self.assertEqual("about:blank", self.marionette.get_url())

    def test_go_back(self):
        self.marionette.navigate(self.test_doc)
        self.assertNotEqual("about:blank", self.location_href)
        self.assertEqual("Marionette Test", self.marionette.title)
        self.marionette.navigate("about:blank")
        self.assertEqual("about:blank", self.location_href)
        self.marionette.go_back()
        self.assertNotEqual("about:blank", self.location_href)
        self.assertEqual("Marionette Test", self.marionette.title)

    def test_go_forward(self):
        self.marionette.navigate(self.test_doc)
        self.assertNotEqual("about:blank", self.location_href)
        self.assertEqual("Marionette Test", self.marionette.title)
        self.marionette.navigate("about:blank")
        self.assertEqual("about:blank", self.location_href)
        self.marionette.go_back()
        self.assertEqual(self.test_doc, self.location_href)
        self.assertEqual("Marionette Test", self.marionette.title)
        self.marionette.go_forward()
        self.assertEqual("about:blank", self.location_href)

    def test_refresh(self):
        self.marionette.navigate(self.test_doc)
        self.assertEqual("Marionette Test", self.marionette.title)
        self.assertTrue(self.marionette.execute_script(
            """var elem = window.document.createElement('div'); elem.id = 'someDiv';
            window.document.body.appendChild(elem); return true;"""))
        self.assertFalse(self.marionette.execute_script(
            "return window.document.getElementById('someDiv') == undefined"))
        self.marionette.refresh()
        # TODO(ato): Bug 1291320
        time.sleep(0.2)
        self.assertEqual("Marionette Test", self.marionette.title)
        self.assertTrue(self.marionette.execute_script(
            "return window.document.getElementById('someDiv') == undefined"))

    def test_navigate_in_child_frame_changes_to_top(self):
        frame_html = self.marionette.absolute_url("frameset.html")

        self.marionette.navigate(frame_html)
        frame = self.marionette.find_element(By.NAME, "third")
        self.marionette.switch_to_frame(frame)
        self.assertRaises(errors.NoSuchElementException,
                          self.marionette.find_element, By.NAME, "third")

        self.marionette.navigate(frame_html)
        self.marionette.find_element(By.NAME, "third")

    @skip_if_mobile("Bug 1323755 - Socket timeout")
    def test_invalid_protocol(self):
        with self.assertRaises(errors.MarionetteException):
            self.marionette.navigate("thisprotocoldoesnotexist://")

    def test_should_navigate_to_requested_about_page(self):
        self.marionette.navigate("about:neterror")
        self.assertEqual(self.marionette.get_url(), "about:neterror")
        self.marionette.navigate(self.marionette.absolute_url("test.html"))
        self.marionette.navigate("about:blocked")
        self.assertEqual(self.marionette.get_url(), "about:blocked")

    def test_find_element_state_complete(self):
        self.marionette.navigate(self.test_doc)
        state = self.marionette.execute_script(
            "return window.document.readyState")
        self.assertEqual("complete", state)
        self.assertTrue(self.marionette.find_element(By.ID, "mozLink"))

    def test_error_when_exceeding_page_load_timeout(self):
        with self.assertRaises(errors.TimeoutException):
            self.marionette.timeout.page_load = 0.1
            self.marionette.navigate(self.marionette.absolute_url("slow"))
            self.marionette.find_element(By.TAG_NAME, "p")

    def test_navigate_iframe(self):
        self.marionette.navigate(self.iframe_doc)
        self.assertTrue('test_iframe.html' in self.marionette.get_url())
        self.assertTrue(self.marionette.find_element(By.ID, "test_iframe"))

    def test_fragment(self):
        doc = inline("<p id=foo>")
        self.marionette.navigate(doc)
        self.marionette.execute_script("window.visited = true", sandbox=None)
        self.marionette.navigate("%s#foo" % doc)
        self.assertTrue(self.marionette.execute_script(
            "return window.visited", sandbox=None))

    @skip_if_mobile("Bug 1334095 - Timeout: No new tab has been opened")
    def test_about_blank_for_new_docshell(self):
        """ Bug 1312674 - Hang when loading about:blank for a new docshell."""
        def open_with_link():
            link = self.marionette.find_element(By.ID, "new-blank-tab")
            link.click()

        # Open a new tab to get a new docshell created
        self.marionette.navigate(self.marionette.absolute_url("windowHandles.html"))
        new_tab = self.open_tab(trigger=open_with_link)
        self.marionette.switch_to_window(new_tab)
        self.assertEqual(self.marionette.get_url(), "about:blank")

        self.marionette.navigate('about:blank')
        self.marionette.close()
        self.marionette.switch_to_window(self.start_window)

    def test_error_on_tls_navigation(self):
        self.assertRaises(errors.InsecureCertificateException,
                          self.marionette.navigate, self.fixtures.where_is("/test.html", on="https"))

    def test_html_document_to_image_document(self):
        self.marionette.navigate(self.fixtures.where_is("test.html"))
        self.marionette.navigate(self.fixtures.where_is("white.png"))
        self.assertIn("white.png", self.marionette.title)

    def test_image_document_to_image_document(self):
        self.marionette.navigate(self.fixtures.where_is("test.html"))

        self.marionette.navigate(self.fixtures.where_is("white.png"))
        self.assertIn("white.png", self.marionette.title)
        self.marionette.navigate(self.fixtures.where_is("black.png"))
        self.assertIn("black.png", self.marionette.title)

    @run_if_manage_instance("Only runnable if Marionette manages the instance")
    @skip_if_mobile("Bug 1322993 - Missing temporary folder")
    def test_focus_after_navigation(self):
        self.marionette.quit()
        self.marionette.start_session()

        self.marionette.navigate(inline("<input autofocus>"))
        active_el = self.marionette.execute_script("return document.activeElement")
        focus_el = self.marionette.find_element(By.CSS_SELECTOR, ":focus")
        self.assertEqual(active_el, focus_el)


class TestTLSNavigation(MarionetteTestCase):
    insecure_tls = {"acceptInsecureCerts": True}
    secure_tls = {"acceptInsecureCerts": False}

    def setUp(self):
        MarionetteTestCase.setUp(self)
        self.marionette.delete_session()
        self.capabilities = self.marionette.start_session(
            {"requiredCapabilities": self.insecure_tls})

    def tearDown(self):
        try:
            self.marionette.delete_session()
        except:
            pass
        MarionetteTestCase.tearDown(self)

    @contextlib.contextmanager
    def safe_session(self):
        try:
            self.capabilities = self.marionette.start_session(
                {"requiredCapabilities": self.secure_tls})
            self.assertFalse(self.capabilities["acceptInsecureCerts"])
            yield self.marionette
        finally:
            self.marionette.delete_session()

    @contextlib.contextmanager
    def unsafe_session(self):
        try:
            self.capabilities = self.marionette.start_session(
                {"requiredCapabilities": self.insecure_tls})
            self.assertTrue(self.capabilities["acceptInsecureCerts"])
            yield self.marionette
        finally:
            self.marionette.delete_session()

    def test_navigate_by_command(self):
        self.marionette.navigate(
            self.fixtures.where_is("/test.html", on="https"))
        self.assertIn("https", self.marionette.get_url())

    def test_navigate_by_click(self):
        link_url = self.fixtures.where_is("/test.html", on="https")
        self.marionette.navigate(
            inline("<a href=%s>https is the future</a>" % link_url))
        self.marionette.find_element(By.TAG_NAME, "a").click()
        self.assertIn("https", self.marionette.get_url())

    def test_deactivation(self):
        invalid_cert_url = self.fixtures.where_is("/test.html", on="https")

        print "with safe session"
        with self.safe_session() as session:
            with self.assertRaises(errors.InsecureCertificateException):
                session.navigate(invalid_cert_url)

        print "with unsafe session"
        with self.unsafe_session() as session:
            session.navigate(invalid_cert_url)

        print "with safe session again"
        with self.safe_session() as session:
            with self.assertRaises(errors.InsecureCertificateException):
                session.navigate(invalid_cert_url)