def test_nonce(self):

        # make a profile with one preference
        path = tempfile.mktemp()
        profile = Profile(path,
                          preferences={'foo': 'bar'},
                          restore=False)
        user_js = os.path.join(profile.profile, 'user.js')
        self.assertTrue(os.path.exists(user_js))

        # ensure the preference is correct
        prefs = Preferences.read_prefs(user_js)
        self.assertEqual(dict(prefs), {'foo': 'bar'})

        del profile

        # augment the profile with a second preference
        profile = Profile(path,
                          preferences={'fleem': 'baz'},
                          restore=True)
        prefs = Preferences.read_prefs(user_js)
        self.assertEqual(dict(prefs), {'foo': 'bar', 'fleem': 'baz'})

        # cleanup the profile;
        # this should remove the new preferences but not the old
        profile.cleanup()
        prefs = Preferences.read_prefs(user_js)
        self.assertEqual(dict(prefs), {'foo': 'bar'})
    def test_reset_should_remove_added_prefs(self):
        """Check that when we call reset the items we expect are updated"""

        profile = Profile()
        prefs_file = os.path.join(profile.profile, 'user.js')

        # we shouldn't have any initial preferences
        initial_prefs = Preferences.read_prefs(prefs_file)
        self.assertFalse(initial_prefs)
        initial_prefs = file(prefs_file).read().strip()
        self.assertFalse(initial_prefs)

        # add some preferences
        prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
        profile.set_preferences(prefs1)
        self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue(bool([line for line in lines
                              if line.startswith('#MozRunner Prefs Start')]))
        self.assertTrue(bool([line for line in lines
                              if line.startswith('#MozRunner Prefs End')]))

        profile.reset()
        self.assertNotEqual(prefs1,
                            Preferences.read_prefs(os.path.join(profile.profile, 'user.js')),
                            "I pity the fool who left my pref")
Example #3
0
def profile(tmpdir):
    # make a profile with one preference
    path = tmpdir.mkdtemp().strpath
    profile = Profile(path, preferences={"foo": "bar"}, restore=False)
    user_js = os.path.join(profile.profile, "user.js")
    assert os.path.exists(user_js)
    return profile
 def setUp(self):
     # make a profile with one preference
     path = tempfile.mktemp()
     self.addCleanup(mozfile.remove, path)
     self.profile = Profile(path, preferences={'foo': 'bar'}, restore=False)
     user_js = os.path.join(self.profile.profile, 'user.js')
     self.assertTrue(os.path.exists(user_js))
Example #5
0
    def _init_profile(self):
        preferences = dict(self.browser_config['preferences'])
        if self.test_config.get('preferences'):
            test_prefs = dict(
                [(i, utils.parse_pref(j))
                 for i, j in self.test_config['preferences'].items()]
            )
            preferences.update(test_prefs)
        # interpolate webserver value in prefs
        webserver = self.browser_config['webserver']
        if '://' not in webserver:
            webserver = 'http://' + webserver
        for name, value in preferences.items():
            if type(value) is str:
                value = utils.interpolate(value, webserver=webserver)
                preferences[name] = value

        extensions = self.browser_config['extensions'][:]
        if self.test_config.get('extensions'):
            extensions.append(self.test_config['extensions'])

        profile = Profile.clone(
            os.path.normpath(self.test_config['profile_path']),
            self.profile_dir,
            restore=False)

        profile.set_preferences(preferences)
        profile.addon_manager.install_addons(extensions)
Example #6
0
    def _init_profile(self):
        preferences = dict(self.browser_config['preferences'])
        if self.test_config.get('preferences'):
            test_prefs = dict([
                (i, utils.parse_pref(j))
                for i, j in self.test_config['preferences'].items()
            ])
            preferences.update(test_prefs)
        # interpolate webserver value in prefs
        webserver = self.browser_config['webserver']
        if '://' not in webserver:
            webserver = 'http://' + webserver
        for name, value in preferences.items():
            if type(value) is str:
                value = utils.interpolate(value, webserver=webserver)
                preferences[name] = value

        extensions = self.browser_config['extensions'][:]
        if self.test_config.get('extensions'):
            extensions.append(self.test_config['extensions'])

        if self.browser_config['develop'] or \
           self.browser_config['branch_name'] == 'Try':
            extensions = [os.path.dirname(i) for i in extensions]

        profile = Profile.clone(os.path.normpath(
            self.test_config['profile_path']),
                                self.profile_dir,
                                restore=False)

        profile.set_preferences(preferences)
        profile.addon_manager.install_addons(extensions)
 def test_cleanup_on_garbage_collected(self):
     clone = Profile.clone(self.profile.profile)
     profile_dir = clone.profile
     self.assertTrue(os.path.exists(profile_dir))
     del clone
     # clone should be deleted
     self.assertFalse(os.path.exists(profile_dir))
Example #8
0
    def test_restore_true(self):
        # make a clone of this profile with restore=True
        clone = Profile.clone(self.profile.profile, restore=True)

        clone.cleanup()

        # clone should be deleted
        self.assertFalse(os.path.exists(clone.profile))
Example #9
0
    def test_restore_false(self):
        # make a clone of this profile with restore=False
        clone = Profile.clone(self.profile.profile, restore=False)

        clone.cleanup()

        # clone should still be around on the filesystem
        self.assertTrue(os.path.exists(clone.profile))
