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
impl Browser
Sourcepub fn start_session(browser: BrowserName, args: Vec<&str>) -> Browser
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();
Sourcepub fn start_session_with_capabilities(
capabilities: Capabilities,
) -> Result<Browser, String>
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();
Sourcepub fn start_remote_session_with_capabilities(
capabilities: Capabilities,
ip: &str,
port: &str,
) -> Result<Browser, String>
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.
Sourcepub fn start_remote_session(
browser: BrowserName,
platform: &str,
ip: &str,
port: &str,
) -> Result<Browser, String>
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.
Sourcepub fn start_chrome_session_with_options(
options: ChromeOptions,
) -> Result<Browser, String>
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());
Sourcepub fn start_firefox_session_with_options(
options: FirefoxOptions,
) -> Result<Browser, String>
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.
Sourcepub fn start_safari_session_with_options(
options: SafariOptions,
) -> Result<Browser, String>
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.
Sourcepub fn close_browser(&mut self) -> Result<(), String>
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.
Sourcepub fn get_timeouts(&self) -> Result<Timeouts, String>
pub fn get_timeouts(&self) -> Result<Timeouts, String>
Returns the session timouts data
Sourcepub fn set_timeouts(&self, timeouts: &Timeouts) -> Result<(), &str>
pub fn set_timeouts(&self, timeouts: &Timeouts) -> Result<(), &str>
Change the session timouts data
pub fn forward(&self) -> Result<(), String>
pub fn refresh(&self) -> Result<(), &str>
Sourcepub fn get_window_handle(&self) -> Result<String, String>
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.
Sourcepub fn get_window_handles(&self) -> Result<Vec<String>, String>
pub fn get_window_handles(&self) -> Result<Vec<String>, String>
Returns the handles of all open windows and tabs
Sourcepub fn switch_to_window(&self, window_id: String) -> Result<(), String>
pub fn switch_to_window(&self, window_id: String) -> Result<(), String>
Switches to the window with the passed id
Sourcepub fn new_window(
&self,
window_type: NewWindowType,
) -> Result<(String, String), String>
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
Sourcepub fn close_window(&self) -> Result<Vec<String>, String>
pub fn close_window(&self) -> Result<Vec<String>, String>
Closes the window and returns the vector of the remaining window handles
Sourcepub fn switch_to_frame_by_id(&self, id: u64) -> Result<(), String>
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).
pub fn switch_to_frame_by_element(&self, element: Element) -> Result<(), String>
pub fn switch_to_parent_frame(&self) -> Result<(), String>
pub fn get_active_element(&self) -> Result<Element, String>
Sourcepub fn find_element(
&self,
loc_strategy: LocatorStrategy,
) -> Result<Element, String>
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());
pub fn find_elements( &self, loc_strategy: LocatorStrategy, ) -> Result<Vec<Element>, String>
Sourcepub fn get_window_rect(&self) -> Result<WindowRect, String>
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
Sourcepub fn set_sindow_rect(
&self,
window_rect: &WindowRect,
) -> Result<WindowRect, String>
pub fn set_sindow_rect( &self, window_rect: &WindowRect, ) -> Result<WindowRect, String>
Allow to resize the window and change it’s position
pub fn maximize_window(&self) -> Result<WindowRect, String>
pub fn minimize_window(&self) -> Result<WindowRect, String>
pub fn fullscreen(&self) -> Result<WindowRect, String>
Return the vector with all the cookies that the browser is holding at the moment
Returns the information on a particular cookie
Sourcepub fn take_screenshot(&self, path: &str) -> Result<(), String>
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();
pub fn take_element_screenshot( &self, elem: &Element, path: &str, ) -> Result<(), String>
Sourcepub fn execute_sync(
&self,
script: &str,
args: &Vec<&str>,
) -> Result<String, String>
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§impl Browser
impl Browser
pub fn dismiss_alert(&self) -> Result<(), String>
pub fn allow_alert(&self) -> Result<(), String>
pub fn get_alert_text(&self) -> Result<String, String>
pub fn send_alert_text(&self, text: &str) -> Result<(), String>
Sourcepub fn perform_actions(&self, actions: Actions) -> Result<(), String>
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();
Sourcepub fn release_actions(&self) -> Result<(), String>
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.