Commit 22fcd03d authored by Artur Khachatryan's avatar Artur Khachatryan Committed by Commit Bot

[ChromeDriver] Handle console log messages with multiple args

Handle the case where args.GetSize() > 1. This happens when the
first arg is a printf-style format string. We previously return the
first arg format string to the test client. Now, we return all args
separated by spaces.

The oroginal author of this change was sblancov84@gmail.com

Bug: chromedriver:669
Change-Id: I9d1f3389baf967108858254df847c358ec1a35c2
Reviewed-on: https://chromium-review.googlesource.com/1179069Reviewed-by: default avatarJohn Chen <johnchen@chromium.org>
Commit-Queue: Artur Khachatryan <khachatryan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584636}
parent ec26a37e
...@@ -197,29 +197,40 @@ Status ConsoleLogger::OnRuntimeConsoleApiCalled( ...@@ -197,29 +197,40 @@ Status ConsoleLogger::OnRuntimeConsoleApiCalled(
} }
} }
// TODO(samuong): Handle the case where args.GetSize() > 1. This happens when
// the first arg is a printf-style format string. We currently return the
// format string to the test client. For details, see
// https://bugs.chromium.org/p/chromedriver/issues/detail?id=669
std::string text; std::string text;
const base::ListValue* args = nullptr; const base::ListValue* args = nullptr;
const base::DictionaryValue* first_arg = nullptr;
if (!params.GetList("args", &args) || args->GetSize() < 1 ||
!args->GetDictionary(0, &first_arg)) {
return Status(kUnknownError, "missing or invalid args");
}
std::string arg_type; std::string arg_type;
if (first_arg->GetString("type", &arg_type) && arg_type == "undefined") { if (!params.GetList("args", &args) || args->GetSize() < 1) {
text = "undefined"; return Status(kUnknownError, "missing or invalid args");
} else if (!first_arg->GetString("description", &text)) {
const base::Value* value = nullptr;
if (!first_arg->Get("value", &value))
return Status(kUnknownError, "missing or invalid arg value");
if (!base::JSONWriter::Write(*value, &text))
return Status(kUnknownError, "failed to convert value to text");
} }
int arg_count = args->GetSize();
const base::DictionaryValue* current_arg = nullptr;
for (int i = 0; i < arg_count; i++) {
if (!args->GetDictionary(i, &current_arg)) {
std::string error_message = base::StringPrintf("Argument %d is missing or invalid", i);
return Status(kUnknownError, error_message );
}
std::string temp_text;
std::string arg_type;
if (current_arg->GetString("type", &arg_type) && arg_type == "undefined") {
temp_text = "undefined";
} else if (!current_arg->GetString("description", &temp_text)) {
const base::Value* value = nullptr;
if (!current_arg->Get("value", &value)) {
return Status(kUnknownError, "missing or invalid arg value");
}
if (!base::JSONWriter::Write(*value, &temp_text)) {
return Status(kUnknownError, "failed to convert value to text");
}
}
// add spaces between the arguments
if (i != 0)
text += " ";
text += temp_text;
}
log_->AddEntry(level, "console-api", base::StringPrintf("%s %s %s", log_->AddEntry(level, "console-api", base::StringPrintf("%s %s %s",
origin.c_str(), origin.c_str(),
line_column.c_str(), line_column.c_str(),
......
...@@ -1067,11 +1067,11 @@ class ChromeDriverTest(ChromeDriverBaseTestWithWebServer): ...@@ -1067,11 +1067,11 @@ class ChromeDriverTest(ChromeDriverBaseTestWithWebServer):
'/chromedriver/pending_console_log.html')) '/chromedriver/pending_console_log.html'))
logs = self._driver.GetLog('browser') logs = self._driver.GetLog('browser')
self.assertEqual('console-api', logs[0]['source']) self.assertEqual('console-api', logs[0]['source'])
self.assertTrue('InitialError' in logs[0]['message']) self.assertTrue('"InitialError" 2018 "Third"' in logs[0]['message'])
self.WaitForCondition(lambda: len(GetPendingLogs(self._driver)) > 0 , 11) self.WaitForCondition(lambda: len(GetPendingLogs(self._driver)) > 0 , 6)
self.assertEqual('console-api', new_logs[0][0]['source']) self.assertEqual('console-api', new_logs[0][0]['source'])
self.assertTrue('RepeatedError' in new_logs[0][0]['message']) self.assertTrue('"RepeatedError" "Second" "Third"' in new_logs[0][0]['message'])
def testGetLogOnClosedWindow(self): def testGetLogOnClosedWindow(self):
self._driver.Load(self.GetHttpUrlForFile('/chromedriver/page_test.html')) self._driver.Load(self.GetHttpUrlForFile('/chromedriver/page_test.html'))
......
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
<body> <body>
<script> <script>
window.onload = function() { window.onload = function() {
console.error("InitialError"); console.error("InitialError", 2018, "Third");
window.setTimeout(function() { window.setTimeout(function() {
console.error("RepeatedError"); console.error("RepeatedError", "Second", "Third");
}, 10000); }, 5000);
}; };
</script> </script>
</body> </body>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment