1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
//! Thirtyfour_query provides an advanced query interface for `thirtyfour`, featuring //! powerful filtering and polling options. //! //! See examples for more details. //! //! ## Usage //! //! ### ElementQuery //! //! First, import the following: //! ```ignore //! use thirtyfour_query::{ElementPoller, ElementQueryable}; //! ``` //! //! Next, set the default polling behaviour: //! ```rust //! # use thirtyfour::prelude::*; //! # use thirtyfour::support::block_on; //! # use thirtyfour_query::{ElementPoller, ElementQueryable}; //! # use std::time::Duration; //! # //! # fn main() -> WebDriverResult<()> { //! # block_on(async { //! # let caps = DesiredCapabilities::chrome(); //! # let mut driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; //! // Disable implicit timeout in order to use new query interface. //! driver.set_implicit_wait_timeout(Duration::new(0, 0)).await?; //! //! let poller = ElementPoller::TimeoutWithInterval(Duration::new(10, 0), Duration::from_millis(500)); //! driver.config_mut().set("ElementPoller", poller)?; //! # Ok(()) //! # }) //! # } //! ``` //! //! Other ElementPoller options are also available, such as NoWait and NumTriesWithInterval. //! These can be overridden on a per-query basis if needed. //! //! Now, using the query interface you can do things like: //! //! ```rust //! # use thirtyfour::prelude::*; //! # use thirtyfour::support::block_on; //! # use thirtyfour_query::{ElementPoller, ElementQueryable}; //! # use std::time::Duration; //! # //! # fn main() -> WebDriverResult<()> { //! # block_on(async { //! # let caps = DesiredCapabilities::chrome(); //! # let mut driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; //! # driver.set_implicit_wait_timeout(Duration::new(0, 0)).await?; //! # //! # let poller = ElementPoller::TimeoutWithInterval(Duration::new(10, 0), Duration::from_millis(500)); //! # driver.config_mut().set("ElementPoller", poller)?; //! # driver.get("http://webappdemo").await?; //! // This won't wait. //! let elem_found = driver.query(By::Id("button1")).exists().await?; //! # assert_eq!(elem_found, true); //! //! // This will wait, using the values from ElementPoller above. //! let elem = driver.query(By::Css("thiswont.match")).or(By::Id("button1")).first().await?; //! # assert_eq!(elem.tag_name().await?, "button"); //! # Ok(()) //! # }) //! # } //! ``` //! //! This will execute both queries once per poll iteration and return the first one that matches. //! //! You can also filter on one or both match arms like this: //! //! ```rust //! # use thirtyfour::prelude::*; //! # use thirtyfour::support::block_on; //! # use thirtyfour_query::{ElementPoller, ElementQueryable, StringMatch}; //! # use std::time::Duration; //! # //! # fn main() -> WebDriverResult<()> { //! # block_on(async { //! # let caps = DesiredCapabilities::chrome(); //! # let mut driver = WebDriver::new("http://localhost:4444/wd/hub", &caps).await?; //! # driver.set_implicit_wait_timeout(Duration::new(0, 0)).await?; //! # //! # let poller = ElementPoller::TimeoutWithInterval(Duration::new(10, 0), Duration::from_millis(500)); //! # driver.config_mut().set("ElementPoller", poller)?; //! # driver.get("http://webappdemo").await?; //! let elem = driver.query(By::Css("thiswont.match")).with_text("testing") //! .or(By::Id("button1")).with_class(StringMatch::new("pure-button").word()).and_enabled() //! .first().await?; //! # assert_eq!(elem.tag_name().await?, "button"); //! # Ok(()) //! # }) //! # } //! ``` //! //! Note the use of `StringMatch` to provide a partial (whole-word) match on the class name. //! See the documentation for [StringMatch](https://crates.io/crates/stringmatch) for more info. //! //! To fetch all matching elements instead of just the first one, simply change `first()` to `all()` //! and you'll get a Vec instead. Also see `all_required()` if you want it to return an error //! when there are no matching elements. //! //! All timeout, interval and ElementPoller details can be overridden on a per-call basis if //! desired. See the `ElementQuery` documentation for more details. //! //! ### ElementWaiter //! //! First, import the following: //! ```ignore //! use thirtyfour_query::{ElementPoller, ElementWaitable}; //! ``` //! //! Next, set the default polling behaviour (same as for ElementQuery - the same polling //! settings are used for both): //! ```ignore //! // Disable implicit timeout in order to use new query interface. //! driver.set_implicit_wait_timeout(Duration::new(0, 0)).await?; //! //! let poller = ElementPoller::TimeoutWithInterval(Duration::new(20, 0), Duration::from_millis(500)); //! driver.config_mut().set("ElementPoller", poller)?; //! ``` //! //! Now you can do things like this: //! ```ignore //! elem.wait_until().displayed().await?; //! // You can optionally provide a nicer error message like this. //! elem.wait_until().error("Timed out waiting for element to disappear").not_displayed().await?; //! //! elem.wait_until().enabled().await?; //! elem.wait_until().clickable().await?; //! ``` //! //! And so on. See the `ElementWaiter` docs for the full list of predicates available. //! //! ElementWaiter also allows the user of custom predicates that take a `&WebElement` argument //! and return a `WebDriverResult<bool>`. //! //! A range of pre-defined predicates are also supplied for convenience in the //! `thirtyfour_query::conditions` module. //! //! ```ignore //! use thirtyfour_query::conditions; //! //! elem.wait_until().conditions(vec![ //! conditions::element_is_displayed(true), //! conditions::element_is_clickable(true) //! ]).await?; //! ``` //! Take a look at the `conditions` module for the full list of predicates available. //! NOTE: Predicates require you to specify whether or not errors should be ignored. //! //! These predicates (or your own) can also be supplied as filters to `ElementQuery`. //! pub mod conditions; mod poller; mod query; mod waiter; pub use poller::*; pub use query::*; pub use waiter::*; use futures::Future; use std::pin::Pin; /// This is a re-export of stringmatch::StringMatch. pub use stringmatch::StringMatch; use thirtyfour::prelude::WebDriverResult; use thirtyfour::WebElement; /// Function signature for element predicates. pub type ElementPredicate = Box< dyn for<'a> Fn( &'a WebElement<'a>, ) -> Pin<Box<dyn Future<Output = WebDriverResult<bool>> + Send + 'a>> + Send + Sync + 'static, >;