Example #10
0
 def test_cleanup_on_garbage_collected(self):
     clone = Profile.clone(self.profile.profile)
     self.addCleanup(mozfile.remove, clone.profile)
     profile_dir = clone.profile
     self.assertTrue(os.path.exists(profile_dir))
     del clone
     # clone should be deleted
     self.assertFalse(os.path.exists(profile_dir))
    def test_restore_false(self):
        # make a clone of this profile with restore=False
        clone = Profile.clone(self.profile.profile, restore=False)

        clone.cleanup()

        # clone should still be around on the filesystem
        self.assertTrue(os.path.exists(clone.profile))
    def test_restore_true(self):
        # make a clone of this profile with restore=True
        clone = Profile.clone(self.profile.profile, restore=True)

        clone.cleanup()

        # clone should be deleted
        self.assertFalse(os.path.exists(clone.profile))
Example #13
0
def test_cleanup_on_garbage_collected(profile):
    clone = Profile.clone(profile.profile)
    profile_dir = clone.profile
    assert os.path.exists(profile_dir)
    del clone

    # clone should be deleted
    assert not os.path.exists(profile_dir)
Example #14
0
def test_preexisting_preferences():
    """ensure you don't clobber preexisting preferences"""

    # make a pretend profile
    tempdir = tempfile.mkdtemp()

    try:
        # make a user.js
        contents = """
user_pref("webgl.enabled_for_all_sites", true);
user_pref("webgl.force-enabled", true);
"""
        user_js = os.path.join(tempdir, "user.js")
        f = open(user_js, "w")
        f.write(contents)
        f.close()

        # make sure you can read it
        prefs = Preferences.read_prefs(user_js)
        original_prefs = [
            ("webgl.enabled_for_all_sites", True),
            ("webgl.force-enabled", True),
        ]
        assert prefs == original_prefs

        # now read this as a profile
        profile = Profile(
            tempdir, preferences={"browser.download.dir": "/home/jhammel"})

        # make sure the new pref is now there
        new_prefs = original_prefs[:] + [
            ("browser.download.dir", "/home/jhammel")
        ]
        prefs = Preferences.read_prefs(user_js)
        assert prefs == new_prefs

        # clean up the added preferences
        profile.cleanup()
        del profile

        # make sure you have the original preferences
        prefs = Preferences.read_prefs(user_js)
        assert prefs == original_prefs
    finally:
        shutil.rmtree(tempdir)
Example #15
0
def test_restore_false(profile):
    # make a clone of this profile with restore=False
    clone = Profile.clone(profile.profile, restore=False)
    try:
        clone.cleanup()

        # clone should still be around on the filesystem
        assert os.path.exists(clone.profile)
    finally:
        mozfile.remove(clone.profile)
Example #16
0
    def start_firefox(self):
        env = os.environ.copy()
        env['MOZ_CRASHREPORTER_NO_REPORT'] = '1'

        profile = Profile()
        profile.set_preferences({"marionette.defaultPrefs.enabled": True,
                                 "marionette.defaultPrefs.port": self.marionette_port,
                                 "dom.disable_open_during_load": False,
                                 "dom.max_script_run_time": 0})

        self.firefox_runner = FirefoxRunner(profile,
                                            self.firefox_binary,
                                            cmdargs=["--marionette"],
                                            env=env,
                                            kp_kwargs = {"processOutputLine":[self.on_output]},
                                            process_class=self.process_cls)
        self.logger.debug("Starting Firefox")
        self.firefox_runner.start()
        self.logger.debug("Firefox Started")
Example #17
0
    def test_magic_markers(self):
        """ensure our magic markers are working"""

        profile = Profile()
        prefs_file = os.path.join(profile.profile, 'user.js')

        # we shouldn't have any initial preferences
        initial_prefs = Preferences.read_prefs(prefs_file)
        self.assertFalse(initial_prefs)
        initial_prefs = file(prefs_file).read().strip()
        self.assertFalse(initial_prefs)

        # add some preferences
        prefs1 = [("browser.startup.homepage", "http://planet.mozilla.org/"),
                  ("zoom.minPercent", 30)]
        profile.set_preferences(prefs1)
        self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue(
            bool([
                line for line in lines
                if line.startswith('#MozRunner Prefs Start')
            ]))
        self.assertTrue(
            bool([
                line for line in lines
                if line.startswith('#MozRunner Prefs End')
            ]))

        # add some more preferences
        prefs2 = [("zoom.maxPercent", 300), ("webgl.verbose", 'false')]
        profile.set_preferences(prefs2)
        self.assertEqual(prefs1 + prefs2, Preferences.read_prefs(prefs_file))
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue(
            len([
                line for line in lines
                if line.startswith('#MozRunner Prefs Start')
            ]) == 2)
        self.assertTrue(
            len([
                line for line in lines
                if line.startswith('#MozRunner Prefs End')
            ]) == 2)

        # now clean it up
        profile.clean_preferences()
        final_prefs = Preferences.read_prefs(prefs_file)
        self.assertFalse(final_prefs)
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue('#MozRunner Prefs Start' not in lines)
        self.assertTrue('#MozRunner Prefs End' not in lines)
