Repository: servo/servo
File: components/webdriver_server/lib.rs (Rust)

Code before:
        self.top_level_script_command(
            WebDriverScriptCommand::GetTitle(sender),
            VerifyBrowsingContextIsOpen::No,
        )?;

        // Step 3. Let title be the session's current top-level
        // browsing context's active document's title.
        let title = wait_for_ipc_response(receiver)?;
        Ok(WebDriverResponse::Generic(ValueResponse(
            serde_json::to_value(title)?,
        )))
    }

    /// <https://w3c.github.io/webdriver/#get-window-handle>
    fn handle_window_handle(&self) -> WebDriverResult<WebDriverResponse> {
        let session = self.session()?;
        // Step 1. If session's current top-level browsing context is no longer open,
        // return error with error code no such window.
        self.verify_top_level_browsing_context_is_open(session.webview_id)?;
        match session.window_handles.get(&session.webview_id) {
            Some(handle) => Ok(WebDriverResponse::Generic(ValueResponse(
                serde_json::to_value(handle)?,
            ))),
            None => Ok(WebDriverResponse::Void),
        }
    }

    /// <https://w3c.github.io/webdriver/#get-window-handles>
    fn handle_window_handles(&self) -> WebDriverResult<WebDriverResponse> {
        let handles = self
            .session()?
            .window_handles
            .values()
            .map(serde_json::to_value)
            .collect::<Result<Vec<_>, _>>()?;
        Ok(WebDriverResponse::Generic(ValueResponse(
            serde_json::to_value(handles)?,
        )))
    }

    /// <https://w3c.github.io/webdriver/#find-element>
    fn handle_find_element(
        &self,
        parameters: &LocatorParameters,
    ) -> WebDriverResult<WebDriverResponse> {
        // Step 1 - 9.
        let res = self.handle_find_elements(parameters)?;
        // Step 10. If result is empty, return error with error code no such element.
        // Otherwise, return the first element of result.
        unwrap_first_element_response(res)
    }

Review comment (bug):
This is more like a work around for those window created by "executing script" with `window.open` failed to be noticed, but I guess we can leave it as is for now.

Code after:

        self.top_level_script_command(
            WebDriverScriptCommand::GetTitle(sender),
            VerifyBrowsingContextIsOpen::No,
        )?;

        // Step 3. Let title be the session's current top-level
        // browsing context's active document's title.
        let title = wait_for_ipc_response(receiver)?;
        Ok(WebDriverResponse::Generic(ValueResponse(
            serde_json::to_value(title)?,
        )))
    }

    /// <https://w3c.github.io/webdriver/#get-window-handle>
    fn handle_window_handle(&mut self) -> WebDriverResult<WebDriverResponse> {
        let webview_id = self.session()?.webview_id;
        let browsing_context_id = self.session()?.browsing_context_id;

        // Step 1. If session's current top-level browsing context is no longer open,
        // return error with error code no such window.
        self.verify_top_level_browsing_context_is_open(webview_id)?;
        let handle = self.get_window_handle(browsing_context_id)?;
        self.session_mut()?
            .window_handles
            .insert(webview_id, handle);

        match self.session()?.window_handles.get(&webview_id) {
            Some(handle) => Ok(WebDriverResponse::Generic(ValueResponse(
                serde_json::to_value(handle)?,
            ))),
            None => Ok(WebDriverResponse::Void),
        }
    }

    /// <https://w3c.github.io/webdriver/#get-window-handles>
    fn handle_window_handles(&mut self) -> WebDriverResult<WebDriverResponse> {
        self.handle_any_user_prompts(self.session()?.webview_id)?;

        self.session_mut()?.window_handles = self.get_window_handles()?;

        let handles = self
            .session()?
            .window_handles
            .values()
            .map(serde_json::to_value)
            .collect::<Result<Vec<_>, _>>()?;

        Ok(WebDriverResponse::Generic(ValueResponse(
            serde_json::to_value(handles)?,
        )))