[][src]Trait thirtyfour_sync::WebDriverCommands

pub trait WebDriverCommands {
    fn session(&self) -> &WebDriverSession;

    fn cmd(&self, command: Command<'_>) -> WebDriverResult<Value> { ... }
fn close(&self) -> WebDriverResult<()> { ... }
fn get<S: Into<String>>(&self, url: S) -> WebDriverResult<()> { ... }
fn current_url(&self) -> WebDriverResult<String> { ... }
fn page_source(&self) -> WebDriverResult<String> { ... }
fn title(&self) -> WebDriverResult<String> { ... }
fn find_element(&self, by: By<'_>) -> WebDriverResult<WebElement<'_>> { ... }
fn find_elements(&self, by: By<'_>) -> WebDriverResult<Vec<WebElement<'_>>> { ... }
fn execute_script(&self, script: &str) -> WebDriverResult<ScriptRetSync<'_>> { ... }
fn execute_script_with_args(
        &self,
        script: &str,
        args: &ScriptArgs
    ) -> WebDriverResult<ScriptRetSync<'_>> { ... }
fn execute_async_script(
        &self,
        script: &str
    ) -> WebDriverResult<ScriptRetSync<'_>> { ... }
fn execute_async_script_with_args(
        &self,
        script: &str,
        args: &ScriptArgs
    ) -> WebDriverResult<ScriptRetSync<'_>> { ... }
fn current_window_handle(&self) -> WebDriverResult<WindowHandle> { ... }
fn window_handles(&self) -> WebDriverResult<Vec<WindowHandle>> { ... }
fn maximize_window(&self) -> WebDriverResult<()> { ... }
fn minimize_window(&self) -> WebDriverResult<()> { ... }
fn fullscreen_window(&self) -> WebDriverResult<()> { ... }
fn get_window_rect(&self) -> WebDriverResult<Rect> { ... }
fn set_window_rect(&self, rect: OptionRect) -> WebDriverResult<()> { ... }
fn back(&self) -> WebDriverResult<()> { ... }
fn forward(&self) -> WebDriverResult<()> { ... }
fn refresh(&self) -> WebDriverResult<()> { ... }
fn get_timeouts(&self) -> WebDriverResult<TimeoutConfiguration> { ... }
fn set_timeouts(
        &self,
        timeouts: TimeoutConfiguration
    ) -> WebDriverResult<()> { ... }
fn set_implicit_wait_timeout(
        &self,
        time_to_wait: Duration
    ) -> WebDriverResult<()> { ... }
fn set_script_timeout(&self, time_to_wait: Duration) -> WebDriverResult<()> { ... }
fn set_page_load_timeout(
        &self,
        time_to_wait: Duration
    ) -> WebDriverResult<()> { ... }
fn action_chain(&self) -> ActionChain<'_> { ... }
fn get_cookies(&self) -> WebDriverResult<Vec<Cookie>> { ... }
fn get_cookie(&self, name: &str) -> WebDriverResult<Cookie> { ... }
fn delete_cookie(&self, name: &str) -> WebDriverResult<()> { ... }
fn delete_all_cookies(&self) -> WebDriverResult<()> { ... }
fn add_cookie(&self, cookie: Cookie) -> WebDriverResult<()> { ... }
fn screenshot_as_base64(&self) -> WebDriverResult<String> { ... }
fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>> { ... }
fn screenshot(&self, path: &Path) -> WebDriverResult<()> { ... }
fn switch_to(&self) -> SwitchTo<'_> { ... }
fn set_window_name(&self, window_name: &str) -> WebDriverResult<()> { ... }
fn extension_command<T: ExtensionCommand + Send>(
        &self,
        ext_cmd: T
    ) -> WebDriverResult<Value> { ... } }

All browser-level W3C WebDriver commands are implemented under this trait.

Thirtyfour is structured as follows:

  • The WebDriverCommands trait contains all of the methods you would typically call in order to interact with the browser.
  • The GenericWebDriver struct implements the WebDriverCommands trait for a generic HTTP client.
  • The WebDriver struct is the GenericWebDriver implemented for a specific HTTP client.

You only need to use WebDriver in your code. Just create an instance of the WebDriver struct and it will have access to all of the methods from the WebDriverCommands trait.

For example:

let caps = DesiredCapabilities::chrome();
let driver = WebDriver::new("http://localhost:4444/wd/hub", &caps)?;
driver.get("http://webappdemo")?;
assert_eq!(driver.current_url()?, "http://webappdemo/");

Required methods

fn session(&self) -> &WebDriverSession

Get the current session and http client.

For thirtyfour internal use only.

Loading content...

Provided methods

fn cmd(&self, command: Command<'_>) -> WebDriverResult<Value>

Convenience wrapper for running WebDriver commands.

For thirtyfour internal use only.

fn close(&self) -> WebDriverResult<()>

Close the current window or tab.

Example:

// Open a new tab.
driver.execute_script(r#"window.open("about:blank", target="_blank");"#)?;
// Get window handles and switch to the new tab.
let handles = driver.window_handles()?;
driver.switch_to().window(&handles[1])?;
// We are now controlling the new tab.
driver.get("http://webappdemo")?;
// Close the tab. This will return to the original tab.
driver.close()?;

fn get<S: Into<String>>(&self, url: S) -> WebDriverResult<()>

Navigate to the specified URL.

Example:

driver.get("http://webappdemo")?;

fn current_url(&self) -> WebDriverResult<String>

Get the current URL as a String.

Example:

driver.get("http://webappdemo")?;
let url = driver.current_url()?;

fn page_source(&self) -> WebDriverResult<String>

Get the page source as a String.

Example:

driver.get("http://webappdemo")?;
let source = driver.page_source()?;

fn title(&self) -> WebDriverResult<String>

Get the page title as a String.

Example:

driver.get("http://webappdemo")?;
let title = driver.title()?;

fn find_element(&self, by: By<'_>) -> WebDriverResult<WebElement<'_>>

Search for an element on the current page using the specified selector.

Example:

let elem_text = driver.find_element(By::Name("input1"))?;
let elem_button = driver.find_element(By::Id("button-set"))?;
let elem_result = driver.find_element(By::Id("input-result"))?;

fn find_elements(&self, by: By<'_>) -> WebDriverResult<Vec<WebElement<'_>>>

Search for all elements on the current page that match the specified selector.

Example:

let elems = driver.find_elements(By::ClassName("section"))?;
for elem in elems {
    assert!(elem.get_attribute("class")?.contains("section"));
}

fn execute_script(&self, script: &str) -> WebDriverResult<ScriptRetSync<'_>>

Execute the specified Javascript synchronously and return the result.

Example:

let ret = driver.execute_script(r#"
    let elem = document.getElementById("button1");
    elem.click();
    return elem;
    "#
)?;
let elem_out = ret.get_element()?;
assert_eq!(elem_out.text()?, "BUTTON 1");
let elem = driver.find_element(By::Id("button-result"))?;
assert_eq!(elem.text()?, "Button 1 clicked");

fn execute_script_with_args(
    &self,
    script: &str,
    args: &ScriptArgs
) -> WebDriverResult<ScriptRetSync<'_>>

Execute the specified Javascript synchronously and return the result.

Example:

let elem = driver.find_element(By::Id("button1"))?;
let mut args = ScriptArgs::new();
args.push(elem.clone())?;
args.push("TESTING")?;
let ret = driver.execute_script_with_args(r#"
    arguments[0].innerHTML = arguments[1];
    return arguments[0];
    "#, &args
)?;
let elem_out = ret.get_element()?;
assert_eq!(elem_out.element_id, elem.element_id);
assert_eq!(elem_out.text()?, "TESTING");

fn execute_async_script(
    &self,
    script: &str
) -> WebDriverResult<ScriptRetSync<'_>>

Execute the specified Javascrypt asynchronously and return the result.

Example:

let ret = driver.execute_async_script(r#"
    // Selenium automatically provides an extra argument which is a
    // function that receives the return value(s).
    let done = arguments[0];
    window.setTimeout(() => {
        let elem = document.getElementById("button1");
        elem.click();
        done(elem);
    }, 1000);
    "#
)?;
let elem_out = ret.get_element()?;
assert_eq!(elem_out.text()?, "BUTTON 1");
let elem = driver.find_element(By::Id("button-result"))?;
assert_eq!(elem.text()?, "Button 1 clicked");

fn execute_async_script_with_args(
    &self,
    script: &str,
    args: &ScriptArgs
) -> WebDriverResult<ScriptRetSync<'_>>

Execute the specified Javascrypt asynchronously and return the result.

Example:

let elem = driver.find_element(By::Id("button1"))?;
let mut args = ScriptArgs::new();
args.push(elem.clone())?;
args.push("TESTING")?;
let ret = driver.execute_async_script_with_args(r#"
    // Selenium automatically provides an extra argument which is a
    // function that receives the return value(s).
    let done = arguments[2];
    window.setTimeout(() => {
        arguments[0].innerHTML = arguments[1];
        done(arguments[0]);
    }, 1000);
    "#, &args
)?;
let elem_out = ret.get_element()?;
assert_eq!(elem_out.element_id, elem.element_id);
assert_eq!(elem_out.text()?, "TESTING");

fn current_window_handle(&self) -> WebDriverResult<WindowHandle>

Get the current window handle.

Example:

// Get the current window handle.
let handle = driver.current_window_handle()?;
// Open a new tab.
driver.execute_script(r#"window.open("about:blank", target="_blank");"#)?;
// Get window handles and switch to the new tab.
let handles = driver.window_handles()?;
driver.switch_to().window(&handles[1])?;
// We are now controlling the new tab.
driver.get("http://webappdemo")?;
assert_ne!(driver.current_window_handle()?, handle);
// Switch back to original tab.
driver.switch_to().window(&handle)?;
assert_eq!(driver.current_window_handle()?, handle);

fn window_handles(&self) -> WebDriverResult<Vec<WindowHandle>>

Get all window handles for the current session.

Example:

assert_eq!(driver.window_handles()?.len(), 1);
// Open a new tab.
driver.execute_script(r#"window.open("about:blank", target="_blank");"#)?;
// Get window handles and switch to the new tab.
let handles = driver.window_handles()?;
assert_eq!(handles.len(), 2);
driver.switch_to().window(&handles[1])?;

fn maximize_window(&self) -> WebDriverResult<()>

Maximize the current window.

Example:

driver.maximize_window()?;

fn minimize_window(&self) -> WebDriverResult<()>

Minimize the current window.

Example:

This example is not tested
driver.minimize_window()?;

fn fullscreen_window(&self) -> WebDriverResult<()>

Make the current window fullscreen.

Example:

driver.fullscreen_window()?;

fn get_window_rect(&self) -> WebDriverResult<Rect>

Get the current window rectangle, in pixels.

The returned Rect struct has members x, y, width, height, all i32.

Example:

use thirtyfour::OptionRect;
let option_rect = OptionRect::new().with_pos(1, 1).with_size(800, 600);
driver.set_window_rect(option_rect.clone())?;
let rect = driver.get_window_rect()?;
assert_eq!(OptionRect::from(rect), option_rect);

fn set_window_rect(&self, rect: OptionRect) -> WebDriverResult<()>

Set the current window rectangle, in pixels.

This requires an OptionRect, which is similar to Rect except all members are wrapped in Option.

Example:

use thirtyfour::OptionRect;
let r = OptionRect::new().with_size(1280, 720);
driver.set_window_rect(r)?;

You can also convert from a Rect if you want to get the window size and modify it before setting it again.

use thirtyfour::OptionRect;
let rect = driver.get_window_rect()?;
let option_rect = OptionRect::from(rect);
driver.set_window_rect(option_rect.with_width(1024))?;

fn back(&self) -> WebDriverResult<()>

Go back. This is equivalent to clicking the browser's back button.

Example:

driver.back()?;

fn forward(&self) -> WebDriverResult<()>

Go forward. This is equivalent to clicking the browser's forward button.

Example:

driver.forward()?;

fn refresh(&self) -> WebDriverResult<()>

Refresh the current page.

Example:

driver.refresh()?;

fn get_timeouts(&self) -> WebDriverResult<TimeoutConfiguration>

Get all timeouts for the current session.

Example:

use thirtyfour::TimeoutConfiguration;
use std::time::Duration;

let timeouts = driver.get_timeouts()?;
println!("Page load timeout = {:?}", timeouts.page_load());

fn set_timeouts(&self, timeouts: TimeoutConfiguration) -> WebDriverResult<()>

Set all timeouts for the current session.

Example:

use thirtyfour::TimeoutConfiguration;
use std::time::Duration;

// Setting timeouts to None means those timeout values will not be updated.
let timeouts = TimeoutConfiguration::new(None, Some(Duration::new(11, 0)), None);
driver.set_timeouts(timeouts.clone())?;

fn set_implicit_wait_timeout(
    &self,
    time_to_wait: Duration
) -> WebDriverResult<()>

Set the implicit wait timeout. This is how long the WebDriver will wait when querying elements.

By default this is set to 30 seconds.

NOTE: Depending on the kind of testing you want to do, you may find it more reliable to set the implicit wait time to 0 (no wait) and implement your own polling loop outside of thirtyfour.

Example:

use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
let delay = Duration::new(11, 0);
driver.set_implicit_wait_timeout(delay)?;

fn set_script_timeout(&self, time_to_wait: Duration) -> WebDriverResult<()>

Set the script timeout. This is how long the WebDriver will wait for a Javascript script to execute.

By default this is set to 60 seconds.

Example:

use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
let delay = Duration::new(11, 0);
driver.set_script_timeout(delay)?;

fn set_page_load_timeout(&self, time_to_wait: Duration) -> WebDriverResult<()>

Set the page load timeout. This is how long the WebDriver will wait for the page to finish loading.

By default this is set to 60 seconds.

Example:

use thirtyfour::TimeoutConfiguration;
use std::time::Duration;
let delay = Duration::new(11, 0);
driver.set_page_load_timeout(delay)?;

fn action_chain(&self) -> ActionChain<'_>

Create a new action chain for this session. Action chains can be used to simulate more complex user input actions involving key combinations, mouse movements, mouse click, right-click, and more.

Example:

let elem_text = driver.find_element(By::Name("input1"))?;
let elem_button = driver.find_element(By::Id("button-set"))?;

driver.action_chain()
    .send_keys_to_element(&elem_text, "thirtyfour")
    .move_to_element_center(&elem_button)
    .click()
    .perform()?;

fn get_cookies(&self) -> WebDriverResult<Vec<Cookie>>

Get all cookies.

Example:

let cookies = driver.get_cookies()?;
for cookie in &cookies {
    println!("Got cookie: {}", cookie.value());
}

Get the specified cookie.

Example:

let cookie = driver.get_cookie("key")?;
println!("Got cookie: {}", cookie.value());

Delete the specified cookie.

Example:

driver.delete_cookie("key")?;

fn delete_all_cookies(&self) -> WebDriverResult<()>

Delete all cookies.

Example:

driver.delete_all_cookies()?;

Add the specified cookie.

Example:

let cookie = Cookie::new("key", serde_json::json!("value"));
driver.add_cookie(cookie)?;

fn screenshot_as_base64(&self) -> WebDriverResult<String>

Take a screenshot of the current window and return it as a base64-encoded String.

fn screenshot_as_png(&self) -> WebDriverResult<Vec<u8>>

Take a screenshot of the current window and return it as PNG bytes.

fn screenshot(&self, path: &Path) -> WebDriverResult<()>

Take a screenshot of the current window and write it to the specified filename.

fn switch_to(&self) -> SwitchTo<'_>

Return a SwitchTo struct for switching to another window or frame.

fn set_window_name(&self, window_name: &str) -> WebDriverResult<()>

Set the current window name. Useful for switching between windows/tabs using driver.switch_to().window_name(name).

Example:

// Get the current window handle.
let handle = driver.current_window_handle()?;
driver.set_window_name("main")?;
// Open a new tab.
driver.execute_script(r#"window.open("about:blank", target="_blank");"#)?;
// Get window handles and switch to the new tab.
let handles = driver.window_handles()?;
driver.switch_to().window(&handles[1])?;
// We are now controlling the new tab.
driver.get("http://webappdemo")?;
assert_ne!(driver.current_window_handle()?, handle);
// Switch back to original tab using window name.
driver.switch_to().window_name("main")?;
assert_eq!(driver.current_window_handle()?, handle);

fn extension_command<T: ExtensionCommand + Send>(
    &self,
    ext_cmd: T
) -> WebDriverResult<Value>

Running an extension command. Extension commands are browser specific commands and using browser specific endpoints and parameters.

Example

use serde::Serialize;
use thirtyfour_sync::prelude::*;
use thirtyfour::{ExtensionCommand, RequestMethod};

#[derive(Serialize)]
pub struct AddonInstallCommand {
   pub path: String,
   pub temporary: Option<bool>,
}

impl ExtensionCommand for AddonInstallCommand {
   fn parameters_json(&self) -> Option<serde_json::Value> {
       Some(serde_json::to_value(self).unwrap())
   }
   fn method(&self) -> RequestMethod {
       RequestMethod::Post
   }

   fn endpoint(&self) -> String {
       String::from("/moz/addon/install")
   }
}

fn main()-> WebDriverResult<()> {
    let caps = DesiredCapabilities::firefox();
    let driver = WebDriver::new("http://localhost:4444", &caps)?;

    let install_command = AddonInstallCommand {
        path: String::from("/path/to/addon.xpi"),
        temporary: Some(true),
    };

    let response = driver.extension_command(install_command)?;

    assert_eq!(response.is_string(), true);

    Ok(())
}
Loading content...

Implementors

impl WebDriverCommands for WebDriverSession[src]

impl<T> WebDriverCommands for GenericWebDriver<T> where
    T: WebDriverHttpClientSync
[src]

Loading content...