Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrtgs committed Sep 11, 2023
2 parents 32e9166 + 86bd74c commit aa118c2
Show file tree
Hide file tree
Showing 23 changed files with 377 additions and 608 deletions.
4 changes: 2 additions & 2 deletions thirtyfour/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ component = ["thirtyfour-macros"]
async-trait = "0.1.56"
base64 = "0.21.3"
cookie = { version = "0.17.0", features = ["percent-encode"] }
fantoccini = { version = "0.20.0-rc.3", default-features = false }
fantoccini = { version = "0.20.0-rc.4", default-features = false }
futures = "0.3.21"
http = "0.2.8"
indexmap = "2.0.0"
Expand All @@ -40,7 +40,7 @@ serde_repr = "0.1.8"
stringmatch = "0.4.0"
thirtyfour-macros = { path = "../thirtyfour-macros", version = "0.1.1", optional = true }
thiserror = "1.0.31"
tokio = { version = "1.20", features = ["fs", "macros", "rt-multi-thread", "io-util", "sync"] }
tokio = { version = "1", features = ["fs", "macros", "rt-multi-thread", "io-util", "sync"] }
url = "2.2.2"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions thirtyfour/examples/components/playground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! cargo run --example wikipedia
use std::time::Duration;

use thirtyfour::{
components::{Component, ElementResolver},
prelude::*,
Expand Down
1 change: 0 additions & 1 deletion thirtyfour/examples/minimal_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! cargo run --example minimal_async
use thirtyfour::prelude::*;
use tokio;

#[tokio::main]
async fn main() -> WebDriverResult<()> {
Expand Down
1 change: 1 addition & 0 deletions thirtyfour/examples/query/custom_poller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use std::sync::Arc;
use std::time::Duration;

use thirtyfour::common::config::WebDriverConfig;
use thirtyfour::extensions::query::ElementPollerWithTimeout;
use thirtyfour::prelude::*;
Expand Down
1 change: 0 additions & 1 deletion thirtyfour/examples/remote_debugging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! cargo run --example remote_debugging
use thirtyfour::prelude::*;
use tokio;

#[tokio::main]
async fn main() -> Result<(), WebDriverError> {
Expand Down
1 change: 0 additions & 1 deletion thirtyfour/examples/selenium_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! cargo run --example selenium_example
use thirtyfour::prelude::*;
use tokio;

#[tokio::main]
async fn main() -> color_eyre::Result<()> {
Expand Down
1 change: 0 additions & 1 deletion thirtyfour/examples/tokio_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! cargo run --example tokio_async
use thirtyfour::prelude::*;
use tokio;

#[tokio::main]
async fn main() -> color_eyre::Result<()> {
Expand Down
1 change: 0 additions & 1 deletion thirtyfour/examples/tokio_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
//! cargo run --example tokio_async
use thirtyfour::prelude::*;
use tokio;

fn main() -> color_eyre::Result<()> {
let rt = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
Expand Down
31 changes: 31 additions & 0 deletions thirtyfour/src/action_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,37 @@ impl ActionChain {
Ok(())
}

/// Add a pause in the action sequence.
///
/// # Example:
/// ```no_run
/// use std::time::Duration;
/// # use thirtyfour::prelude::*;
/// # use thirtyfour::support::block_on;
/// #
/// # fn main() -> WebDriverResult<()> {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// let elem = driver.find(By::Id("button1")).await?;
/// driver
/// .action_chain()
/// .click_and_hold()
/// .pause(Duration::from_millis(200))
/// .release()
/// .perform()
/// .await?;
/// # driver.quit().await?;
/// # Ok(())
/// # })
/// # }
/// ```
pub fn pause(mut self, duration: Duration) -> Self {
self.key_actions = Some(self.key_actions.take().unwrap().pause(duration));
self.mouse_actions = Some(self.mouse_actions.take().unwrap().pause(duration));
self
}

/// Click and release the left mouse button.
///
/// # Example:
Expand Down
2 changes: 1 addition & 1 deletion thirtyfour/src/alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl SessionHandle {
/// # block_on(async {
/// # let caps = DesiredCapabilities::chrome();
/// # let driver = WebDriver::new("http://localhost:4444", caps).await?;
/// driver.send_alert_text(Key::Control + "a".to_string()).await?;
/// driver.send_alert_text(Key::Control + "a").await?;
/// driver.send_alert_text("thirtyfour").await?;
/// # driver.quit().await?;
/// # Ok(())
Expand Down
201 changes: 4 additions & 197 deletions thirtyfour/src/common/capabilities/chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use base64::{Engine, engine::general_purpose::STANDARD as BASE64};

use paste::paste;

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / stable

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / check

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / beta

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / firefox (stable)

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / chrome (stable)

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / macos-latest / firefox (stable)

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / test

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / macos-latest / chrome (stable)

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / chrome (beta)

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / ubuntu-latest / chrome (nightly)

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / windows-latest / chrome (stable)

unused import: `paste::paste`

Check warning on line 4 in thirtyfour/src/common/capabilities/chrome.rs

View workflow job for this annotation

GitHub Actions / windows-latest / firefox (stable)

unused import: `paste::paste`
use serde::Serialize;
use serde_json::{json, to_value, Value};
use serde_json::{json, Value};

use crate::error::WebDriverResult;
use crate::common::capabilities::chromium::ChromiumLikeCapabilities;
use crate::{BrowserCapabilitiesHelper, Capabilities, CapabilitiesHelper};

/// Capabilities for Chrome.
Expand All @@ -21,29 +21,6 @@ impl Default for ChromeCapabilities {
}
}

macro_rules! chrome_arg_wrapper {
($($fname:ident => $opt:literal),*) => {
paste! {
$(
#[doc = concat!("Set the ", $opt, " option.")]
pub fn [<set_ $fname>](&mut self) -> WebDriverResult<()> {
self.add_arg($opt)
}

#[doc = concat!("Unset the ", $opt, " option.")]
pub fn [<unset_ $fname>](&mut self) -> WebDriverResult<()> {
self.remove_arg($opt)
}

#[doc = concat!("Return true if the ", $opt, " option is set.")]
pub fn [<is_ $fname>](&mut self) -> bool {
self.has_arg($opt)
}
)*
}
}
}

impl ChromeCapabilities {
/// Create a new ChromeCapabilities struct.
pub fn new() -> Self {
Expand All @@ -53,178 +30,6 @@ impl ChromeCapabilities {
capabilities,
}
}

/// Get the current list of command-line arguments to `chromedriver` as a vec.
pub fn args(&self) -> Vec<String> {
self.browser_option("args").unwrap_or_default()
}

/// Get the current list of Chrome extensions as a vec.
///
/// Each item is a base64-encoded string containing the .CRX extension file contents.
/// Use `add_extension()` to add a new extension file.
pub fn extensions(&self) -> Vec<String> {
self.browser_option("extensions").unwrap_or_default()
}

/// Get the path to the chrome binary (if one was previously set).
pub fn binary(&self) -> Option<String> {
self.browser_option("binary")
}

/// Set the path to chrome binary to use.
pub fn set_binary(&mut self, path: &str) -> WebDriverResult<()> {
self.insert_browser_option("binary", path)
}

/// Unset the chrome binary path.
pub fn unset_binary(&mut self) {
self.remove_browser_option("binary");
}

/// Get the current debugger address (if one was previously set).
pub fn debugger_address(&self) -> Option<String> {
self.browser_option("debuggerAddress")
}

/// Set the debugger address.
pub fn set_debugger_address(&mut self, address: &str) -> WebDriverResult<()> {
self.insert_browser_option("debuggerAddress", address)
}

/// Unset the debugger address.
pub fn unset_debugger_address(&mut self) {
self.remove_browser_option("debuggerAddress");
}

/// Add the specified command-line argument to `chromedriver`.
///
/// ## Example
///
/// ```ignore
/// let mut caps = DesiredCapabilities::chrome();
/// caps.add_chrome_arg("--disable-local-storage")?;
/// ```
///
/// The full list of switches can be found here:
/// [https://chromium.googlesource.com/chromium/src/+/master/chrome/common/chrome_switches.cc](https://chromium.googlesource.com/chromium/src/+/master/chrome/common/chrome_switches.cc)
pub fn add_arg(&mut self, arg: &str) -> WebDriverResult<()> {
let mut args = self.args();
let arg_string = arg.to_string();
if !args.contains(&arg_string) {
args.push(arg_string);
}
self.insert_browser_option("args", to_value(args)?)
}

/// Remove the specified Chrome command-line argument if it had been added previously.
pub fn remove_arg(&mut self, arg: &str) -> WebDriverResult<()> {
let mut args = self.args();
if args.is_empty() {
Ok(())
} else {
args.retain(|v| v != arg);
self.insert_browser_option("args", to_value(args)?)
}
}

/// Return true if the specified arg is currently set.
pub fn has_arg(&self, arg: &str) -> bool {
self.args().contains(&arg.to_string())
}

/// Add the specified experimental option.
///
/// ## Example
/// ```no_run
/// use thirtyfour::DesiredCapabilities;
/// let mut caps = DesiredCapabilities::chrome();
/// caps.add_experimental_option("excludeSwitches", vec!["--enable-logging"]).unwrap();
/// ```
pub fn add_experimental_option(
&mut self,
name: impl Into<String>,
value: impl Serialize,
) -> WebDriverResult<()> {
self.insert_browser_option(name, value)
}

/// Remove the specified experimental option.
pub fn remove_experimental_option(&mut self, name: &str) {
self.remove_browser_option(name);
}

/// Add a base64-encoded extension.
pub fn add_encoded_extension(&mut self, extension_base64: &str) -> WebDriverResult<()> {
let mut extensions = self.extensions();
let ext_string = extension_base64.to_string();
if !extensions.contains(&ext_string) {
extensions.push(ext_string);
}
self.insert_browser_option("extensions", to_value(extensions)?)
}

/// Remove the specified base64-encoded extension if it had been added previously.
pub fn remove_encoded_extension(&mut self, extension_base64: &str) -> WebDriverResult<()> {
let mut extensions = self.extensions();
if extensions.is_empty() {
Ok(())
} else {
extensions.retain(|v| v != extension_base64);
self.insert_browser_option("extensions", to_value(extensions)?)
}
}

/// Add Chrome extension file. This will be a file with a .CRX extension.
pub fn add_extension(&mut self, crx_file: &Path) -> WebDriverResult<()> {
let contents = std::fs::read(crx_file)?;
let b64_contents = BASE64.encode(contents);
self.add_encoded_extension(&b64_contents)
}

/// Remove the specified Chrome extension file if an identical extension had been added
/// previously.
pub fn remove_extension(&mut self, crx_file: &Path) -> WebDriverResult<()> {
let contents = std::fs::read(crx_file)?;
let b64_contents = BASE64.encode(contents);
self.remove_encoded_extension(&b64_contents)
}

/// Get the list of exclude switches.
pub fn exclude_switches(&self) -> Vec<String> {
self.browser_option("excludeSwitches").unwrap_or_default()
}

/// Add the specified arg to the list of exclude switches.
pub fn add_exclude_switch(&mut self, arg: &str) -> WebDriverResult<()> {
let mut args = self.exclude_switches();
let arg_string = arg.to_string();
if !args.contains(&arg_string) {
args.push(arg_string);
}
self.add_experimental_option("excludeSwitches", to_value(args)?)
}

/// Remove the specified arg from the list of exclude switches.
pub fn remove_exclude_switch(&mut self, arg: &str) -> WebDriverResult<()> {
let mut args = self.exclude_switches();
if args.is_empty() {
Ok(())
} else {
args.retain(|v| v != arg);
self.add_experimental_option("excludeSwitches", to_value(args)?)
}
}

chrome_arg_wrapper! {
headless => "--headless",
disable_web_security => "--disable-web-security",
ignore_certificate_errors => "--ignore-certificate-errors",
no_sandbox => "--no-sandbox",
disable_gpu => "--disable-gpu",
disable_dev_shm_usage => "--disable-dev-shm-usage",
disable_local_storage => "--disable-local-storage"
}
}

impl CapabilitiesHelper for ChromeCapabilities {
Expand All @@ -245,6 +50,8 @@ impl BrowserCapabilitiesHelper for ChromeCapabilities {
const KEY: &'static str = "goog:chromeOptions";
}

impl ChromiumLikeCapabilities for ChromeCapabilities {}

impl From<ChromeCapabilities> for Capabilities {
fn from(caps: ChromeCapabilities) -> Capabilities {
caps.capabilities
Expand Down
Loading

0 comments on commit aa118c2

Please sign in to comment.