~vikanezrimaya/kittybox

ffd8a0b6872b2718414fc3457d50dd0d622b2a9a — Vika 2 years ago d47c536 fantoccini
WIP: fantoccini end-to-end tests
4 files changed, 214 insertions(+), 5 deletions(-)

M kittybox-rs/Cargo.lock
M kittybox-rs/Cargo.toml
A kittybox-rs/tests/lib.rs
M shell.nix
M kittybox-rs/Cargo.lock => kittybox-rs/Cargo.lock +146 -4
@@ 451,7 451,7 @@ dependencies = [
 "num-integer",
 "num-traits",
 "serde",
 "time",
 "time 0.1.44",
 "winapi",
]



@@ 505,6 505,33 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"

[[package]]
name = "cookie"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94d4706de1b0fa5b132270cddffa8585166037822e260a944fe161acd137ca05"
dependencies = [
 "percent-encoding",
 "time 0.3.11",
 "version_check",
]

[[package]]
name = "core-foundation"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146"
dependencies = [
 "core-foundation-sys",
 "libc",
]

[[package]]
name = "core-foundation-sys"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"

[[package]]
name = "cpufeatures"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 770,6 797,28 @@ dependencies = [
]

[[package]]
name = "fantoccini"
version = "0.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06a9d234724d23f70281ece51b20726eb0cf5fb4b156e5b9adfd65de6251a43b"
dependencies = [
 "base64",
 "cookie",
 "futures-core",
 "futures-util",
 "http",
 "hyper",
 "hyper-rustls",
 "mime",
 "serde",
 "serde_json",
 "time 0.3.11",
 "tokio",
 "url",
 "webdriver",
]

[[package]]
name = "fastrand"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 1178,7 1227,9 @@ checksum = "d87c48c02e0dc5e3b849a2041db3029fd066650f8f717c07bf8ed78ccb895cac"
dependencies = [
 "http",
 "hyper",
 "log",
 "rustls",
 "rustls-native-certs",
 "tokio",
 "tokio-rustls",
]


