From f887c2612c5ce58aa3637a2ada0f4efd0b494906 Mon Sep 17 00:00:00 2001 From: j-mendez Date: Sun, 18 Feb 2024 19:49:46 -0500 Subject: [PATCH] chore(cli): add playwright script --- README.md | 4 ++- kayle_cli/Cargo.toml | 2 +- kayle_cli/README.md | 33 ++++++++++++++---- kayle_cli/src/main.rs | 54 +++++++++++++++++++++++++----- kayle_cli/src/playwright_script.rs | 32 ++++++++++++++++++ kayle_cli/src/puppeteer_script.rs | 2 +- 6 files changed, 109 insertions(+), 18 deletions(-) create mode 100644 kayle_cli/src/playwright_script.rs diff --git a/README.md b/README.md index 3c0e46c..fa07d33 100644 --- a/README.md +++ b/README.md @@ -296,12 +296,14 @@ The [kayle CLI](./kayle_cli/) is a work in progress. ```sh npm install kayle_cli +# install the deps - you can swap puppeteer with playwright +kayle_cli --automation-lib puppeteer install ``` Audit a website url. ```sh -kayle_cli https://www.somewebsite.com +kayle_cli --automation-lib puppeteer https://www.somewebsite.com ``` ## Testing diff --git a/kayle_cli/Cargo.toml b/kayle_cli/Cargo.toml index a666ffc..4476946 100644 --- a/kayle_cli/Cargo.toml +++ b/kayle_cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kayle_cli" -version = "0.0.1" +version = "0.0.2" edition = "2021" description = "The kayle CLI for web accessibility auditing" repository = "https://github.com/a11ywatch/kayle" diff --git a/kayle_cli/README.md b/kayle_cli/README.md index 79ea955..3ea0426 100644 --- a/kayle_cli/README.md +++ b/kayle_cli/README.md @@ -5,7 +5,6 @@ The kayle CLI, for web accessibility audits. [WIP] ## Requirements Node.js is required. -Install puppeteer globally `npm i puppeteer -g`. ## Installation @@ -19,6 +18,12 @@ npm install kayle_cli ## Getting Started +Make sure you have an automation lib installed. Use the arg `--automation-lib` to switch from the default puppeteer to playwright. + +```sh +kayle_cli --automation-lib puppeteer install +``` + Pass in a list of urls to get the results. ```sh @@ -28,20 +33,37 @@ Usage: kayle_cli [OPTIONS] Commands: upgrade Upgrade kayle and the dependencies required + install Install kayle and the dependencies required configure Configure the audits help Print this message or the help of the given subcommand(s) Options: -s, --standard The accessibility standard to run, WCAG2A, WCAG2AA, WCAG2AAA, and Section508 + --include-warnings - Include warnings in the audit [possible values: true, false] + Include warnings in the audit + + [possible values: true, false] + --include-notices - Include notices in the audit [possible values: true, false] + Include notices in the audit + + [possible values: true, false] + -r, --runners The accessibility runner to use htmlcs, axecore, or kayle + + --automation-lib + The automation lib to use either puppeteer or playwright + + Possible values: + - puppeteer: The puppeteer library. Defaults to this + - playwright: The playwright library by microsoft + -h, --help - Print help + Print help (see a summary with '-h') + -V, --version Print version ``` @@ -53,5 +75,4 @@ kayle_cli https://www.drake.com ## Todo -1. Configuration. -2. Allow Opt between puppeteer or playwright. \ No newline at end of file +1. Configuration. \ No newline at end of file diff --git a/kayle_cli/src/main.rs b/kayle_cli/src/main.rs index eca3fbb..18981c3 100644 --- a/kayle_cli/src/main.rs +++ b/kayle_cli/src/main.rs @@ -1,13 +1,14 @@ -use clap::{Parser, Subcommand}; -use std::ffi::OsString; -use std::process::Command; +use clap::{Parser, Subcommand, ValueEnum}; +use std::{ffi::OsString, process::Command}; +mod playwright_script; mod puppeteer_script; #[derive(Debug, Subcommand)] enum Commands { /// Upgrade kayle and the dependencies required. - #[command(arg_required_else_help = true)] Upgrade, + /// Install kayle and the dependencies required. + Install, /// Configure the audits. Configure, /// The kayle program. @@ -15,6 +16,16 @@ enum Commands { Kayle(Vec), } +/// Automation libraries to use. +#[derive(Debug, Default, Clone, PartialEq, ValueEnum)] +enum AutomationLib { + #[default] + /// The puppeteer library. Defaults to this. + Puppeteer, + /// The playwright library by microsoft + Playwright, +} + /// Web Accessibility Auditing using the kayle engine #[derive(Parser, Debug)] #[command(version, about, long_about = None)] @@ -31,45 +42,70 @@ struct Args { /// The accessibility runner to use htmlcs, axecore, or kayle. #[arg(short, long)] runners: Option>, + #[arg(long, value_enum)] + /// The automation lib to use either puppeteer or playwright. + automation_lib: Option, #[command(subcommand)] command: Commands, } fn main() { let args = Args::parse(); + let puppeteer = args.automation_lib.unwrap_or_default() == AutomationLib::Puppeteer; match args.command { Commands::Configure => { println!("Configuration TODO"); } - Commands::Upgrade => { + Commands::Upgrade | Commands::Install => { + println!( + "Installing kayle and {}...", + if puppeteer { "puppeteer" } else { "playwright" } + ); Command::new("npm") .args(["i", "kayle", "-g"]) .status() .expect("Failed to execute command - npm install kayle command"); Command::new("npm") - .args(["i", "puppeteer", "-g"]) + .args([ + "i", + if puppeteer { "puppeteer" } else { "playwright" }, + "-g", + ]) .status() - .expect("Failed to execute command - npm install puppeteer command"); + .expect(if puppeteer { + "Failed to execute command - npm install puppeteer command" + } else { + "Failed to execute command - npm install playwright command" + }); } Commands::Kayle(urls) => { let accessibility_standard = args.standard.unwrap_or_default(); let include_warnings = args.include_warnings.unwrap_or_default(); let include_notices = args.include_notices.unwrap_or_default(); let runners = args.runners.unwrap_or_default().join(","); + let headless_script = if puppeteer { + puppeteer_script::SCRIPT_EXECUTION + } else { + playwright_script::SCRIPT_EXECUTION + }; for u in urls.iter() { Command::new("node") .args([ "-e", - puppeteer_script::SCRIPT_EXECUTION, + headless_script, u.to_str().unwrap(), &accessibility_standard, if include_notices { "true"} else { "false" }, if include_warnings { "true"} else { "false" }, &runners]) .status() - .expect("Failed to execute node command. Make sure puppeteer and node is installed."); + .expect(if puppeteer { + "Failed to execute node command - make sure node and puppeteer is installed." + } else { + "Failed to execute node command - make sure node and playwright is installed." + }); } } } diff --git a/kayle_cli/src/playwright_script.rs b/kayle_cli/src/playwright_script.rs new file mode 100644 index 0000000..8090865 --- /dev/null +++ b/kayle_cli/src/playwright_script.rs @@ -0,0 +1,32 @@ +/// kayle script using puppeteer to drive the browser +pub const SCRIPT_EXECUTION: &'static str = r###" +const { argv, stdout } = require('node:process'); +const { chromium } = require('playwright'); +const { kayle, Standard } = require('kayle'); + +const args = argv.slice(1); + +(async () => { + const browser = await chromium.launch(); + const page = await browser.newPage(); + + if (process.env.LOG_ENABLED) { + page.on("console", (msg) => console.log(msg.text())); + } + + const runners = args[4].split(","); + + const audit = await kayle({ + page, + browser, + runners: runners.length && runners[0] ? runners : ["htmlcs"], + includeWarnings: args[3] === "true", + includeNotices: args[2] === "true", + standard: args[1] || Standard.WCAG2AA, + origin: args[0], + }); + + stdout.write(JSON.stringify(audit)); + process.exit(); +})(); +"###; diff --git a/kayle_cli/src/puppeteer_script.rs b/kayle_cli/src/puppeteer_script.rs index 280a21f..872d105 100644 --- a/kayle_cli/src/puppeteer_script.rs +++ b/kayle_cli/src/puppeteer_script.rs @@ -1,4 +1,4 @@ -/// kayle script using puppeteer to drive the browser +/// kayle script using puppeteer to drive the browser pub const SCRIPT_EXECUTION: &'static str = r###" const { argv, stdout } = require('node:process'); const puppeteer = require('puppeteer');