Bug 1452483 - Use relative path to source file for injected scripts. r?maja_zf draft
authorAndreas Tolfsen <ato@sny.no>
Mon, 09 Apr 2018 11:43:08 +0100
changeset 780426 dc94597876c49f2e7725e0ec0d98cbed159ed056
parent 780425 d3194be79b21cf1251ecb0af4f7b6e08640bfa34
child 780427 10d9a0365643f7b5e74c6cfc1d4f1ae74693da7c
push id105996
push userbmo:ato@sny.no
push dateWed, 11 Apr 2018 12:15:00 +0000
reviewersmaja_zf
bugs1452483
milestone61.0a1
Bug 1452483 - Use relative path to source file for injected scripts. r?maja_zf The basename is not sufficient to locate the file. Using the file's relative path will match the behaviour of JavaScript stacktraces. We can't use relative paths on Windows because the source file may exist on another disk drive, and on Windows you cannot make relative paths across disk drives because they don't share the same root. MozReview-Commit-ID: 4EPITa2kH6J
testing/marionette/client/marionette_driver/marionette.py
--- a/testing/marionette/client/marionette_driver/marionette.py
+++ b/testing/marionette/client/marionette_driver/marionette.py
@@ -1701,26 +1701,25 @@ class Marionette(object):
             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": os.path.basename(frame[0])}
-
-        rv = self._send_message("WebDriver:ExecuteScript",
-                                body, key="value")
+                "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,
                              debug_script=False):
         """Executes an asynchronous JavaScript script, and returns the
         result (or None if the script does return a value).
 
@@ -1751,23 +1750,24 @@ class Marionette(object):
                 marionetteScriptFinished(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": os.path.basename(frame[0]),
+                "filename": filename,
                 "debug_script": debug_script}
 
         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`