@@ 1289,6 1340,7 @@ dependencies = [
 "easy-scraper",
 "either",
 "faker_rand",
 "fantoccini",
 "futures",
 "futures-util",
 "hex",


@@ 1714,6 1766,15 @@ dependencies = [
]

[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
 "libc",
]

[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 1733,9 1794,9 @@ checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"

[[package]]
name = "openssl-sys"
version = "0.9.73"
version = "0.9.75"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5fd19fb3e0a8191c1e34935718976a3e70c112ab9a24af6d7cadccd9d90bc0"
checksum = "e5f9bd0c2710541a3cda73d6f9ac4f1b240de4ae261065d309dbe73d9dceb42f"
dependencies = [
 "autocfg 1.1.0",
 "cc",


@@ 2394,7 2455,7 @@ dependencies = [
 "percent-encoding",
 "pin-project-lite",
 "rustls",
 "rustls-pemfile",
 "rustls-pemfile 0.3.0",
 "serde",
 "serde_json",
 "serde_urlencoded",


@@ 2446,6 2507,18 @@ dependencies = [
]

[[package]]
name = "rustls-native-certs"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0167bac7a9f490495f3c33013e7722b53cb087ecbe082fb0c6387c96f634ea50"
dependencies = [
 "openssl-probe",
 "rustls-pemfile 1.0.0",
 "schannel",
 "security-framework",
]

[[package]]
name = "rustls-pemfile"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 2455,6 2528,15 @@ dependencies = [
]

[[package]]
name = "rustls-pemfile"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7522c9de787ff061458fe9a829dc790a3f5b22dc571694fc5883f448b94d9a9"
dependencies = [
 "base64",
]

[[package]]
name = "rustversion"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 2493,6 2575,29 @@ dependencies = [
]

[[package]]
name = "security-framework"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dc14f172faf8a0194a3aded622712b0de276821addc574fa54fc0a1167e10dc"
dependencies = [
 "bitflags",
 "core-foundation",
 "core-foundation-sys",
 "libc",
 "security-framework-sys",
]

[[package]]
name = "security-framework-sys"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556"
dependencies = [
 "core-foundation-sys",
 "libc",
]

[[package]]
name = "selectors"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 2893,6 2998,24 @@ dependencies = [
]

[[package]]
name = "time"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72c91f41dcb2f096c05f0873d667dceec1087ce5bcf984ec8ffb19acddbb3217"
dependencies = [
 "itoa 1.0.1",
 "libc",
 "num_threads",
 "time-macros",
]

[[package]]
name = "time-macros"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792"

[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"


@@ 3383,6 3506,25 @@ dependencies = [
]

[[package]]
name = "webdriver"
version = "0.46.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9973cb72c8587d5ad5efdb91e663d36177dc37725e6c90ca86c626b0cc45c93f"
dependencies = [
 "base64",
 "bytes",
 "cookie",
 "http",
 "log",
 "serde",
 "serde_derive",
 "serde_json",
 "time 0.3.11",
 "unicode-segmentation",
 "url",
]

[[package]]
name = "webpki"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"

M kittybox-rs/Cargo.toml => kittybox-rs/Cargo.toml +4 -0
@@ 49,6 49,10 @@ httpmock = "^0.6"            # HTTP mocking library that allows you to simulate 
faker_rand = "^0.1.1"        # Seedable, rand-compatible generators of fake data
rand = "^0.8.5"              # Utilities for random number generation
tracing-test = "^0.2.2"
[dev-dependencies.fantoccini]
version = "^0.19.1"
features = ["rustls-tls"]
default-features = false

[dependencies]
async-trait = "^0.1.50"      # Type erasure for async trait methods

A kittybox-rs/tests/lib.rs => kittybox-rs/tests/lib.rs +61 -0
@@ 0,0 1,61 @@
use fantoccini::{ClientBuilder, Client, Locator};

struct WebDriverServer(std::process::Child);
impl WebDriverServer {
    fn new() -> std::io::Result<Self> {
        Ok(WebDriverServer(
            std::process::Command::new("geckodriver")
                .arg("--port")
                .arg("4444")
                .spawn()?
        ))
    }
}

impl Drop for WebDriverServer {
    fn drop(&mut self) {
        self.0.kill();
        self.0.try_wait().expect("Failed to wait for fallen child!");
    }
}

#[tokio::test]
async fn test_onboarding() -> Result<(), fantoccini::error::CmdError> {
    // TODO launch geckodriver ourselves so we don't have to provision it
    let webdriver = WebDriverServer::new();
    let c = {
        let mut c = ClientBuilder::rustls();
        if std::env::var("DISPLAY").ok().is_none() && std::env::var("WAYLAND_DISPLAY").ok().is_none() {
            c.capabilities(match serde_json::json!({
                "moz:firefoxOptions": {
                    "args": ["-headless"]
                }
            }) {
                serde_json::Value::Object(map) => map,
                _ => unreachable!()
            });
        }
        c.connect("http://localhost:4444")
            .await
            .expect("failed to connect to WebDriver")
    };

    // first, go to the Wikipedia page for Foobar
    c.goto("http://localhost:8080/").await?;
    let url = c.current_url().await?;
    assert_eq!(url.as_ref(), "http://localhost:8080/.kittybox/onboarding");

    todo!();

    /*
    // click "Foo (disambiguation)"
    c.find(Locator::Css(".mw-disambig")).await?.click().await?;

    // click "Foo Lake"
    c.find(Locator::LinkText("Foo Lake")).await?.click().await?;

    let url = c.current_url().await?;
    assert_eq!(url.as_ref(), "https://en.wikipedia.org/wiki/Foo_Lake");
    */
    c.close().await?;
}

M shell.nix => shell.nix +3 -1
@@ 2,6 2,7 @@
, cargo-watch, cargo-edit, cargo-outdated
, xh, systemfd, tokio-console
, pkg-config, protobuf, openssl, zlib
, geckodriver, firefox
}:
mkShell {
  name = "rust-dev-shell";


@@ 14,6 15,7 @@ mkShell {
    pkg-config protobuf
    # required for httpmock, not actually used in the release
    openssl.dev zlib

    # Required for end-to-end testing with WebDriver
    geckodriver firefox
  ];
}