Example #18
0
    def test_reset_should_remove_added_prefs(self):
        """Check that when we call reset the items we expect are updated"""

        profile = Profile()
        prefs_file = os.path.join(profile.profile, 'user.js')

        # we shouldn't have any initial preferences
        initial_prefs = Preferences.read_prefs(prefs_file)
        self.assertFalse(initial_prefs)
        initial_prefs = file(prefs_file).read().strip()
        self.assertFalse(initial_prefs)

        # add some preferences
        prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
        profile.set_preferences(prefs1)
        self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue(
            bool([
                line for line in lines
                if line.startswith('#MozRunner Prefs Start')
            ]))
        self.assertTrue(
            bool([
                line for line in lines
                if line.startswith('#MozRunner Prefs End')
            ]))

        profile.reset()
        self.assertNotEqual(
            prefs1,
            Preferences.read_prefs(os.path.join(profile.profile, 'user.js')),
            "I pity the fool who left my pref")
Example #19
0
    def test_nonce(self):

        # make a profile with one preference
        path = tempfile.mktemp()
        self.addCleanup(mozfile.remove, path)
        profile = Profile(path, preferences={'foo': 'bar'}, restore=False)
        user_js = os.path.join(profile.profile, 'user.js')
        self.assertTrue(os.path.exists(user_js))

        # ensure the preference is correct
        prefs = Preferences.read_prefs(user_js)
        self.assertEqual(dict(prefs), {'foo': 'bar'})

        del profile

        # augment the profile with a second preference
        profile = Profile(path, preferences={'fleem': 'baz'}, restore=True)
        prefs = Preferences.read_prefs(user_js)
        self.assertEqual(dict(prefs), {'foo': 'bar', 'fleem': 'baz'})

        # cleanup the profile;
        # this should remove the new preferences but not the old
        profile.cleanup()
        prefs = Preferences.read_prefs(user_js)
        self.assertEqual(dict(prefs), {'foo': 'bar'})
Example #20
0
    def test_preexisting_preferences(self):
        """ensure you don't clobber preexisting preferences"""

        # make a pretend profile
        tempdir = tempfile.mkdtemp()

        try:
            # make a user.js
            contents = """
user_pref("webgl.enabled_for_all_sites", true);
user_pref("webgl.force-enabled", true);
"""
            user_js = os.path.join(tempdir, 'user.js')
            f = file(user_js, 'w')
            f.write(contents)
            f.close()

            # make sure you can read it
            prefs = Preferences.read_prefs(user_js)
            original_prefs = [('webgl.enabled_for_all_sites', True), ('webgl.force-enabled', True)]
            self.assertTrue(prefs == original_prefs)

            # now read this as a profile
            profile = Profile(tempdir, preferences={"browser.download.dir": "/home/jhammel"})

            # make sure the new pref is now there
            new_prefs = original_prefs[:] + [("browser.download.dir", "/home/jhammel")]
            prefs = Preferences.read_prefs(user_js)
            self.assertTrue(prefs == new_prefs)

            # clean up the added preferences
            profile.cleanup()
            del profile

            # make sure you have the original preferences
            prefs = Preferences.read_prefs(user_js)
            self.assertTrue(prefs == original_prefs)
        except:
            shutil.rmtree(tempdir)
            raise
Example #21
0
    def _init_profile(self):
        preferences = dict(self.browser_config['preferences'])
        if self.test_config.get('preferences'):
            test_prefs = dict([
                (i, utils.parse_pref(j))
                for i, j in self.test_config['preferences'].items()
            ])
            preferences.update(test_prefs)
        # interpolate webserver value in prefs
        webserver = self.browser_config['webserver']
        if '://' not in webserver:
            webserver = 'http://' + webserver
        for name, value in preferences.items():
            if type(value) is str:
                value = utils.interpolate(value, webserver=webserver)
                preferences[name] = value

        extensions = self.browser_config['extensions'][:]
        if self.test_config.get('extensions'):
            extensions.append(self.test_config['extensions'])

        # downloading a profile instead of using the empty one
        if self.test_config['profile'] is not None:
            path = heavy.download_profile(self.test_config['profile'])
            self.test_config['profile_path'] = path

        profile = Profile.clone(os.path.normpath(
            self.test_config['profile_path']),
                                self.profile_dir,
                                restore=False)

        profile.set_preferences(preferences)

        # installing addons
        profile.addon_manager.install_addons(extensions)

        # installing webextensions
        webextensions = self.test_config.get('webextensions', None)
        if isinstance(webextensions, basestring):
            webextensions = [webextensions]

        if webextensions is not None:
            for webext in webextensions:
                filename = utils.interpolate(webext)
                if mozinfo.os == 'win':
                    filename = filename.replace('/', '\\')
                if not filename.endswith('.xpi'):
                    continue
                if not os.path.exists(filename):
                    continue

                profile.addon_manager.install_from_path(filename)
Example #22
0
def create_profile(addons, pref_categories, profile=None):
    from mozprofile.prefs import Preferences
    from mozprofile.profile import Profile
    from subprocess import check_output

    prefs = Preferences()
    for category in pref_categories:
        prefs.add_file("{}:{}".format(C.MOZRUNNER_PREFS_INI, category))

    return Profile(
        addons=addons,
        preferences=prefs(),
        restore=False,
    )
