-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathelement_commands_unittest.cc
127 lines (110 loc) · 4.65 KB
/
element_commands_unittest.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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 <memory>
#include <string>
#include "base/values.h"
#include "chrome/test/chromedriver/chrome/status.h"
#include "chrome/test/chromedriver/chrome/stub_chrome.h"
#include "chrome/test/chromedriver/chrome/stub_web_view.h"
#include "chrome/test/chromedriver/element_commands.h"
#include "chrome/test/chromedriver/session.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/selenium-atoms/atoms.h"
namespace {
class MockChrome : public StubChrome {
public:
MockChrome() : web_view_("1") {}
~MockChrome() override = default;
Status GetWebViewById(const std::string& id, WebView** web_view) override {
if (id == web_view_.GetId()) {
*web_view = &web_view_;
return Status(kOk);
}
return Status(kUnknownError);
}
private:
// Using a StubWebView does not allow testing the functionality end-to-end,
// more details in crbug.com/850703
StubWebView web_view_;
};
typedef Status (*Command)(Session* session,
WebView* web_view,
const std::string& element_id,
const base::Value::Dict& params,
std::unique_ptr<base::Value>* value);
Status CallElementCommand(Command command,
StubWebView* web_view,
const std::string& element_id,
const base::Value::Dict& params = {},
bool w3c_compliant = true,
std::unique_ptr<base::Value>* value = nullptr) {
MockChrome* chrome = new MockChrome();
Session session("id", std::unique_ptr<Chrome>(chrome));
session.w3c_compliant = w3c_compliant;
std::unique_ptr<base::Value> local_value;
return command(&session, web_view, element_id, params,
value ? value : &local_value);
}
} // namespace
namespace {
class MockResponseWebView : public StubWebView {
public:
MockResponseWebView() : StubWebView("1") {}
~MockResponseWebView() override = default;
Status CallFunction(const std::string& frame,
const std::string& function,
const base::Value::List& args,
std::unique_ptr<base::Value>* result) override {
if (function ==
webdriver::atoms::asString(webdriver::atoms::GET_LOCATION)) {
base::Value::Dict dict;
dict.SetByDottedPath("value.status", 0);
dict.Set("x", 0.0);
dict.Set("y", 128.8);
*result = std::make_unique<base::Value>(std::move(dict));
} else if (function ==
webdriver::atoms::asString(webdriver::atoms::GET_SIZE)) {
// Do not set result; this should be an error state
return Status(kStaleElementReference);
} else {
base::Value::Dict dict;
dict.SetByDottedPath("value.status", 0);
*result = std::make_unique<base::Value>(std::move(dict));
}
return Status(kOk);
}
};
} // namespace
TEST(ElementCommandsTest, ExecuteGetElementRect_SizeError) {
MockResponseWebView webview = MockResponseWebView();
base::Value::Dict params;
std::unique_ptr<base::Value> result_value;
Status status = CallElementCommand(ExecuteGetElementRect, &webview,
"3247f4d1-ce70-49e9-9a99-bdc7591e032f",
params, true, &result_value);
ASSERT_EQ(kStaleElementReference, status.code()) << status.message();
}
TEST(ElementCommandsTest, ExecuteSendKeysToElement_NoValue) {
MockResponseWebView webview = MockResponseWebView();
base::Value::Dict params;
std::unique_ptr<base::Value> result_value;
Status status = CallElementCommand(ExecuteSendKeysToElement, &webview,
"3247f4d1-ce70-49e9-9a99-bdc7591e032f",
params, false, &result_value);
ASSERT_EQ(kInvalidArgument, status.code()) << status.message();
ASSERT_NE(status.message().find("'value' must be a list"), std::string::npos)
<< status.message();
}
TEST(ElementCommandsTest, ExecuteSendKeysToElement_ValueNotAList) {
MockResponseWebView webview = MockResponseWebView();
base::Value::Dict params;
params.Set("value", "not-a-list");
std::unique_ptr<base::Value> result_value;
Status status = CallElementCommand(ExecuteSendKeysToElement, &webview,
"3247f4d1-ce70-49e9-9a99-bdc7591e032f",
params, false, &result_value);
ASSERT_EQ(kInvalidArgument, status.code()) << status.message();
ASSERT_NE(status.message().find("'value' must be a list"), std::string::npos)
<< status.message();
}