Bug 1308902 - Update Puppeteer to use new L10n module of Marionette. draft
authorHenrik Skupin <mail@hskupin.info>
Fri, 11 Nov 2016 22:56:43 +0100
changeset 437894 6fc0985455104ed88f947074b2f3d7c462f870df
parent 437888 7ed5dfa7f3ed17caa4f416600fc3c22aead049d4
child 536759 745e58afb11ddeff3ba71d2215545ed3613d8b65
push id35548
push userbmo:hskupin@gmail.com
push dateFri, 11 Nov 2016 21:57:21 +0000
bugs1308902
milestone52.0a1
Bug 1308902 - Update Puppeteer to use new L10n module of Marionette. MozReview-Commit-ID: CaTjFABGvtD
testing/firefox-ui/tests/puppeteer/test_l10n.py
testing/puppeteer/firefox/firefox_puppeteer/api/l10n.py
--- a/testing/firefox-ui/tests/puppeteer/test_l10n.py
+++ b/testing/firefox-ui/tests/puppeteer/test_l10n.py
@@ -1,14 +1,14 @@
 # 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 marionette_driver import By
-from marionette_driver.errors import MarionetteException
+from marionette_driver.errors import NoSuchElementException
 
 from firefox_puppeteer.api.l10n import L10n
 from firefox_ui_harness.testcases import FirefoxTestCase
 
 
 class TestL10n(FirefoxTestCase):
 
     def setUp(self):
@@ -21,17 +21,18 @@ class TestL10n(FirefoxTestCase):
     def test_dtd_entity_chrome(self):
         dtds = ['chrome://global/locale/about.dtd',
                 'chrome://browser/locale/baseMenuOverlay.dtd']
 
         value = self.l10n.get_entity(dtds, 'helpSafeMode.label')
         elm = self.marionette.find_element(By.ID, 'helpSafeMode')
         self.assertEqual(value, elm.get_attribute('label'))
 
-        self.assertRaises(MarionetteException, self.l10n.get_entity, dtds, 'notExistent')
+        self.assertRaises(NoSuchElementException,
+                          self.l10n.get_entity, dtds, 'notExistent')
 
     def test_dtd_entity_content(self):
         dtds = ['chrome://global/locale/about.dtd',
                 'chrome://global/locale/aboutSupport.dtd']
 
         value = self.l10n.get_entity(dtds, 'aboutSupport.pageTitle')
 
         self.marionette.set_context(self.marionette.CONTEXT_CONTENT)
@@ -43,9 +44,10 @@ class TestL10n(FirefoxTestCase):
     def test_properties(self):
         properties = ['chrome://global/locale/filepicker.properties',
                       'chrome://global/locale/findbar.properties']
 
         # TODO: Find a way to verify the retrieved translated string
         value = self.l10n.get_property(properties, 'NotFound')
         self.assertNotEqual(value, '')
 
-        self.assertRaises(MarionetteException, self.l10n.get_property, properties, 'notExistent')
+        self.assertRaises(NoSuchElementException,
+                          self.l10n.get_property, properties, 'notExistent')
--- a/testing/puppeteer/firefox/firefox_puppeteer/api/l10n.py
+++ b/testing/puppeteer/firefox/firefox_puppeteer/api/l10n.py
@@ -1,92 +1,125 @@
 # 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/.
 
+# -----------------
+# DEPRECATED module
+# -----------------
+# Replace its use in tests when Firefox 45 ESR support ends with
+# marionette_driver.localization.L10n
+
 import copy
 
-from marionette_driver.errors import MarionetteException
+from marionette_driver.errors import (
+    NoSuchElementException,
+    UnknownCommandException,
+)
+from marionette_driver.localization import L10n as L10nMarionette
 
 from firefox_puppeteer.base import BaseLib
 
 
 class L10n(BaseLib):
+    """An API which allows Marionette to handle localized content.
+
+    .. deprecated:: 52.2.0
+       Use the localization module from :py:mod:`marionette_driver` instead.
+
+    The `localization`_ of UI elements in Gecko based applications is done via
+    entities and properties. For static values entities are used, which are located
+    in .dtd files. Whereby for dynamically updated content the values come from
+    .property files. Both types of elements can be identifed via a unique id,
+    and the translated content retrieved.
+
+    .. _localization: https://mzl.la/2eUMjyF
+    """
+
+    def __init__(self, marionette):
+        super(L10n, self).__init__(marionette)
+
+        self._l10nMarionette = L10nMarionette(self.marionette)
 
     def get_entity(self, dtd_urls, entity_id):
         """Returns the localized string for the specified DTD entity id.
 
         To find the entity all given DTD files will be searched for the id.
 
         :param dtd_urls: A list of dtd files to search.
         :param entity_id: The id to retrieve the value from.
 
         :returns: The localized string for the requested entity.
 
-        :raises MarionetteException: When entity id is not found in dtd_urls.
+        :raises NoSuchElementException: When entity id is not found in dtd_urls.
         """
         # Add xhtml11.dtd to prevent missing entity errors with XHTML files