Example #23
0
def test_magic_markers():
    """ensure our magic markers are working"""

    profile = Profile()
    prefs_file = os.path.join(profile.profile, "user.js")

    # we shouldn't have any initial preferences
    initial_prefs = Preferences.read_prefs(prefs_file)
    assert not initial_prefs
    initial_prefs = open(prefs_file).read().strip()
    assert not initial_prefs

    # add some preferences
    prefs1 = [
        ("browser.startup.homepage", "http://planet.mozilla.org/"),
        ("zoom.minPercent", 30),
    ]
    profile.set_preferences(prefs1)
    assert prefs1 == Preferences.read_prefs(prefs_file)
    lines = open(prefs_file).read().strip().splitlines()
    assert bool(
        [line for line in lines if line.startswith("#MozRunner Prefs Start")])
    assert bool(
        [line for line in lines if line.startswith("#MozRunner Prefs End")])

    # add some more preferences
    prefs2 = [("zoom.maxPercent", 300), ("webgl.verbose", "false")]
    profile.set_preferences(prefs2)
    assert prefs1 + prefs2 == Preferences.read_prefs(prefs_file)
    lines = open(prefs_file).read().strip().splitlines()
    assert (len([
        line for line in lines if line.startswith("#MozRunner Prefs Start")
    ]) == 2)
    assert len([
        line for line in lines if line.startswith("#MozRunner Prefs End")
    ]) == 2

    # now clean it up
    profile.clean_preferences()
    final_prefs = Preferences.read_prefs(prefs_file)
    assert not final_prefs
    lines = open(prefs_file).read().strip().splitlines()
    assert "#MozRunner Prefs Start" not in lines
    assert "#MozRunner Prefs End" not in lines
    def test_magic_markers(self):
        """ensure our magic markers are working"""

        profile = Profile()
        prefs_file = os.path.join(profile.profile, 'user.js')

        # we shouldn't have any initial preferences
        initial_prefs = Preferences.read_prefs(prefs_file)
        self.assertFalse(initial_prefs)
        initial_prefs = file(prefs_file).read().strip()
        self.assertFalse(initial_prefs)

        # add some preferences
        prefs1 = [("browser.startup.homepage", "http://planet.mozilla.org/"),
                   ("zoom.minPercent", 30)]
        profile.set_preferences(prefs1)
        self.assertEqual(prefs1, Preferences.read_prefs(prefs_file))
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue(bool([line for line in lines
                              if line.startswith('#MozRunner Prefs Start')]))
        self.assertTrue(bool([line for line in lines
                              if line.startswith('#MozRunner Prefs End')]))

        # add some more preferences
        prefs2 = [("zoom.maxPercent", 300),
                   ("webgl.verbose", 'false')]
        profile.set_preferences(prefs2)
        self.assertEqual(prefs1 + prefs2, Preferences.read_prefs(prefs_file))
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue(len([line for line in lines
                             if line.startswith('#MozRunner Prefs Start')]) == 2)
        self.assertTrue(len([line for line in lines
                             if line.startswith('#MozRunner Prefs End')]) == 2)

        # now clean it up
        profile.clean_preferences()
        final_prefs = Preferences.read_prefs(prefs_file)
        self.assertFalse(final_prefs)
        lines = file(prefs_file).read().strip().splitlines()
        self.assertTrue('#MozRunner Prefs Start' not in lines)
        self.assertTrue('#MozRunner Prefs End' not in lines)
Example #25
0
    def test_restore_true(self):
        counter = [0]

        def _feedback(dir, content):
            # Called by shutil.copytree on each visited directory.
            # Used here to display info.
            #
            # Returns the items that should be ignored by
            # shutil.copytree when copying the tree, so always returns
            # an empty list.
            counter[0] += 1
            return []

        # make a clone of this profile with restore=True
        clone = Profile.clone(self.profile.profile, restore=True,
                              ignore=_feedback)
        self.addCleanup(mozfile.remove, clone.profile)

        clone.cleanup()

        # clone should be deleted
        self.assertFalse(os.path.exists(clone.profile))
        self.assertTrue(counter[0] > 0)
Example #26
0
def test_restore_true(profile):
    counter = [0]

    def _feedback(dir, content):
        # Called by shutil.copytree on each visited directory.
        # Used here to display info.
        #
        # Returns the items that should be ignored by
        # shutil.copytree when copying the tree, so always returns
        # an empty list.
        counter[0] += 1
        return []

    # make a clone of this profile with restore=True
    clone = Profile.clone(profile.profile, restore=True, ignore=_feedback)
    try:
        clone.cleanup()

        # clone should be deleted
        assert not os.path.exists(clone.profile)
        assert counter[0] > 0
    finally:
        mozfile.remove(clone.profile)
Example #27
0
def test_reset_should_keep_user_added_prefs():
    """Check that when we call reset the items we expect are updated"""
    profile = Profile()
    prefs_file = os.path.join(profile.profile, 'user.js')

    # we shouldn't have any initial preferences
    initial_prefs = Preferences.read_prefs(prefs_file)
    assert not initial_prefs
    initial_prefs = open(prefs_file).read().strip()
    assert not initial_prefs

    # add some preferences
    prefs1 = [("mr.t.quotes", "i aint getting on no plane!")]
    profile.set_persistent_preferences(prefs1)
    assert prefs1 == Preferences.read_prefs(prefs_file)
    lines = open(prefs_file).read().strip().splitlines()
    assert any(line.startswith('#MozRunner Prefs Start') for line in lines)
    assert any(line.startswith('#MozRunner Prefs End') for line in lines)

    profile.reset()
    assert prefs1 == Preferences.read_prefs(os.path.join(profile.profile, 'user.js'))
Example #28
0
def test_nonce(tmpdir):
    # make a profile with one preference
    path = tmpdir.strpath
    profile = Profile(path, preferences={"foo": "bar"}, restore=False)
    user_js = os.path.join(profile.profile, "user.js")
    assert os.path.exists(user_js)

    # ensure the preference is correct
    prefs = Preferences.read_prefs(user_js)
    assert dict(prefs) == {"foo": "bar"}

    del profile

    # augment the profile with a second preference
    profile = Profile(path, preferences={"fleem": "baz"}, restore=True)
    prefs = Preferences.read_prefs(user_js)
    assert dict(prefs) == {"foo": "bar", "fleem": "baz"}

    # cleanup the profile;
    # this should remove the new preferences but not the old
    profile.cleanup()
    prefs = Preferences.read_prefs(user_js)
    assert dict(prefs) == {"foo": "bar"}
