Skip to content

Commit

Permalink
added await_promise argument to call_js_fn method
Browse files Browse the repository at this point in the history
  • Loading branch information
ibaryshnikov authored and atroche committed Mar 25, 2019
1 parent 167e6aa commit d82ffa8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/browser/tab/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl<'a> Element<'a> {
pub fn call_js_fn(
&self,
function_declaration: &str,
await_promise: bool,
) -> Result<runtime::methods::RemoteObject, Error> {
let result = self
.parent
Expand All @@ -279,6 +280,7 @@ impl<'a> Element<'a> {
return_by_value: false,
generate_preview: true,
silent: false,
await_promise,
})?
.result;

Expand Down Expand Up @@ -376,7 +378,8 @@ impl<'a> Element<'a> {
}

pub fn get_js_midpoint(&self) -> Result<Point, Error> {
let result = self.call_js_fn("function(){ return this.getBoundingClientRect(); }")?;
let result =
self.call_js_fn("function(){ return this.getBoundingClientRect(); }", false)?;

let properties = result
.preview
Expand Down
1 change: 1 addition & 0 deletions src/protocol/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub mod methods {
pub return_by_value: bool,
pub generate_preview: bool,
pub silent: bool,
pub await_promise: bool,
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
38 changes: 38 additions & 0 deletions tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,44 @@ fn find_elements() -> Result<(), failure::Error> {
Ok(())
}

#[test]
fn call_js_fn_sync() -> Result<(), failure::Error> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let element = tab.wait_for_element("#foobar")?;
let result = element.call_js_fn("function() { return 42 }", false)?;
assert_eq!(result.object_type, "number");
assert_eq!(result.description, Some("42".to_owned()));
assert_eq!(result.value, Some((42).into()));
Ok(())
}

#[test]
fn call_js_fn_async_unresolved() -> Result<(), failure::Error> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let element = tab.wait_for_element("#foobar")?;
let result = element.call_js_fn("async function() { return 42 }", false)?;
assert_eq!(result.object_type, "object");
assert_eq!(result.subtype, Some("promise".to_owned()));
assert_eq!(result.description, Some("Promise".to_owned()));
assert_eq!(result.value, None);
Ok(())
}

#[test]
fn call_js_fn_async_resolved() -> Result<(), failure::Error> {
logging::enable_logging();
let (server, browser, tab) = dumb_server(include_str!("simple.html"));
let element = tab.wait_for_element("#foobar")?;
let result = element.call_js_fn("async function() { return 42 }", true)?;
assert_eq!(result.object_type, "number");
assert_eq!(result.subtype, None);
assert_eq!(result.description, Some("42".to_owned()));
assert_eq!(result.value, Some((42).into()));
Ok(())
}

#[test]
fn set_request_interception() -> Result<(), failure::Error> {
logging::enable_logging();
Expand Down

0 comments on commit d82ffa8

Please sign in to comment.