diff --git a/config/environment/ui-test.php b/config/environment/ui-test.php
index <HASH>..<HASH> 100644
--- a/config/environment/ui-test.php
+++ b/config/environment/ui-test.php
@@ -5,8 +5,17 @@ return array(
     'observers.global' => array(
 
         array('Request.dispatch.end', function (&$result) {
-            // remove the port from URLs if any so UI tests won't fail if the port isn't 80
-            $result = preg_replace('/localhost:[0-9]+/', 'localhost', $result);
+            $config = \Piwik\Config::getInstance();
+            $host = $config->tests['http_host'];
+            $port = $config->tests['port'];
+
+            if (!empty($port)) {
+                // remove the port from URLs if any so UI tests won't fail if the port isn't 80
+                $result = str_replace($host . ':' . $port, $host, $result);
+            }
+
+            // remove PIWIK_INCLUDE_PATH from result so tests don't change based on the machine used
+            $result = str_replace(realpath(PIWIK_INCLUDE_PATH), '', $result);
         }),
 
     ),
Remove host & port only if test port is defined in config & remove Piwik base path from all output so tests won't change based on machine.