Example #29
0
    def _init_profile(self):
        preferences = dict(self.browser_config['preferences'])
        if self.test_config.get('preferences'):
            test_prefs = dict(
                [(i, utils.parse_pref(j))
                 for i, j in self.test_config['preferences'].items()]
            )
            preferences.update(test_prefs)
        # interpolate webserver value in prefs
        webserver = self.browser_config['webserver']
        if '://' not in webserver:
            webserver = 'http://' + webserver
        for name, value in preferences.items():
            if type(value) is str:
                value = utils.interpolate(value, webserver=webserver)
                preferences[name] = value

        extensions = self.browser_config['extensions'][:]
        if self.test_config.get('extensions'):
            extensions.append(self.test_config['extensions'])

        # downloading a profile instead of using the empty one
        if self.test_config['profile'] is not None:
            path = heavy.download_profile(self.test_config['profile'])
            self.test_config['profile_path'] = path

        profile_path = os.path.normpath(self.test_config['profile_path'])
        LOG.info("Cloning profile located at %s" % profile_path)

        def _feedback(directory, content):
            # Called by shutil.copytree on each visited directory.
            # Used here to display info.
            #
            # Returns the items that should be ignored by
            # shutil.copytree when copying the tree, so always returns
            # an empty list.
            sub = directory.split(profile_path)[-1].lstrip("/")
            if sub:
                LOG.info("=> %s" % sub)
            return []

        profile = Profile.clone(profile_path,
                                self.profile_dir,
                                ignore=_feedback,
                                restore=False)

        profile.set_preferences(preferences)

        # installing addons
        LOG.info("Installing Add-ons")
        profile.addon_manager.install_addons(extensions)

        # installing webextensions
        webextensions = self.test_config.get('webextensions', None)
        if isinstance(webextensions, basestring):
            webextensions = [webextensions]

        if webextensions is not None:
            LOG.info("Installing Webextensions")
            for webext in webextensions:
                filename = utils.interpolate(webext)
                if mozinfo.os == 'win':
                    filename = filename.replace('/', '\\')
                if not filename.endswith('.xpi'):
                    continue
                if not os.path.exists(filename):
                    continue

                profile.addon_manager.install_from_path(filename)
