-
Notifications
You must be signed in to change notification settings - Fork 68
/
alert_commands.cc
103 lines (88 loc) · 3.56 KB
/
alert_commands.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/test/chromedriver/alert_commands.h"
#include <memory>
#include "base/functional/callback.h"
#include "base/values.h"
#include "chrome/test/chromedriver/chrome/chrome.h"
#include "chrome/test/chromedriver/chrome/devtools_client.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "chrome/test/chromedriver/chrome/web_view.h"
#include "chrome/test/chromedriver/net/timeout.h"
#include "chrome/test/chromedriver/session.h"
Status ExecuteAlertCommand(const AlertCommand& alert_command,
Session* session,
const base::Value::Dict& params,
std::unique_ptr<base::Value>* value) {
WebView* web_view = nullptr;
Status status = session->GetTargetWindow(&web_view);
if (status.IsError())
return status;
status = web_view->HandleReceivedEvents();
if (status.IsError())
return status;
status = web_view->WaitForPendingNavigations(
session->GetCurrentFrameId(), Timeout(session->page_load_timeout), true);
if (status.IsError() && status.code() != kUnexpectedAlertOpen)
return status;
return alert_command.Run(session, web_view, params, value);
}
Status ExecuteGetAlert(Session* session,
WebView* web_view,
const base::Value::Dict& params,
std::unique_ptr<base::Value>* value) {
*value = std::make_unique<base::Value>(web_view->IsDialogOpen());
return Status(kOk);
}
Status ExecuteGetAlertText(Session* session,
WebView* web_view,
const base::Value::Dict& params,
std::unique_ptr<base::Value>* value) {
std::string message;
Status status = web_view->GetDialogMessage(message);
if (status.IsError())
return status;
*value = std::make_unique<base::Value>(message);
return Status(kOk);
}
Status ExecuteSetAlertText(Session* session,
WebView* web_view,
const base::Value::Dict& params,
std::unique_ptr<base::Value>* value) {
const std::string* text = params.FindString("text");
if (!text)
return Status(kInvalidArgument, "missing or invalid 'text'");
if (!web_view->IsDialogOpen()) {
return Status(kNoSuchAlert);
}
std::string type;
Status status = web_view->GetTypeOfDialog(type);
if (status.IsError())
return status;
if (type == "prompt")
session->prompt_text = std::make_optional<std::string>(*text);
else if (type == "alert" || type == "confirm")
return Status(kElementNotInteractable,
"User dialog does not have a text box input field.");
else
return Status(kUnsupportedOperation,
"Text can only be sent to window.prompt dialogs.");
return Status(kOk);
}
Status ExecuteAcceptAlert(Session* session,
WebView* web_view,
const base::Value::Dict& params,
std::unique_ptr<base::Value>* value) {
Status status = web_view->HandleDialog(true, session->prompt_text);
session->prompt_text.reset();
return status;
}
Status ExecuteDismissAlert(Session* session,
WebView* web_view,
const base::Value::Dict& params,
std::unique_ptr<base::Value>* value) {
Status status = web_view->HandleDialog(false, session->prompt_text);
session->prompt_text.reset();
return status;
}