-        dtds = copy.copy(dtd_urls)
-        dtds.append("resource:///res/dtd/xhtml11.dtd")
+        try:
+            return self._l10nMarionette.localize_entity(dtd_urls, entity_id)
+        except UnknownCommandException:
+            dtds = copy.copy(dtd_urls)
+            dtds.append("resource:///res/dtd/xhtml11.dtd")
 
-        dtd_refs = ''
-        for index, item in enumerate(dtds):
-            dtd_id = 'dtd_%s' % index
-            dtd_refs += '<!ENTITY %% %s SYSTEM "%s">%%%s;' % \
-                (dtd_id, item, dtd_id)
+            dtd_refs = ''
+            for index, item in enumerate(dtds):
+                dtd_id = 'dtd_%s' % index
+                dtd_refs += '<!ENTITY %% %s SYSTEM "%s">%%%s;' % \
+                    (dtd_id, item, dtd_id)
 
-        contents = """<?xml version="1.0"?>
-            <!DOCTYPE elem [%s]>
-
-            <elem id="entity">&%s;</elem>""" % (dtd_refs, entity_id)
+            contents = """<?xml version="1.0"?>
+                <!DOCTYPE elem [%s]>
 
-        with self.marionette.using_context('chrome'):
-            value = self.marionette.execute_script("""
-                var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
-                             .createInstance(Components.interfaces.nsIDOMParser);
-                var doc = parser.parseFromString(arguments[0], "text/xml");
-                var node = doc.querySelector("elem[id='entity']");
+                <elem id="entity">&%s;</elem>""" % (dtd_refs, entity_id)
 
-                return node ? node.textContent : null;
-            """, script_args=[contents])
+            with self.marionette.using_context('chrome'):
+                value = self.marionette.execute_script("""
+                    var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
+                                 .createInstance(Components.interfaces.nsIDOMParser);
+                    var doc = parser.parseFromString(arguments[0], "text/xml");
+                    var node = doc.querySelector("elem[id='entity']");
 
-        if not value:
-            raise MarionetteException('DTD Entity not found: %s' % entity_id)
+                    return node ? node.textContent : null;
+                """, script_args=[contents])
 
-        return value
+            if not value:
+                raise NoSuchElementException('DTD Entity not found: %s' % entity_id)
+
+            return value
 
     def get_property(self, property_urls, property_id):
         """Returns the localized string for the specified property id.
 
         To find the property all given property files will be searched for
         the id.
 
         :param property_urls: A list of property files to search.
         :param property_id: The id to retrieve the value from.
 
         :returns: The localized string for the requested entity.
 
-        :raises MarionetteException: When property id is not found in
+        :raises NoSuchElementException: When property id is not found in
             property_urls.
         """
+        try:
+            return self._l10nMarionette.localize_property(property_urls, property_id)
+        except UnknownCommandException:
+            with self.marionette.using_context('chrome'):
+                value = self.marionette.execute_script("""
+                    let property = null;
+                    let property_id = arguments[1];
 
-        with self.marionette.using_context('chrome'):
-            value = self.marionette.execute_script("""
-                let property = null;
-                let property_id = arguments[1];
-
-                arguments[0].some(aUrl => {
-                  let bundle = Services.strings.createBundle(aUrl);
+                    arguments[0].some(aUrl => {
+                      let bundle = Services.strings.createBundle(aUrl);
 
-                  try {
-                    property = bundle.GetStringFromName(property_id);
-                    return true;
-                  }
-                  catch (ex) { }
-                });
+                      try {
+                        property = bundle.GetStringFromName(property_id);
+                        return true;
+                      }
+                      catch (ex) { }
+                    });
 
-                return property;
-            """, script_args=[property_urls, property_id])
+                    return property;
+                """, script_args=[property_urls, property_id])
 
-        if not value:
-            raise MarionetteException('Property not found: %s' % property_id)
+            if not value:
+                raise NoSuchElementException('Property not found: %s' % property_id)
 
-        return value
+            return value