Example #30
0
    def _init_profile(self):
        extensions = self.browser_config['extensions'][:]
        if self.test_config.get('extensions'):
            extensions.extend(self.test_config['extensions'])

        # downloading a profile instead of using the empty one
        if self.test_config['profile'] is not None:
            path = heavy.download_profile(self.test_config['profile'])
            self.test_config['profile_path'] = path

        profile_path = os.path.normpath(self.test_config['profile_path'])
        LOG.info("Cloning profile located at %s" % profile_path)

        def _feedback(directory, content):
            # Called by shutil.copytree on each visited directory.
            # Used here to display info.
            #
            # Returns the items that should be ignored by
            # shutil.copytree when copying the tree, so always returns
            # an empty list.
            sub = directory.split(profile_path)[-1].lstrip("/")
            if sub:
                LOG.info("=> %s" % sub)
            return []

        profile = Profile.clone(profile_path,
                                self.profile_dir,
                                ignore=_feedback,
                                restore=False)

        # build pref interpolation context
        webserver = self.browser_config['webserver']
        if '://' not in webserver:
            webserver = 'http://' + webserver

        interpolation = {
            'webserver': webserver,
        }

        # merge base profiles
        with open(os.path.join(self.profile_data_dir, 'profiles.json'),
                  'r') as fh:
            base_profiles = json.load(fh)['talos']

        for name in base_profiles:
            path = os.path.join(self.profile_data_dir, name)
            LOG.info("Merging profile: {}".format(path))
            profile.merge(path, interpolation=interpolation)

        # set test preferences
        preferences = self.browser_config.get('preferences', {}).copy()
        if self.test_config.get('preferences'):
            test_prefs = dict([
                (i, utils.parse_pref(j))
                for i, j in self.test_config['preferences'].items()
            ])
            preferences.update(test_prefs)

        for name, value in preferences.items():
            if type(value) is str:
                value = utils.interpolate(value, **interpolation)
                preferences[name] = value
        profile.set_preferences(preferences)

        # installing addons
        LOG.info("Installing Add-ons:")
        LOG.info(extensions)
        profile.addons.install(extensions)

        # installing webextensions
        webextensions = self.test_config.get('webextensions', None)
        if isinstance(webextensions, basestring):
            webextensions = [webextensions]

        if webextensions is not None:
            LOG.info("Installing Webextensions:")
            for webext in webextensions:
                filename = utils.interpolate(webext)
                if mozinfo.os == 'win':
                    filename = filename.replace('/', '\\')
                if not filename.endswith('.xpi'):
                    continue
                if not os.path.exists(filename):
                    continue
                LOG.info(filename)
                profile.addons.install(filename)
    def do_POST(self):
        try:

            path, body, session, element = self.process_request()
            logger.debug("%s - %s - %s - %s" % (path, body, session, element))

            if path == "/back":
                logger.info("Navigating back - %s" % session)
                assert session
                assert self.server.marionette.go_back()
                self.send_JSON(session=session)
            elif path == "/clear":
                logger.info("Clearing %s - %s" % (element, session))
                assert session
                marionette_element = HTMLElement(self.server.marionette, element)
                marionette_element.clear()
                self.send_JSON(session=session)
            elif path == "/click":
                logger.info("Clicking %s - %s" % (element, session))
                assert session
                marionette_element = HTMLElement(self.server.marionette, element)
                marionette_element.click()
                self.send_JSON(session=session)
            elif path == "/element":
                logger.info("Find Element using - %s, value - %s  - %s" % (body["using"], body["value"], session))
                # find element variants
                assert session
                self.send_JSON(
                    session=session,
                    value={"ELEMENT": self.server.marionette.find_element(body["using"], body["value"], id=element).id},
                )
            elif path == "/elements":
                logger.info("Find Elements using - %s, value - %s  - %s" % (body["using"], body["value"], session))
                # find elements variants
                assert session
                self.send_JSON(
                    session=session,
                    value=[
                        {"ELEMENT": x.id} for x in self.server.marionette.find_elements(body["using"], body["value"])
                    ],
                )
            elif path == "/execute":
                logger.info("Executing Script - %s" % session)
                assert session
                if body["args"]:
                    result = self.server.marionette.execute_script(
                        body["script"], script_args=body["args"], new_sandbox=False
                    )
                else:
                    result = self.server.marionette.execute_script(body["script"], new_sandbox=False)
                self.send_JSON(session=session, value=result)
            elif path == "/execute_async":
                logger.info("Executing Async Script - %s" % session)
                assert session
                result = None
                if body["args"]:
                    result = self.server.marionette.execute_async_script(
                        body["script"], script_args=body["args"], new_sandbox=False
                    )
                else:
                    result = self.server.marionette.execute_async_script(body["script"], new_sandbox=False)
                self.send_JSON(session=session, value=result)
            elif path == "/forward":
                logger.info("Forwarding - %s" % session)
                assert session
                assert self.server.marionette.go_forward()
                self.send_JSON(session=session)
            elif path == "/frame":
                logger.info("Switch to Frame %s - %s" % (body["id"], session))
                assert session
                frame = body["id"]
                if isinstance(frame, dict) and "ELEMENT" in frame:
                    frame = HTMLElement(self.server.marionette, frame["ELEMENT"])
                assert self.server.marionette.switch_to_frame(frame)
                self.send_JSON(session=session)
            elif path == "/refresh":
                logger.info("Refreshing the page - %s" % session)
                assert session
                assert self.server.marionette.refresh()
                self.send_JSON(session=session)
            elif path == "/session":
                logger.info("Creating new session")
                logger.debug("loading webdriver prefs")
                with open("webdriver.json") as webpref:
                    read_prefs = webpref.read()

                prefs = json.loads(read_prefs)
                port = free_port()

                logger.debug("Creating Profile")
                profile = Profile()
                profile.set_preferences(prefs["frozen"])
                profile.set_preferences(prefs["mutable"])
                profile.set_preferences({"marionette.defaultPrefs.enabled": True, "marionette.defaultPrefs.port": port})

                logger.debug("Profile created at %s" % profile.profile)
                logger.debug("Creating runner")
                firefox_binary = (
                    body["desiredCapabilities"]["firefoxBinary"]
                    if body["desiredCapabilities"]["firefoxBinary"]
                    else firefox_binary_path()
                )
                self.server.runner = FirefoxRunner(profile, firefox_binary)
                self.server.runner.start()
                logger.debug("Browser has been started")
                logger.info("Creating Marionette instance on %s:%s" % ("localhost", port))
                self.server.marionette = Marionette("localhost", port)
                self.server.marionette.wait_for_port()
                session = self.server.marionette.start_session()
                self.send_JSON(session=session, value={})
                # 'value' is the browser capabilities, which we're ignoring for now
            elif path == "/timeouts/async_script":
                logger.info("Script timeout %s ms - %s" % (body["ms"], session))
                assert session
                assert self.server.marionette.set_script_timeout(body["ms"])
                self.send_JSON(session=session)
            elif path == "/timeouts/implicit_wait":
                logger.info("Implicit timeout %s ms - %s" % (body["ms"], session))
                assert session
                assert self.server.marionette.set_search_timeout(body["ms"])
                self.send_JSON(session=session)
            elif path == "/url":
                logger.info("Navigating to %s - %s" % (body["url"], session))
                assert session
                assert self.server.marionette.navigate(body["url"])
                self.send_JSON(session=session)
            elif path == "/value":
                logger.info("Send Keys %s - %s" % ("".join(body["value"]), session))
                assert session
                keys = "".join(body["value"])
                marionette_element = HTMLElement(self.server.marionette, element)
                assert marionette_element.send_keys(keys)
                self.send_JSON(session=session)
            elif path == "/window":
                logger.info("Switch to Window %s - %s" % (body["name"], session))
                assert session
                assert self.server.marionette.switch_to_window(body["name"])
                self.send_JSON(session=session)
            else:
                logger.error("Unknown path - %s" % session)
                self.file_not_found()

        except MarionetteException as e:
            logger.error("Status: %s - Message: %s" % (e.status, e.message))
            self.send_JSON(data={"status": e.status}, value={"message": e.message})
        except:
            trace_ = traceback.format_exc()
            logger.critical("Server Exception: %s" % trace_)
            self.server_error(trace_)
