Skip to content

Commit

Permalink
chore(cli): add playwright script
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Feb 19, 2024
1 parent ad14755 commit f887c26
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 18 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion kayle_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
33 changes: 27 additions & 6 deletions kayle_cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -28,20 +33,37 @@ Usage: kayle_cli [OPTIONS] <COMMAND>

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 <STANDARD>
The accessibility standard to run, WCAG2A, WCAG2AA, WCAG2AAA, and Section508

--include-warnings <INCLUDE_WARNINGS>
Include warnings in the audit [possible values: true, false]
Include warnings in the audit

[possible values: true, false]

--include-notices <INCLUDE_NOTICES>
Include notices in the audit [possible values: true, false]
Include notices in the audit

[possible values: true, false]

-r, --runners <RUNNERS>
The accessibility runner to use htmlcs, axecore, or kayle

--automation-lib <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
```
Expand All @@ -53,5 +75,4 @@ kayle_cli https://www.drake.com

## Todo

1. Configuration.
2. Allow Opt between puppeteer or playwright.
1. Configuration.
54 changes: 45 additions & 9 deletions kayle_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
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.
#[command(external_subcommand)]
Kayle(Vec<OsString>),
}

/// 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)]
Expand All @@ -31,45 +42,70 @@ struct Args {
/// The accessibility runner to use htmlcs, axecore, or kayle.
#[arg(short, long)]
runners: Option<Vec<String>>,
#[arg(long, value_enum)]
/// The automation lib to use either puppeteer or playwright.
automation_lib: Option<AutomationLib>,
#[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."
});
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions kayle_cli/src/playwright_script.rs
Original file line number Diff line number Diff line change
@@ -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();
})();
"###;
2 changes: 1 addition & 1 deletion kayle_cli/src/puppeteer_script.rs
Original file line number Diff line number Diff line change
@@ -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');
Expand Down

0 comments on commit f887c26

Please sign in to comment.