Publish final URL after navigation

If the URL specified in a navigation command results in a redirect, we
want to publish the final destination, rather than the provided
location.  Thus, after navigation completes, we get the browser's
current URL and publish that instead.
dev/ci
Dustin 2022-12-31 10:43:01 -06:00
parent 8431a33f20
commit 4eba92f4a0
3 changed files with 25 additions and 4 deletions

View File

@ -69,6 +69,13 @@ pub struct GetTitleResponse {
pub value: String,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
pub struct GetCurrentUrlResponse {
pub value: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
@ -85,4 +92,6 @@ pub enum Command {
GetTitle,
#[serde(rename = "WebDriver:Navigate")]
Navigate(NavigateParams),
#[serde(rename = "WebDriver:GetCurrentURL")]
GetCurrentUrl,
}

View File

@ -18,8 +18,8 @@ use tracing::{debug, error, trace, warn};
pub use error::{CommandError, ConnectionError, ErrorResponse, MessageError};
use message::{
Command, GetTitleResponse, Hello, NavigateParams, NewSessionParams,
NewSessionResponse,
Command, GetCurrentUrlResponse, GetTitleResponse, Hello, NavigateParams,
NewSessionParams, NewSessionResponse,
};
#[derive(Debug, Deserialize, Serialize)]
@ -174,6 +174,13 @@ impl Marionette {
Ok(res.value)
}
pub async fn get_current_url(&mut self) -> Result<String, CommandError> {
let res: GetCurrentUrlResponse =
self.conn.send_message(Command::GetCurrentUrl).await?.unwrap();
debug!("Received message: {:?}", res);
Ok(res.value)
}
pub async fn navigate<U>(&mut self, url: U) -> Result<(), CommandError>
where
U: Into<String>,

View File

@ -136,8 +136,13 @@ impl<'a> crate::mqtt::MessageHandler for MessageHandler<'a> {
if let Err(e) = self.marionette.navigate(url.to_string()).await {
error!("Failed to navigate: {}", e);
}
if let Err(e) = publisher.publish_url(screen, &url).await {
error!("Failed to publish title: {}", e);
match self.marionette.get_current_url().await {
Ok(u) => {
if let Err(e) = publisher.publish_url(screen, &u).await {
error!("Failed to publish URL: {}", e);
}
}
Err(e) => error!("Failed to get current browser URL: {}", e),
}
match self.marionette.get_title().await {
Ok(t) => {