| Crates.io | web-sys-ec |
| lib.rs | web-sys-ec |
| version | 0.1.0 |
| created_at | 2025-05-08 13:06:32.611024+00 |
| updated_at | 2025-05-08 21:24:21.517661+00 |
| description | Expected conditions in Selenium-like style for WASM targets using web-sys. |
| homepage | |
| repository | https://github.com/mondeja/web-sys-ec |
| max_upload_size | |
| id | 1665319 |
| size | 64,423 |
Expected conditions in Selenium-like style for WASM targets using web-sys.
When you test your apps with wasm-pack test, you may want to use the
web-sys crate to interact with the DOM. This is a great way to test your
app in a real browser environment.
However, actions in a browser are asynchronous, and you may want to wait for
certain conditions to be met while testing. This is where web-sys-ec comes
in.
Provides an interface to interact with the DOM waiting for certain conditions to be met. It is inspired by the Selenium syntax, where you can wait for certain expected conditions to be met before proceeding with your tests.
I've created this library to help me with my own testing needs and it's far away from being complete. Feel free to open pull requests or issues if you find any bugs or have suggestions for improvements and I'll be happy to review them.
Add web-sys-ec to your Cargo.toml:
[dependencies]
web-sys-ec = "0.1"
Wait 10 seconds for a <p> HTML element to contain the text "Select a language:":
use web_sys_ec::{By, Ec, Wait};
Wait(10)
.until((
By::TagName("p"),
Ec::InnerTextContains("Select a language:"),
))
.await;
Wait 1 second for the <html> HTML element to have the lang attribute set to
"es":
use web_sys_ec::{By, Ec, Wait};
Wait(1)
.until((
By::TagName("html"),
Ec::AttributeValueIs("lang", "es"),
))
.await;
Wait 1 second for the local storage to have the language key set to "es":
use web_sys_ec::{Ec, Wait};
Wait(1).until(Ec::LocalStorageAttributeValueIs("language","es")).await;
Wait 200 milliseconds for a <p id="foo"> HTML element to exist in the DOM:
use web_sys_ec::{By, Wait};
Wait(0.2).until("p#foo").await;
If a condition is not met, it will panic with a message like:
Expected condition has not been met in the given time:
- Caller: tests/end2end/tests/csr_complete.rs:54:10
- Selector: HTML element with tag name 'html' (`By::TagName("html")`)
- Condition: HTML element attribute 'lang' value is equal to 'es' (`Ec::AttributeValueIs("lang", "es")`)
- Duration: 1s
- Poll frecuency: 20ms
- Number of attempts: 51
Note that the Caller: ... line will only be shown if you're using the nightly
toolchain and the nightly feature is enabled.
nightly: Enables nightly toolchain support, which is currently needed to
provide caller tracking.