Example #32
0
    def _init_profile(self):
        extensions = self.browser_config["extensions"][:]
        if self.test_config.get("extensions"):
            extensions.extend(self.test_config["extensions"])

        # downloading a profile instead of using the empty one
        if self.test_config["profile"] is not None:
            path = heavy.download_profile(self.test_config["profile"])
            self.test_config["profile_path"] = path

        profile_path = os.path.normpath(self.test_config["profile_path"])
        LOG.info("Cloning profile located at %s" % profile_path)

        def _feedback(directory, content):
            # Called by shutil.copytree on each visited directory.
            # Used here to display info.
            #
            # Returns the items that should be ignored by
            # shutil.copytree when copying the tree, so always returns
            # an empty list.
            sub = directory.split(profile_path)[-1].lstrip("/")
            if sub:
                LOG.info("=> %s" % sub)
            return []

        profile = Profile.clone(
            profile_path, self.profile_dir, ignore=_feedback, restore=False
        )

        # build pref interpolation context
        webserver = self.browser_config["webserver"]
        if "://" not in webserver:
            webserver = "http://" + webserver

        interpolation = {
            "webserver": webserver,
        }

        # merge base profiles
        with open(os.path.join(self.profile_data_dir, "profiles.json"), "r") as fh:
            base_profiles = json.load(fh)["talos"]

        for name in base_profiles:
            path = os.path.join(self.profile_data_dir, name)
            LOG.info("Merging profile: {}".format(path))
            profile.merge(path, interpolation=interpolation)

        # set test preferences
        preferences = self.browser_config.get("preferences", {}).copy()
        if self.test_config.get("preferences"):
            test_prefs = dict(
                [
                    (i, utils.parse_pref(j))
                    for i, j in self.test_config["preferences"].items()
                ]
            )
            preferences.update(test_prefs)

        for name, value in preferences.items():
            if type(value) is str:
                value = utils.interpolate(value, **interpolation)
                preferences[name] = value
        profile.set_preferences(preferences)

        # installing addons
        LOG.info("Installing Add-ons:")
        LOG.info(extensions)
        profile.addons.install(extensions)

        # installing webextensions
        webextensions_to_install = []
        webextensions_folder = self.test_config.get("webextensions_folder", None)
        if isinstance(webextensions_folder, six.string_types):
            folder = utils.interpolate(webextensions_folder)
            for file in os.listdir(folder):
                if file.endswith(".xpi"):
                    webextensions_to_install.append(os.path.join(folder, file))

        webextensions = self.test_config.get("webextensions", None)
        if isinstance(webextensions, six.string_types):
            webextensions_to_install.append(webextensions)

        if webextensions_to_install is not None:
            LOG.info("Installing Webextensions:")
            for webext in webextensions_to_install:
                filename = utils.interpolate(webext)
                if mozinfo.os == "win":
                    filename = filename.replace("/", "\\")
                if not filename.endswith(".xpi"):
                    continue
                if not os.path.exists(filename):
                    continue
                LOG.info(filename)
                profile.addons.install(filename)
