Navigate to a URL, go back/forward in history, or reload the page.
(
client: &mut CdpClient,
session_id: &str,
url: Option<&str>,
back: bool,
forward: bool,
reload: bool,
extra_headers: Option<&str>,
output: Option<&str>,
)
| 7 | |
| 8 | /// Navigate to a URL, go back/forward in history, or reload the page. |
| 9 | pub async fn navigate( |
| 10 | client: &mut CdpClient, |
| 11 | session_id: &str, |
| 12 | url: Option<&str>, |
| 13 | back: bool, |
| 14 | forward: bool, |
| 15 | reload: bool, |
| 16 | extra_headers: Option<&str>, |
| 17 | output: Option<&str>, |
| 18 | ) -> Result<CommandResult> { |
| 19 | // Validate navigation intent before mutating session state |
| 20 | let intent_count = [back, forward, reload, url.is_some()] |
| 21 | .iter() |
| 22 | .filter(|&&b| b) |
| 23 | .count(); |
| 24 | if intent_count == 0 { |
| 25 | bail!("URL required (or use --back, --forward, --reload)"); |
| 26 | } |
| 27 | if intent_count > 1 { |
| 28 | bail!("Conflicting navigation intents: only one of URL, --back, --forward, or --reload can be specified"); |
| 29 | } |
| 30 | |
| 31 | super::pages::apply_extra_headers(client, session_id, extra_headers).await?; |
| 32 | |
| 33 | let navigate_result = async { |
| 34 | if back { |
| 35 | return go_back(client, session_id, output).await; |
| 36 | } |
| 37 | if forward { |
| 38 | return go_forward(client, session_id, output).await; |
| 39 | } |
| 40 | if reload { |
| 41 | return do_reload(client, session_id, output).await; |
| 42 | } |
| 43 | |
| 44 | let url = url.unwrap(); // safe: validated above |
| 45 | |
| 46 | let result = client |
| 47 | .send_to_target(session_id, "Page.navigate", json!({"url": url})) |
| 48 | .await?; |
| 49 | |
| 50 | if let Some(err) = result.get("errorText").and_then(|v| v.as_str()) { |
| 51 | bail!("Navigation error: {err}"); |
| 52 | } |
| 53 | |
| 54 | wait_for_load(client, session_id, NAVIGATION_TIMEOUT_MS).await?; |
| 55 | let result = |
| 56 | CommandResult::output(format!("Navigated to {url}")).with_navigated_to(url.to_string()); |
| 57 | Ok(result.save_output(output).await?) |
| 58 | } |
| 59 | .await; |
| 60 | |
| 61 | // Only clear headers that this invocation applied. |
| 62 | if extra_headers.is_some() { |
| 63 | if let Err(e) = super::pages::clear_extra_headers(client, session_id).await { |
| 64 | eprintln!("Warning: failed to clear extra headers: {e}"); |
| 65 | } |
| 66 | } |
no test coverage detected