Struct Browser

Source
pub struct Browser { /* private fields */ }
Expand description

Main crate struct

Contains methods for manipulating the browser session, internally making requests to the selenium server. All methods that create local sessions imply that the selenium server is running on port 4444

Implementations§

Source§

impl Browser

Source

pub fn start_session(browser: BrowserName, args: Vec<&str>) -> Browser

Method to construct the Browser instance with basic session. Supports args for Chrome, while for Firefox and Safari they will be ignored. To customize the Chrome and Firefox sessions pls use the corresponding method start_…_session_with_options().

§Examples
let mut browser = Browser::start_session(BrowserName::Chrome,vec!["--headless"]);
browser.close_browser();
Source

pub fn start_session_with_capabilities( capabilities: Capabilities, ) -> Result<Browser, String>

Allows to create a customized session with various capabilities. For details please check the docs for the Capabilities struct and its methods.

§Examples

let mut ch_op = ChromeOptions::new();
ch_op.add_args(vec!["--headless","--window-size=500,1000"]);
ch_op.add_mobile_emulation(MobileDevice::standard_device("Nexus 6"));

let mut c = Capabilities::new(BrowserName::Chrome, "windows");
c.set_chrome_options(ch_op);

let mut br = Browser::start_session_with_capabilities(c).unwrap();
br.open("https://vk.com").unwrap();
br.take_screenshot("vk.png").unwrap();
br.close_browser().unwrap();
let res = std::fs::read("vk.png");
assert!(res.is_ok());
std::fs::remove_file("vk.png").unwrap();
Source

pub fn start_remote_session_with_capabilities( capabilities: Capabilities, ip: &str, port: &str, ) -> Result<Browser, String>

Does the same thing as the start_session_with_capabilities(),but for the remote session.

Source

pub fn start_remote_session( browser: BrowserName, platform: &str, ip: &str, port: &str, ) -> Result<Browser, String>

Method to construct the Browser instance with basic remote session. Also intended to add chrome/firefox/safari options to the remote sessions.

Source

pub fn start_chrome_session_with_options( options: ChromeOptions, ) -> Result<Browser, String>

Method to start the session customized with ChromeOptions

§Examples
let mut ch = ChromeOptions::new();
let mob = MobileDevice::standard_device("Nexus 6");
ch.add_args(vec!["--headless","--window-size=400,600"]);
ch.add_mobile_emulation(mob);
let mut br = Browser::start_chrome_session_with_options(ch).unwrap();
let res = br.open("https://vk.com");
let res2 = br.close_browser();
assert!(res.is_ok()&&res2.is_ok());
Source

pub fn start_firefox_session_with_options( options: FirefoxOptions, ) -> Result<Browser, String>

Method to start the Firefox session adjusted with FirefoxOptions Works similar to the ChromeOptions. For more info please check the FirefoxOptions docs.

Source

pub fn start_safari_session_with_options( options: SafariOptions, ) -> Result<Browser, String>

Method to start the Safari with settings. Works similar to the ChromeOptions and FFOptions. For more info please check the SafariOptions docs.

Source

pub fn open(&self, uri: &str) -> Result<(), String>

Open a webpage or a local file

Get the url of the current page.

Source

pub fn close_browser(&mut self) -> Result<(), String>

Deletes current session. As the Browser struct needs dropping, it is better to call drop() after this method in long-multibrowser scenarios.

Source

pub fn get_timeouts(&self) -> Result<Timeouts, String>

Returns the session timouts data

Source

pub fn set_timeouts(&self, timeouts: &Timeouts) -> Result<(), &str>

Change the session timouts data

Source

pub fn back(&self) -> Result<(), String>

Return to the previous page

Source

pub fn forward(&self) -> Result<(), String>

Source

pub fn refresh(&self) -> Result<(), &str>

Source

pub fn get_title(&self) -> Result<String, String>

Returns the title of the current tab

Source

pub fn get_window_handle(&self) -> Result<String, String>

Returns the handle of the current window, which later may be used to switch to this window.

Source

pub fn get_window_handles(&self) -> Result<Vec<String>, String>

Returns the handles of all open windows and tabs

Source

pub fn switch_to_window(&self, window_id: String) -> Result<(), String>

Switches to the window with the passed id

Source

pub fn new_window( &self, window_type: NewWindowType, ) -> Result<(String, String), String>

Opens a new window or a new tab depending on the window_type

Source

pub fn close_window(&self) -> Result<Vec<String>, String>

Closes the window and returns the vector of the remaining window handles

