Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/cli-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ steel browser
- `mouse`: Mouse input at a point, for canvases and maps
- `snapshot`: Take an accessibility tree snapshot with element refs
- `screenshot`: Take a screenshot
- `pdf`: Save the page as a PDF (headless sessions only)
- `pdf`: Save the page as a PDF
- `eval`: Run JavaScript in the page and print its value
- `find`: Find elements and print their refs
- `content`: Get the page HTML
Expand All @@ -238,7 +238,7 @@ steel browser start

- `--stealth` (boolean, optional): Enable stealth mode (humanize interactions + auto CAPTCHA)
- `-p, --proxy` (string, optional): Use a residential proxy
- `--timeout` (string, optional): Session timeout in milliseconds (create-time only)
- `--timeout` (string, optional): Session timeout in milliseconds. Defaults to 900000 (15 minutes), which every plan allows (create-time only)
- `--inactivity-timeout` (string, optional): Inactivity timeout in milliseconds: release the session when no command, CDP message, or remote input arrives for this long. Defaults to 600000 (10 minutes); pass 0 to disable (create-time only)
- `--solve-captcha` (boolean, optional): Enable manual CAPTCHA solving on new sessions (create-time only)
- `--profile` (string, optional): Named profile to load (see `steel profile list`)
Expand Down Expand Up @@ -698,7 +698,7 @@ steel browser screenshot

## steel browser pdf

Save the page as a PDF (headless sessions only)
Save the page as a PDF

### Usage

Expand All @@ -725,6 +725,7 @@ steel browser eval
### Parameters

- `script` (string, required): JavaScript expression or function body; its value is printed
- `--timeout` (string, optional): Time limit in milliseconds (default 10000, at most 50000)

## steel browser find

Expand Down
6 changes: 4 additions & 2 deletions docs/references/steel-browser-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ steel browser get box <sel>
steel browser is visible|enabled|checked <sel>
steel browser find <value> # CSS by default; --by role|text|label|placeholder|alt|title|testid, --name, --exact
steel browser content # page HTML
steel browser eval <script> # run JavaScript and print its value
steel browser eval <script> # run JavaScript and print its value; --timeout <ms> (at most 50000)
steel browser screenshot # -o <path>, --full, --selector <sel>, --format, --quality
steel browser pdf -o page.pdf # headless sessions only
steel browser pdf -o page.pdf # --landscape, --print-background
```

## Waiting
Expand Down Expand Up @@ -118,6 +118,8 @@ steel browser status x-7 # wait for a command that is still runnin
steel browser cancel x-7 # stop it
```

When the CLI stops waiting for a command that is still running, it prints the execution id for these two commands. Ctrl-C while a command runs cancels it. Press Ctrl-C again to stop waiting for the cancel.

Commands cannot answer dialogs. A dialog stays open until it is closed, and commands on its tab fail with `dialog_pending` until then. The CLI prints the dialog id and its tab. Close the dialog in the live view (`steel browser live` prints its URL), or close its tab with `steel browser tab close <tab>`. The last open tab cannot be closed, so open another one first with `steel browser tab new`. `steel browser dialog` was removed and prints this advice.

## Batch
Expand Down
81 changes: 71 additions & 10 deletions src/api/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::time::Duration;

use serde_json::Value;
use tokio::sync::watch;

use crate::api::client::{ApiError, SteelClient};

Expand Down Expand Up @@ -42,6 +43,11 @@ impl Reply {
}
}

pub enum Submitted {
Reply(Reply),
Stopped { maybe_ran: bool },
}

pub struct Session<'a> {
pub client: &'a SteelClient,
pub id: &'a str,
Expand All @@ -57,6 +63,13 @@ const fn backoff(attempt: u32) -> Duration {
Duration::from_millis(250 * 4u64.pow(attempt.saturating_sub(1)))
}

async fn pause(duration: Duration, stop: &mut watch::Receiver<bool>) {
tokio::select! {
() = tokio::time::sleep(duration) => {}
Ok(_) = stop.wait_for(|stopped| *stopped) => {}
}
}

impl Session<'_> {
fn path(&self, suffix: &str) -> String {
format!("/sessions/{}{suffix}", self.id)
Expand Down Expand Up @@ -111,13 +124,20 @@ impl Session<'_> {
})
}

