Skip to content

Commit

Permalink
[IEDriver] Ignore process id match when finding the window handle - I…
Browse files Browse the repository at this point in the history
…E Mode on Edge. (#12246)

This assumes a single Edge instance is running
on the host. Not suited for running sessions
in parallel, or having another Edge browser
manually at the same time, because it will
most likely lead to return the wrong window
handle.
  • Loading branch information
diemol committed Jun 23, 2023
1 parent b49da80 commit 6e85abb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
29 changes: 28 additions & 1 deletion cpp/iedriver/BrowserFactory.cpp
Expand Up @@ -101,6 +101,7 @@ BrowserFactory::BrowserFactory(void) {
this->GetIEVersion();
this->oleacc_instance_handle_ = NULL;
this->edge_ie_mode_ = false;
this->ignore_process_match_ = false;
}

BrowserFactory::~BrowserFactory(void) {
Expand All @@ -127,6 +128,7 @@ void BrowserFactory::Initialize(BrowserFactorySettings settings) {
this->browser_command_line_switches_ = StringUtilities::ToWString(settings.browser_command_line_switches);
this->initial_browser_url_ = StringUtilities::ToWString(settings.initial_browser_url);
this->edge_ie_mode_ = settings.attach_to_edge_ie || this->ie_redirects_edge_;
this->ignore_process_match_ = settings.ignore_process_match;
this->ignore_zoom_setting_ = settings.ignore_zoom_setting || this->edge_ie_mode_;
LOG(DEBUG) << "path before was " << settings.edge_executable_path << "\n";
this->edge_executable_location_ = StringUtilities::ToWString(settings.edge_executable_path);
Expand Down Expand Up @@ -565,8 +567,18 @@ bool BrowserFactory::AttachToBrowserUsingActiveAccessibility
reinterpret_cast<LPARAM>(process_window_info));
} else {
// If we're in edge_ie_mode, we need to look for different windows
::EnumWindows(&BrowserFactory::FindEdgeWindow,
if (this->ignore_process_match_) {
LOG(TRACE) << "Finding windonw handle for IE Mode on Edge, "
<< "ignoring process id match. This assumes only one "
<< "Edge instance is running on the host.";
::EnumWindows(&BrowserFactory::FindEdgeWindowIgnoringProcessMatch,
reinterpret_cast<LPARAM>(process_window_info));
} else {
LOG(TRACE) << "Finding windonw handle for IE Mode on Edge";
::EnumWindows(&BrowserFactory::FindEdgeWindow,
reinterpret_cast<LPARAM>(process_window_info));

}
}

if (process_window_info->hwndBrowser == NULL) {
Expand Down Expand Up @@ -1116,6 +1128,21 @@ BOOL CALLBACK BrowserFactory::FindEdgeWindow(HWND hwnd, LPARAM arg) {
return EnumChildWindows(hwnd, FindEdgeChildWindowForProcess, arg);
}

BOOL CALLBACK BrowserFactory::FindEdgeWindowIgnoringProcessMatch(HWND hwnd, LPARAM arg) {
// Could this be an EdgeChrome window?
// 19 == "Chrome_WidgetWin_1"
char name[20];
if (::GetClassNameA(hwnd, name, 20) == 0) {
// No match found. Skip
return TRUE;
}

// continue if it is not "Chrome_WidgetWin_1"
if (strcmp(ANDIE_FRAME_WINDOW_CLASS, name) != 0) return TRUE;

return EnumChildWindows(hwnd, FindEdgeChildWindowForProcess, arg);
}

BOOL CALLBACK BrowserFactory::FindIEBrowserHandles(HWND hwnd, LPARAM arg) {
std::vector<HWND>* handles = reinterpret_cast<std::vector<HWND>*>(arg);

Expand Down
4 changes: 4 additions & 0 deletions cpp/iedriver/BrowserFactory.h
Expand Up @@ -38,6 +38,7 @@ struct BrowserFactorySettings {
std::string browser_command_line_switches;
bool attach_to_edge_ie; // Used to attach to EdgeChromium IE processes
std::string edge_executable_path;
bool ignore_process_match; // Ignores window handle process id match on IE Mode.
};

class BrowserFactory {
Expand All @@ -62,6 +63,7 @@ class BrowserFactory {
bool force_createprocess_api(void) const { return this->force_createprocess_api_; }
bool force_shell_windows_api(void) const { return this->force_shell_windows_api_; }
int browser_attach_timeout(void) const { return this->browser_attach_timeout_; }
bool ignore_process_match(void) const { return this->ignore_process_match_; }
std::string initial_browser_url(void);
std::string browser_command_line_switches(void);

Expand All @@ -81,6 +83,7 @@ class BrowserFactory {
private:
static BOOL CALLBACK FindBrowserWindow(HWND hwnd, LPARAM param);
static BOOL CALLBACK FindEdgeWindow(HWND hwnd, LPARAM param);
static BOOL CALLBACK FindEdgeWindowIgnoringProcessMatch(HWND hwnd, LPARAM param);
static bool IsWindowsVersionOrGreater(unsigned short major_version,
unsigned short minor_version,
unsigned short service_pack);
Expand Down Expand Up @@ -136,6 +139,7 @@ class BrowserFactory {
bool ie_redirects_edge_;

bool edge_ie_mode_;
bool ignore_process_match_;
std::wstring edge_executable_location_;
std::wstring edge_user_data_dir_;
};
Expand Down
11 changes: 11 additions & 0 deletions cpp/iedriver/CommandHandlers/NewSessionCommandHandler.cpp
Expand Up @@ -420,6 +420,16 @@ void NewSessionCommandHandler::SetBrowserFactorySettings(const IECommandExecutor
false);
factory_settings.attach_to_edge_ie = attach_to_edgechrome.asBool();

// Ignore window handle process id match when launching Edge on IE Mode
// Useful when the process is launched with admin/privilege rights. This
// assumes only one Edge browser is running on the host.
factory_settings.ignore_process_match = false;
Json::Value ignore_process_match_ie_mode = this->GetCapability(capabilities,
IGNORE_PROCESS_MATCH,
Json::booleanValue,
false);
factory_settings.ignore_process_match = ignore_process_match_ie_mode.asBool();

Json::Value edge_executable_path = this->GetCapability(capabilities,
EDGE_EXECUTABLE_PATH,
Json::stringValue,
Expand Down Expand Up @@ -564,6 +574,7 @@ Json::Value NewSessionCommandHandler::CreateReturnedCapabilities(const IECommand
ie_options[BROWSER_COMMAND_LINE_SWITCHES_CAPABILITY] = executor.browser_factory()->browser_command_line_switches();
ie_options[FORCE_CREATE_PROCESS_API_CAPABILITY] = executor.browser_factory()->force_createprocess_api();
ie_options[ENSURE_CLEAN_SESSION_CAPABILITY] = executor.browser_factory()->clear_cache();
ie_options[IGNORE_PROCESS_MATCH] = executor.browser_factory()->ignore_process_match();
ie_options[NATIVE_EVENTS_CAPABILITY] = executor.input_manager()->enable_native_events();
ie_options[ENABLE_PERSISTENT_HOVER_CAPABILITY] = executor.input_manager()->use_persistent_hover();
ie_options[ELEMENT_SCROLL_BEHAVIOR_CAPABILITY] = executor.input_manager()->scroll_behavior();
Expand Down
1 change: 1 addition & 0 deletions cpp/iedriver/WebDriverConstants.h
Expand Up @@ -66,6 +66,7 @@
#define ENABLE_FULL_PAGE_SCREENSHOT_CAPABILITY "ie.enableFullPageScreenshot"
#define ATTACH_TO_EDGE_CHROME "ie.edgechromium"
#define EDGE_EXECUTABLE_PATH "ie.edgepath"
#define IGNORE_PROCESS_MATCH "ie.ignoreprocessmatch"

// New top-level browsing context types
#define WINDOW_WINDOW_TYPE "window"
Expand Down

0 comments on commit 6e85abb

Please sign in to comment.