Skip to content

Commit 2dcb34b

Browse files
committed
Improve docs/api for frame handling
One of the pieces of the api that is very well documented is frame/element handling - this commit adds some basic docstrings to the frame methods, and makes the Element::new() method public. For examples I've addedd two more commands to the www binary - `frames` lists iframes in a page with their reference id - `switchframe ID` switches the current context to the selected frame - `switchframe` switches to the toplevel context This still falls short in two areas - there is no way to determine the current frame - AFAIK there is no command for this in the webdriver spec - we are missing a method to switch to the parent frame
1 parent 11aedcf commit 2dcb34b

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/bin/www.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ fn execute_function(name: &str, args: &str, sess: &DriverSession) -> Result<(),
3030
println!("#{} {}", idx, elem.outer_html()?);
3131
}
3232
}
33+
"frames" => {
34+
for (idx, elem) in sess.find_elements("iframe", LocationStrategy::Css)?.iter().enumerate() {
35+
println!("#{} {}", idx, elem.raw_reference());
36+
}
37+
}
3338
"windows" => {
3439
for (idx, handle) in sess.get_window_handles()?.iter().enumerate() {
3540
println!("#{} {}", idx, handle)
@@ -45,6 +50,14 @@ fn execute_function(name: &str, args: &str, sess: &DriverSession) -> Result<(),
4550
other => println!("{}", other),
4651
}
4752
}
53+
"switchframe" => {
54+
let arg = args.trim();
55+
if arg.is_empty() {
56+
try!(sess.switch_to_frame(JsonValue::Null));
57+
} else {
58+
try!(sess.switch_to_frame(try!(Element::new(sess, arg.to_string()).reference())));
59+
}
60+
}
4861
_ => println!("Unknown function: \"{}\"", name),
4962
}
5063
Ok(())

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ impl DriverSession {
320320
Ok(v.value)
321321
}
322322

323+
/// Valid values are element references as returned by Element::reference() or null to switch
324+
/// to the top level frame
323325
pub fn switch_to_frame(&self, handle: JsonValue) -> Result<(), Error> {
324326
let _: Empty = try!(self.client.post(&format!("/session/{}/frame", self.session_id), &SwitchFrameCmd::from(handle)));
325327
Ok(())
@@ -340,7 +342,7 @@ pub struct Element<'a> {
340342
}
341343

342344
impl<'a> Element<'a> {
343-
fn new(s: &'a DriverSession, reference: String) -> Self {
345+
pub fn new(s: &'a DriverSession, reference: String) -> Self {
344346
Element { session: s, reference: reference }
345347
}
346348

@@ -370,11 +372,16 @@ impl<'a> Element<'a> {
370372
Ok(v.value)
371373
}
372374

375+
/// Returns a reference that can be passed on to the API
373376
pub fn reference(&self) -> Result<JsonValue, Error> {
374377
serde_json::to_value(&ElementReference::from_str(&self.reference))
375378
.map_err(|err| Error::from(err))
376379
}
377380

381+
/// The raw reference id that identifies this element, this can be used
382+
/// with Element::new()
383+
pub fn raw_reference(&self) -> &str { &self.reference }
384+
378385
/// Gets the `innerHTML` javascript attribute for this element. Some drivers can get
379386
/// this using regular attributes, in others it does not work. This method gets it
380387
/// executing a bit of javascript.

0 commit comments

Comments
 (0)