Skip to content

Commit 29e9746

Browse files
committed
Making set window rect command spec-compliant for x and y coordinates
The W3C WebDriver specification allows negative x and y coordinates for setting a window rect. Previously, the driver forced the passed in coordinates to be greater than zero.
1 parent f1a7bf6 commit 29e9746

File tree

8 files changed

+133
-43
lines changed

8 files changed

+133
-43
lines changed

cpp/iedriver/CommandHandlers/SetWindowRectCommandHandler.cpp

Lines changed: 108 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "SetWindowRectCommandHandler.h"
1818
#include "errorcodes.h"
19+
#include "json.h"
1920
#include "../Browser.h"
2021
#include "../IECommandExecutor.h"
2122

@@ -37,32 +38,79 @@ void SetWindowRectCommandHandler::ExecuteInternal(
3738
int y = -1;
3839

3940
std::string argument_error_message = "";
40-
if (!GetNumericParameter(command_parameters, "width", &width, &argument_error_message)) {
41-
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
42-
return;
41+
Json::Value width_parameter;
42+
bool is_width_defined = this->IsParameterDefined(command_parameters,
43+
"width",
44+
&width_parameter);
45+
46+
if (is_width_defined) {
47+
if (!this->GetNumericParameter("width",
48+
true,
49+
width_parameter,
50+
&width,
51+
&argument_error_message)) {
52+
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
53+
argument_error_message);
54+
return;
55+
}
4356
}
4457

45-
if (!GetNumericParameter(command_parameters, "height", &height, &argument_error_message)) {
46-
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
47-
return;
58+
Json::Value height_parameter;
59+
bool is_height_defined = this->IsParameterDefined(command_parameters,
60+
"height",
61+
&height_parameter);
62+
if (is_height_defined) {
63+
if (!this->GetNumericParameter("height",
64+
true,
65+
height_parameter,
66+
&height,
67+
&argument_error_message)) {
68+
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
69+
argument_error_message);
70+
return;
71+
}
4872
}
4973

50-
if (!GetNumericParameter(command_parameters, "x", &x, &argument_error_message)) {
51-
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
52-
return;
74+
Json::Value x_parameter;
75+
bool is_x_defined = this->IsParameterDefined(command_parameters,
76+
"x",
77+
&x_parameter);
78+
if (is_x_defined) {
79+
if (!this->GetNumericParameter("x",
80+
false,
81+
x_parameter,
82+
&x,
83+
&argument_error_message)) {
84+
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
85+
argument_error_message);
86+
return;
87+
}
5388
}
5489

55-
if (!GetNumericParameter(command_parameters, "y", &y, &argument_error_message)) {
56-
response->SetErrorResponse(ERROR_INVALID_ARGUMENT, argument_error_message);
57-
return;
90+
Json::Value y_parameter;
91+
bool is_y_defined = this->IsParameterDefined(command_parameters,
92+
"y",
93+
&y_parameter);
94+
95+
if (is_y_defined) {
96+
if (!GetNumericParameter("y",
97+
false,
98+
y_parameter,
99+
&y,
100+
&argument_error_message)) {
101+
response->SetErrorResponse(ERROR_INVALID_ARGUMENT,
102+
argument_error_message);
103+
return;
104+
}
58105
}
59106

60107
int status_code = WD_SUCCESS;
61108

62109
BrowserHandle browser_wrapper;
63110
status_code = executor.GetCurrentBrowser(&browser_wrapper);
64111
if (status_code != WD_SUCCESS) {
65-
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW, "Error retrieving current window");
112+
response->SetErrorResponse(ERROR_NO_SUCH_WINDOW,
113+
"Error retrieving current window");
66114
return;
67115
}
68116

@@ -73,16 +121,22 @@ void SetWindowRectCommandHandler::ExecuteInternal(
73121
HWND window_handle = browser_wrapper->GetTopLevelWindowHandle();
74122
RECT current_window_rect;
75123
::GetWindowRect(window_handle, &current_window_rect);
76-
if (x < 0 || y < 0) {
124+
if (!is_x_defined || !is_y_defined) {
77125
x = current_window_rect.left;
78126
y = current_window_rect.top;
79127
}
80-
if (height < 0 || width < 0) {
128+
if (!is_height_defined || !is_width_defined) {
81129
height = current_window_rect.bottom - current_window_rect.top;
82130
width = current_window_rect.right - current_window_rect.left;
83131
}
84132

85-
BOOL set_window_pos_result = ::SetWindowPos(window_handle, NULL, x, y, width, height, 0);
133+
BOOL set_window_pos_result = ::SetWindowPos(window_handle,
134+
NULL,
135+
x,
136+
y,
137+
width,
138+
height,
139+
0);
86140
if (!set_window_pos_result) {
87141
response->SetErrorResponse(ERROR_UNKNOWN_ERROR,
88142
"Unexpected error setting window size (SetWindowPos API failed)");
@@ -101,28 +155,49 @@ void SetWindowRectCommandHandler::ExecuteInternal(
101155
}
102156

103157
bool SetWindowRectCommandHandler::GetNumericParameter(
104-
const ParametersMap& command_parameters,
105158
const std::string& argument_name,
159+
const bool is_positive_required,
160+
const Json::Value& parameter_value,
106161
int* argument_value,
107162
std::string* error_message) {
108-
ParametersMap::const_iterator parameter_iterator = command_parameters.find(argument_name);
109-
if (parameter_iterator != command_parameters.end()) {
110-
if (parameter_iterator->second.isNull()) {
111-
return true;
112-
}
113-
if (!parameter_iterator->second.isNumeric()) {
114-
*error_message = argument_name + " must be a numeric parameter.";
115-
return false;
116-
}
117-
int value = parameter_iterator->second.asInt();
118-
if (value < 0) {
119-
*error_message = argument_name + " must be a numeric parameter greater than zero.";
120-
return false;
121-
}
163+
int max_value = MAXINT;
164+
std::string max_value_description = "2^31 - 1";
165+
int min_value = MININT;
166+
std::string min_value_description = "-2^31";
167+
if (is_positive_required) {
168+
min_value = 0;
169+
min_value_description = "zero";
170+
}
171+
if (!parameter_value.isNumeric()) {
172+
*error_message = argument_name + " must be a numeric parameter.";
173+
return false;
174+
}
175+
int value = parameter_value.asInt();
176+
if (value < min_value) {
177+
*error_message = argument_name + " must be a numeric parameter greater than " + min_value_description;
178+
return false;
179+
}
180+
if (value > max_value) {
181+
*error_message = argument_name + " must be a numeric parameter less than " + max_value_description;
182+
return false;
183+
}
122184

123-
*argument_value = value;
124-
return true;
185+
*argument_value = value;
186+
return true;
187+
}
188+
189+
bool SetWindowRectCommandHandler::IsParameterDefined(
190+
const ParametersMap& command_parameters,
191+
const std::string& parameter_name,
192+
Json::Value* parameter_value) {
193+
ParametersMap::const_iterator parameter_iterator = command_parameters.find(parameter_name);
194+
if (parameter_iterator == command_parameters.end()) {
195+
return false;
196+
}
197+
if (parameter_iterator->second.isNull()) {
198+
return false;
125199
}
200+
*parameter_value = parameter_iterator->second;
126201
return true;
127202
}
128203

cpp/iedriver/CommandHandlers/SetWindowRectCommandHandler.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
#include "../IECommandHandler.h"
2020

21+
namespace Json {
22+
class Value;
23+
}
24+
2125
namespace webdriver {
2226

2327
class SetWindowRectCommandHandler : public IECommandHandler {
@@ -31,10 +35,14 @@ class SetWindowRectCommandHandler : public IECommandHandler {
3135
Response* response);
3236

3337
private:
34-
bool GetNumericParameter(const ParametersMap& command_parameters,
35-
const std::string& argument_name,
38+
bool GetNumericParameter(const std::string& argument_name,
39+
const bool is_positive_required,
40+
const Json::Value& parameter_value,
3641
int* argument_value,
3742
std::string* error_message);
43+
bool IsParameterDefined(const ParametersMap& command_parameters,
44+
const std::string& parameter_name,
45+
Json::Value* parameter_value);
3846
};
3947

4048
} // namespace webdriver

cpp/iedriver/IEDriver.rc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ END
5050
//
5151

5252
VS_VERSION_INFO VERSIONINFO
53-
FILEVERSION 3,13,0,3
54-
PRODUCTVERSION 3,13,0,3
53+
FILEVERSION 3,13,0,4
54+
PRODUCTVERSION 3,13,0,4
5555
FILEFLAGSMASK 0x3fL
5656
#ifdef _DEBUG
5757
FILEFLAGS 0x1L
@@ -68,12 +68,12 @@ BEGIN
6868
BEGIN
6969
VALUE "CompanyName", "Software Freedom Conservancy"
7070
VALUE "FileDescription", "Driver library for the IE driver"
71-
VALUE "FileVersion", "3.13.0.3"
71+
VALUE "FileVersion", "3.13.0.4"
7272
VALUE "InternalName", "IEDriver.dll"
7373
VALUE "LegalCopyright", "Copyright (C) 2018"
7474
VALUE "OriginalFilename", "IEDriver.dll"
7575
VALUE "ProductName", "Selenium WebDriver"
76-
VALUE "ProductVersion", "3.13.0.3"
76+
VALUE "ProductVersion", "3.13.0.4"
7777
END
7878
END
7979
BLOCK "VarFileInfo"

cpp/iedriver/Script.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
#include "CustomTypes.h"
2424

25+
#define ANONYMOUS_FUNCTION_START L"(function() { "
26+
#define ANONYMOUS_FUNCTION_END L" })();"
27+
2528
// Forward declaration of classes.
2629
namespace Json {
2730
class Value;

cpp/iedriverserver/CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ available via the project downloads page. Changes in "revision" field indicate
99
private releases checked into the prebuilts directory of the source tree, but
1010
not made generally available on the downloads page.
1111

12+
v3.13.0.4
13+
=========
14+
* Made set window rect command spec-compliant for x and y coordinates
15+
1216
v3.13.0.3
1317
=========
1418
* Fixed crashing bug in obscured element detection.

cpp/iedriverserver/IEDriverServer.rc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ END
5050
//
5151

5252
VS_VERSION_INFO VERSIONINFO
53-
FILEVERSION 3,13,0,3
54-
PRODUCTVERSION 3,13,0,3
53+
FILEVERSION 3,13,0,4
54+
PRODUCTVERSION 3,13,0,4
5555
FILEFLAGSMASK 0x3fL
5656
#ifdef _DEBUG
5757
FILEFLAGS 0x1L
@@ -68,12 +68,12 @@ BEGIN
6868
BEGIN
6969
VALUE "CompanyName", "Software Freedom Conservancy"
7070
VALUE "FileDescription", "Command line server for the IE driver"
71-
VALUE "FileVersion", "3.13.0.3"
71+
VALUE "FileVersion", "3.13.0.4"
7272
VALUE "InternalName", "IEDriverServer.exe"
7373
VALUE "LegalCopyright", "Copyright (C) 2018"
7474
VALUE "OriginalFilename", "IEDriverServer.exe"
7575
VALUE "ProductName", "Selenium WebDriver"
76-
VALUE "ProductVersion", "3.13.0.3"
76+
VALUE "ProductVersion", "3.13.0.4"
7777
END
7878
END
7979
BLOCK "VarFileInfo"
2.5 KB
Binary file not shown.
2 KB
Binary file not shown.

0 commit comments

Comments
 (0)