pub async fn run(&self, mut body: Value) -> Result<Reply, ApiError> {
pub async fn submit(
&self,
mut body: Value,
mut stop: watch::Receiver<bool>,
) -> Result<Submitted, ApiError> {
let mut key = new_key();
let mut maybe_ran = false;
let mut delivery = 0;
let mut busy = 0;
let mut renewed = false;
loop {
if *stop.borrow() {
return Ok(Submitted::Stopped { maybe_ran });
}
let reply = match self.post(&body, Some(&key)).await {
Ok(reply) => reply,
Err(err @ ApiError::Unreachable { .. }) => {
Expand All @@ -126,7 +146,7 @@ impl Session<'_> {
return Err(err);
}
delivery += 1;
tokio::time::sleep(backoff(delivery)).await;
pause(backoff(delivery), &mut stop).await;
continue;
}
Err(err) => return Err(err),
Expand All @@ -135,23 +155,22 @@ impl Session<'_> {
(503, _) if delivery < DELIVERY_RETRIES => {
maybe_ran = true;
delivery += 1;
tokio::time::sleep(backoff(delivery)).await;
pause(backoff(delivery), &mut stop).await;
}
(429, _) if busy < BUSY_RETRIES => {
busy += 1;
let wait = reply.retry_after.unwrap_or(1).min(MAX_RETRY_AFTER_SECS);
tokio::time::sleep(Duration::from_secs(wait)).await;
pause(Duration::from_secs(wait), &mut stop).await;
}
(422, Some("invalid_operation_token")) if !maybe_ran && !renewed => {
let Some(next) = reply.body.get("next").cloned() else {
return Ok(reply);
return Ok(Submitted::Reply(reply));
};
body["after"] = next;
key = new_key();
renewed = true;
}
(200, _) => return self.settle(reply).await,
_ => return Ok(reply),
_ => return Ok(Submitted::Reply(reply)),
}
}
}
Expand Down Expand Up @@ -183,7 +202,7 @@ impl Session<'_> {
}
}

async fn settle(&self, mut reply: Reply) -> Result<Reply, ApiError> {
pub async fn settle(&self, mut reply: Reply) -> Result<Reply, ApiError> {
for _ in 0..STATUS_POLLS {
if !reply.is_pending() {
return Ok(reply);
Expand Down Expand Up @@ -318,7 +337,11 @@ mod tests {
client: &client,
id: ID,
};
session.run(body).await
let (_stop, stopping) = watch::channel(false);
match session.submit(body, stopping).await? {
Submitted::Reply(reply) => session.settle(reply).await,
Submitted::Stopped { .. } => panic!("nothing asked the submit to stop"),
}
}

fn commands_path() -> String {
Expand Down Expand Up @@ -432,6 +455,43 @@ mod tests {
assert_eq!(keys[0], keys[1]);
}

#[tokio::test]
async fn stops_instead_of_retrying_once_asked() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path(commands_path()))
.respond_with(
ResponseTemplate::new(429)
.insert_header("retry-after", "5")
.set_body_json(json!({"error": {"code": "queue_full", "message": "busy"}})),
)
.expect(1)
.mount(&server)
.await;
let client = SteelClient::with(server.uri(), ApiMode::Cloud, auth()).unwrap();
let session = Session {
client: &client,
id: ID,
};
let (stop, stopping) = watch::channel(false);
let asked = async {
while server.received_requests().await.unwrap().is_empty() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
stop.send_replace(true);
};
let started = std::time::Instant::now();
let (submitted, ()) = tokio::join!(
session.submit(json!({"commands": [{"command": "reload"}]}), stopping),
asked
);
assert!(matches!(
submitted,
Ok(Submitted::Stopped { maybe_ran: false })
));
assert!(started.elapsed() < Duration::from_secs(4));
}

#[tokio::test]
async fn renews_an_invalid_after_on_the_first_try() {
let server = MockServer::start().await;
Expand Down Expand Up @@ -591,7 +651,8 @@ mod tests {
client: &client,
id: ID,
};
let result = session.run(json!({"commands": []})).await;
let (_stop, stopping) = watch::channel(false);
let result = session.submit(json!({"commands": []}), stopping).await;
assert!(matches!(result, Err(ApiError::MissingAuth)));
}

Expand Down
Loading