web-sys-ec 0.0.1

Expected conditions in Selenium-like style for WASM targets using web-sys.
Documentation

web-sys-ec

Expected conditions in Selenium-like style for WASM targets using web-sys.

Rationale

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, a lot of actions in web-sys are asynchronous, and you may want to wait for certain conditions to be met before proceeding with your tests. This is where the web-sys-ec crate comes in.

It provides a library to interact with the DOM and wait 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.

Disclaimer

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.

Installation

Add web-sys-ec to your Cargo.toml:

[dependencies]
web-sys-ec = "0.1"

Usage

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".into()),
        Ec::InnerTextContains("Select a language:".into()),
    ))
    .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".into()),
        Ec::AttributeValueIs("lang".into(), "es".into()),
    ))
    .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".into(),
        "es".into(),
    ))
    .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.

Features

  • nightly: Enables nightly toolchain support, which is currently needed to provides caller tracking.