Example #33
0
    def _init_profile(self):
        preferences = dict(self.browser_config['preferences'])
        if self.test_config.get('preferences'):
            test_prefs = dict(
                [(i, utils.parse_pref(j))
                 for i, j in self.test_config['preferences'].items()]
            )
            preferences.update(test_prefs)
        # interpolate webserver value in prefs
        webserver = self.browser_config['webserver']
        if '://' not in webserver:
            webserver = 'http://' + webserver
        for name, value in preferences.items():
            if type(value) is str:
                value = utils.interpolate(value, webserver=webserver)
                preferences[name] = value

        extensions = self.browser_config['extensions'][:]
        if self.test_config.get('extensions'):
            extensions.extend(self.test_config['extensions'])

        # downloading a profile instead of using the empty one
        if self.test_config['profile'] is not None:
            path = heavy.download_profile(self.test_config['profile'])
            self.test_config['profile_path'] = path

        profile_path = os.path.normpath(self.test_config['profile_path'])
        LOG.info("Cloning profile located at %s" % profile_path)

        def _feedback(directory, content):
            # Called by shutil.copytree on each visited directory.
            # Used here to display info.
            #
            # Returns the items that should be ignored by
            # shutil.copytree when copying the tree, so always returns
            # an empty list.
            sub = directory.split(profile_path)[-1].lstrip("/")
            if sub:
                LOG.info("=> %s" % sub)
            return []

        profile = Profile.clone(profile_path,
                                self.profile_dir,
                                ignore=_feedback,
                                restore=False)

        profile.set_preferences(preferences)

        # installing addons
        LOG.info("Installing Add-ons:")
        LOG.info(extensions)
        profile.addon_manager.install_addons(extensions)

        # installing webextensions
        webextensions = self.test_config.get('webextensions', None)
        if isinstance(webextensions, basestring):
            webextensions = [webextensions]

        if webextensions is not None:
            LOG.info("Installing Webextensions:")
            for webext in webextensions:
                filename = utils.interpolate(webext)
                if mozinfo.os == 'win':
                    filename = filename.replace('/', '\\')
                if not filename.endswith('.xpi'):
                    continue
                if not os.path.exists(filename):
                    continue
                LOG.info(filename)
                profile.addon_manager.install_from_path(filename)
    def do_POST(self):
        try:

            path, body, session, element = self.process_request()
            logger.debug("%s - %s - %s - %s" % (path, body, session, element))

            if path == '/back':
                logger.info("Navigating back - %s" % session)
                assert(session)
                assert(self.server.marionette.go_back())
                self.send_JSON(session=session)
            elif path == '/clear':
                logger.info("Clearing %s - %s" % (element, session))
                assert(session)
                marionette_element = HTMLElement(self.server.marionette, element)
                marionette_element.clear()
                self.send_JSON(session=session)
            elif path == '/click':
                logger.info("Clicking %s - %s" % (element, session))
                assert(session)
                marionette_element = HTMLElement(self.server.marionette, element)
                marionette_element.click()
                self.send_JSON(session=session)
            elif path == '/element':
                logger.info("Find Element using - %s, value - %s  - %s" \
                            % (body['using'], body['value'], session)) 
                # find element variants
                assert(session)
                self.send_JSON(session=session,
                               value={'ELEMENT': self.server.marionette.find_element(body['using'], body['value'], id=element).id})
            elif path == '/elements':
                logger.info("Find Elements using - %s, value - %s  - %s" \
                            % (body['using'], body['value'], session)) 
                # find elements variants
                assert(session)
                self.send_JSON(session=session,
                               value=[{'ELEMENT': x.id} for x in self.server.marionette.find_elements(body['using'], body['value'])])
            elif path == '/execute':
                logger.info("Executing Script - %s" % session)
                assert(session)
                if body['args']:
                    result = self.server.marionette.execute_script(body['script'], script_args=body['args'],
                                                            new_sandbox=False)
                else:
                    result = self.server.marionette.execute_script(body['script'],new_sandbox=False)
                self.send_JSON(session=session, value=result)
            elif path == '/execute_async':
                logger.info("Executing Async Script - %s" % session)
                assert(session)
                result = None
                if body['args']:
                    result = self.server.marionette.execute_async_script(body['script'], script_args=body['args'], new_sandbox=False)
                else:
                    result = self.server.marionette.execute_async_script(body['script'], new_sandbox=False)
                self.send_JSON(session=session, value=result)
            elif path == '/forward':
                logger.info("Forwarding - %s" % session)
                assert(session)
                assert(self.server.marionette.go_forward())
                self.send_JSON(session=session)
            elif path == '/frame':
                logger.info("Switch to Frame %s - %s" % (body['id'], session))
                assert(session)
                frame = body['id']
                if isinstance(frame, dict) and 'ELEMENT' in frame:
                    frame = HTMLElement(self.server.marionette, frame['ELEMENT'])
                assert(self.server.marionette.switch_to_frame(frame))
                self.send_JSON(session=session)
            elif path == '/refresh':
                logger.info("Refreshing the page - %s" % session)
                assert(session)
                assert(self.server.marionette.refresh())
                self.send_JSON(session=session)
            elif path == '/session':
                logger.info("Creating new session")
                logger.debug("loading webdriver prefs")
                with open('webdriver.json') as webpref:
                    read_prefs = webpref.read()

                prefs = json.loads(read_prefs)
                port = free_port()

                logger.debug("Creating Profile")
                profile = Profile()
                profile.set_preferences(prefs['frozen'])
                profile.set_preferences(prefs['mutable'])
                profile.set_preferences({"marionette.defaultPrefs.enabled": True,
                                        "marionette.defaultPrefs.port": port})

                logger.debug("Profile created at %s" % profile.profile)
                logger.debug("Creating runner")
                firefox_binary = body["desiredCapabilities"]['firefoxBinary'] if body["desiredCapabilities"]['firefoxBinary'] else firefox_binary_path()
                self.server.runner = FirefoxRunner(profile, firefox_binary)
                self.server.runner.start()
                logger.debug("Browser has been started")
                import time
                time.sleep(10)
                logger.info("Creating Marionette instance on %s:%s" % ("localhost", port)) 
                self.server.marionette = Marionette("localhost", port)
                session = self.server.marionette.start_session()
                self.send_JSON(session=session, value={})
                # 'value' is the browser capabilities, which we're ignoring for now
            elif path == '/timeouts/async_script':
                logger.info("Script timeout %s ms - %s" % (body['ms'], session))
                assert(session)
                assert(self.server.marionette.set_script_timeout(body['ms']))
                self.send_JSON(session=session)
            elif path == '/timeouts/implicit_wait':
                logger.info("Implicit timeout %s ms - %s" % (body['ms'], session))
                assert(session)
                assert(self.server.marionette.set_search_timeout(body['ms']))
                self.send_JSON(session=session)
            elif path == '/url':
                logger.info("Navigating to %s - %s" % (body['url'], session))
                assert(session)
                assert(self.server.marionette.navigate(body['url']))
                self.send_JSON(session=session)
            elif path == '/value':
                logger.info("Send Keys %s - %s" % (''.join(body['value']), session))
                assert(session)
                keys = ''.join(body['value'])
                marionette_element = HTMLElement(self.server.marionette, element)
                assert(marionette_element.send_keys(keys))
                self.send_JSON(session=session)
            elif path == '/window':
                logger.info("Switch to Window %s - %s" % (body['name'], session))
                assert(session)
                assert(self.server.marionette.switch_to_window(body['name']))
                self.send_JSON(session=session)
            else:
                logger.error("Unknown path - %s" % session)
                self.file_not_found()

        except MarionetteException as e:
            logger.error("Status: %s - Message: %s" % (e.status, e.message))
            self.send_JSON(data={'status': e.status}, value={'message': e.message})
        except:
            trace_ = traceback.format_exc()
            logger.critical("Server Exception: %s" % trace_)
            self.server_error(trace_)