Source

pub fn switch_to_frame_by_id(&self, id: u64) -> Result<(), String>

Switches to the frame with a given id. For instance, if there are 4 frames and you wish to switch to the second one, you should call this method like this - switch_to_frame_by_id(1).

Source

pub fn switch_to_frame_by_element(&self, element: Element) -> Result<(), String>

Source

pub fn switch_to_parent_frame(&self) -> Result<(), String>

Source

pub fn get_active_element(&self) -> Result<Element, String>

Source

pub fn find_element( &self, loc_strategy: LocatorStrategy, ) -> Result<Element, String>

If the locator matches several elements, it returns the first one

§Examples

let mut br = Browser::start_session(BrowserName::Chrome,  vec!["--headless"]);
br.open("https://vk.com").unwrap();
let el = br.find_element(LocatorStrategy::CSS("#ts_input")).unwrap();
let res = el.click(); 
br.close_browser().unwrap();
assert!(res.is_ok()); 
Source

pub fn find_elements( &self, loc_strategy: LocatorStrategy, ) -> Result<Vec<Element>, String>

Source

pub fn get_window_rect(&self) -> Result<WindowRect, String>

Returns the WindowRect instance which contains the information about the position and size of the current window

Source

pub fn set_sindow_rect( &self, window_rect: &WindowRect, ) -> Result<WindowRect, String>

Allow to resize the window and change it’s position

Source

pub fn maximize_window(&self) -> Result<WindowRect, String>

Source

pub fn minimize_window(&self) -> Result<WindowRect, String>

Source

pub fn fullscreen(&self) -> Result<WindowRect, String>

Source

pub fn source(&self) -> Result<String, String>

Returns the page source code

Source

pub fn get_all_cookies(&self) -> Result<Vec<Cookie>, String>

Return the vector with all the cookies that the browser is holding at the moment

Returns the information on a particular cookie

Source

pub fn delete_all_cookies(&self) -> Result<(), String>

Source

pub fn take_screenshot(&self, path: &str) -> Result<(), String>

The path should be absolute with the extension

§Examples
let mut br = Browser::start_session(BrowserName::Chrome,  vec!["--headless","--window-size=7680,4320"]);
br.open("https://vk.com").unwrap();
br.take_screenshot("screen.png").unwrap();
br.close_browser().unwrap();
Source

pub fn take_element_screenshot( &self, elem: &Element, path: &str, ) -> Result<(), String>

Source

pub fn execute_sync( &self, script: &str, args: &Vec<&str>, ) -> Result<String, String>

Executes the sync fun in the browser. In case the argument is a string, it should be a raw string or should incluse escapes with double quotes For example, if the args list you want to pass is [5,“Jack”, 15], the vector should be [“5”,r#“Jack”#,“15”]

Source

pub fn execute_async( &self, script: &str, args: &Vec<&str>, ) -> Result<String, String>

Executes the async fun in the browser. The args should be passed similarly to the execute_sync fn.

Source

pub fn print( &self, print_settings: &PrintSettings, path: &str, ) -> Result<(), String>

Prints out the page. If you want to print it to pdf, use headless mode. The structs PrintSettings,Page and Margin allow you to customize the print.

Source§

impl Browser

Source

pub fn dismiss_alert(&self) -> Result<(), String>

Source

pub fn allow_alert(&self) -> Result<(), String>

Source

pub fn get_alert_text(&self) -> Result<String, String>

Source

pub fn send_alert_text(&self, text: &str) -> Result<(), String>

Source

pub fn perform_actions(&self, actions: Actions) -> Result<(), String>

Pls see the Actions struct page to learn how to properly construct the Actions instance.

§Examples
let mut actions = Actions::new();
let mut actions_keys = ActionsKeys::new();
actions_keys.press_special_key(SpecialKey::ShiftLeft);
actions_keys.press_key("a");
actions.add_key_actions(actions_keys);
let mut br = Browser::start_session(BrowserName::Chrome,  vec!["--headless","--window-size=1000,500"]);
br.open("https:vk.com/").unwrap();
br.find_element(LocatorStrategy::CSS("#ts_input")).unwrap().click().unwrap();
br.perform_actions(actions);
br.close_browser().unwrap();
Source

pub fn release_actions(&self) -> Result<(), String>

Releases all the actions present in the current session’s internal state. While the key actions are performed just after calling the perform_actions method, for instance, mouse actions are performed only after calling this method.

Trait Implementations§

Source§

impl Debug for Browser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.