Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/anti-work/shortest
QA via natural language AI tests
https://github.com/anti-work/shortest
anthropic automation chromium e2e-testing e2e-tests end-to-end-testing javascript nextjs playwright test-automation testing testing-framework testing-tool
Last synced: about 5 hours ago
JSON representation
QA via natural language AI tests
- Host: GitHub
- URL: https://github.com/anti-work/shortest
- Owner: anti-work
- License: mit
- Created: 2024-09-18T20:44:05.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2025-01-28T06:16:18.000Z (8 days ago)
- Last Synced: 2025-01-29T01:05:23.053Z (7 days ago)
- Topics: anthropic, automation, chromium, e2e-testing, e2e-tests, end-to-end-testing, javascript, nextjs, playwright, test-automation, testing, testing-framework, testing-tool
- Language: TypeScript
- Homepage: https://shortest.com
- Size: 1.17 MB
- Stars: 4,162
- Watchers: 26
- Forks: 221
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: license.md
Awesome Lists containing this project
- awesome-AI-driven-development - shortest - QA via natural language AI tests (Uncategorized / Uncategorized)
- AiTreasureBox - anti-work/shortest - 02-01_4204_2](https://img.shields.io/github/stars/anti-work/shortest.svg)|QA via natural language AI tests| (Repos)
- StarryDivineSky - anti-work/shortest
README
# Shortest
AI-powered natural language end-to-end testing framework.
Your browser does not support the video tag.
## Features
- Natural language E2E testing framework
- AI-powered test execution using Anthropic Claude API
- Built on Playwright
- GitHub integration with 2FA support
- Email validation with Mailosaur## Using Shortest in your project
If helpful, [here's a short video](https://github.com/anti-work/shortest/issues/143#issuecomment-2564488173)!
### Installation
Use the `shortest init` command to streamline the setup process in a new or existing project.
The `shortest init` command will:
```sh
npx @antiwork/shortest init
```This will:
- Automatically install the `@antiwork/shortest` package as a dev dependency if it is not already installed
- Create a default `shortest.config.ts` file with boilerplate configuration
- Generate a `.env.local` file (unless present) with placeholders for required environment variables, such as `ANTHROPIC_API_KEY`
- Add `.env.local` and `.shortest/` to `.gitignore`### Quick start
1. Determine your test entry and add your Anthropic API key in config file: `shortest.config.ts`
```typescript
import type { ShortestConfig } from "@antiwork/shortest";export default {
headless: false,
baseUrl: "http://localhost:3000",
testPattern: "**/*.test.ts",
anthropicKey: process.env.ANTHROPIC_API_KEY,
} satisfies ShortestConfig;
```2. Create test files using the pattern specified in the config: `app/login.test.ts`
```typescript
import { shortest } from "@antiwork/shortest";shortest("Login to the app using email and password", {
username: process.env.GITHUB_USERNAME,
password: process.env.GITHUB_PASSWORD,
});
```### Using callback functions
You can also use callback functions to add additional assertions and other logic. AI will execute the callback function after the test
execution in browser is completed.```typescript
import { shortest } from "@antiwork/shortest";
import { db } from "@/lib/db/drizzle";
import { users } from "@/lib/db/schema";
import { eq } from "drizzle-orm";shortest("Login to the app using username and password", {
username: process.env.USERNAME,
password: process.env.PASSWORD,
}).after(async ({ page }) => {
// Get current user's clerk ID from the page
const clerkId = await page.evaluate(() => {
return window.localStorage.getItem("clerk-user");
});if (!clerkId) {
throw new Error("User not found in database");
}// Query the database
const [user] = await db
.select()
.from(users)
.where(eq(users.clerkId, clerkId))
.limit(1);expect(user).toBeDefined();
});
```### Lifecycle hooks
You can use lifecycle hooks to run code before and after the test.
```typescript
import { shortest } from "@antiwork/shortest";shortest.beforeAll(async ({ page }) => {
await clerkSetup({
frontendApiUrl:
process.env.PLAYWRIGHT_TEST_BASE_URL ?? "http://localhost:3000",
});
});shortest.beforeEach(async ({ page }) => {
await clerk.signIn({
page,
signInParams: {
strategy: "email_code",
identifier: "[email protected]",
},
});
});shortest.afterEach(async ({ page }) => {
await page.close();
});shortest.afterAll(async ({ page }) => {
await clerk.signOut({ page });
});
```### Chaining tests
Shortest supports flexible test chaining patterns:
```typescript
// Sequential test chain
shortest([
"user can login with email and password",
"user can modify their account-level refund policy",
]);// Reusable test flows
const loginAsLawyer = "login as lawyer with valid credentials";
const loginAsContractor = "login as contractor with valid credentials";
const allAppActions = ["send invoice to company", "view invoices"];// Combine flows with spread operator
shortest([loginAsLawyer, ...allAppActions]);
shortest([loginAsContractor, ...allAppActions]);
```### API testing
Test API endpoints using natural language
```typescript
const req = new APIRequest({
baseURL: API_BASE_URI,
});shortest(
"Ensure the response contains only active users",
req.fetch({
url: "/users",
method: "GET",
params: new URLSearchParams({
active: true,
}),
}),
);
```Or simply:
```typescript
shortest(`
Test the API GET endpoint ${API_BASE_URI}/users with query parameter { "active": true }
Expect the response to contain only active users
`);
```### Running tests
```bash
pnpm shortest # Run all tests
pnpm shortest __tests__/login.test.ts # Run specific test
pnpm shortest --headless # Run in headless mode using CLI
```You can find example tests in the [`examples`](./examples) directory.
### CI setup
You can run Shortest in your CI/CD pipeline by running tests in headless mode. Make sure to add your Anthropic API key to your CI/CD pipeline secrets.
[See example here](https://github.com/anti-work/shortest/blob/main/.github/workflows/shortest.yml)
### GitHub 2FA login setup
Shortest supports login using GitHub 2FA. For GitHub authentication tests:
1. Go to your repository settings
2. Navigate to "Password and Authentication"
3. Click on "Authenticator App"
4. Select "Use your authenticator app"
5. Click "Setup key" to obtain the OTP secret
6. Add the OTP secret to your `.env.local` file or use the Shortest CLI to add it
7. Enter the 2FA code displayed in your terminal into Github's Authenticator setup page to complete the process```bash
shortest --github-code --secret=
```### Environment setup
Required in `.env.local`:
```bash
ANTHROPIC_API_KEY=your_api_key
GITHUB_TOTP_SECRET=your_secret # Only for GitHub auth tests
```## Web app development
This guide will help you set up the Shortest web app for local development.
### Prerequisites
- React >=19.0.0 (if using with Next.js 14+ or Server Actions)
- Next.js >=14.0.0 (if using Server Components/Actions)> [!WARNING]
> Using this package with React 18 in Next.js 14+ projects may cause type conflicts with Server Actions and `useFormStatus`
>
> If you encounter type errors with form actions or React hooks, ensure you're using React 19### Getting started
1. Clone the repository:
```bash
git clone https://github.com/anti-work/shortest.git
cd shortest
```2. Install dependencies:
```bash
npm install -g pnpm
pnpm install
```### Environment setup
#### For Anti-Work team members
Pull Vercel env vars:
```bash
pnpm i -g vercel
vercel link
vercel env pull
```#### For other contributors
1. Run `pnpm run setup` to configure the environment variables.
2. The setup wizard will ask you for information. Refer to "Services Configuration" section below for more details.### Set up the database
```bash
pnpm drizzle-kit generate
pnpm db:migrate
pnpm db:seed # creates stripe products, currently unused
```### Services configuration
You'll need to set up the following services for local development. If you're not a Anti-Work Vercel team member, you'll need to either run the setup wizard `pnpm run setup` or manually configure each of these services and add the corresponding environment variables to your `.env.local` file:
Clerk
1. Go to [clerk.com](https://clerk.com) and create a new app.
2. Name it whatever you like and **disable all login methods except GitHub**.
![Clerk App Login](https://github.com/user-attachments/assets/1de7aebc-8e9d-431a-ae13-af60635307a1)
3. Once created, copy the environment variables to your `.env.local` file.
![Clerk Env Variables](https://github.com/user-attachments/assets/df3381e6-017a-4e01-8bd3-5793e5f5d31e)
4. In the Clerk dashboard, disable the "Require the same device and browser" setting to ensure tests with Mailosaur work properly.Vercel Postgres
1. Go to your dashboard at [vercel.com](https://vercel.com).
2. Navigate to the Storage tab and click the `Create Database` button.
![Vercel Create Database](https://github.com/user-attachments/assets/acdf3ba7-31a6-498b-860c-171018d5ba02)
3. Choose `Postgres` from the `Browse Storage` menu.
![Neon Postgres](https://github.com/user-attachments/assets/9ad2a391-5213-4f31-a6c3-b9e54c69bb2e)
4. Copy your environment variables from the `Quickstart` `.env.local` tab.
![Vercel Postgres .env.local](https://github.com/user-attachments/assets/e48f1d96-2fd6-4e2e-aaa6-eeb5922cc521)Anthropic
1. Go to your dashboard at [anthropic.com](https://anthropic.com) and grab your API Key.
- Note: If you've never done this before, you will need to answer some questions and likely load your account with a balance. Not much is needed to test the app.
![Anthropic API Key](https://github.com/user-attachments/assets/0905ed4b-5815-4d50-bf43-8713a4397674)Stripe
1. Go to your `Developers` dashboard at [stripe.com](https://stripe.com).
2. Turn on `Test mode`.
3. Go to the `API Keys` tab and copy your `Secret key`.
![Stripe Secret Key](https://github.com/user-attachments/assets/0830b226-f2c2-4b92-a28f-f4682ad03ec0)
4. Go to the terminal of your project and type `pnpm run stripe:webhooks`. It will prompt you to login with a code then give you your `STRIPE_WEBHOOK_SECRET`.
![Stripe Webhook Secret](https://github.com/user-attachments/assets/b02531ed-5c31-40ba-8483-32880aa3ca36)GitHub OAuth
1. Create a GitHub OAuth App:
- Go to your GitHub account settings.
- Navigate to `Developer settings` > `OAuth Apps` > `New OAuth App`.
- Fill in the application details:
- **Application name**: Choose any name for your app
- **Homepage URL**: Set to `http://localhost:3000` for local development
- **Authorization callback URL**: Use the Clerk-provided callback URL (found in below image)
![Github OAuth App](https://github.com/user-attachments/assets/1af635fd-dedc-401c-a45a-159cb20bb209)2. Configure Clerk with GitHub OAuth:
- Go to your Clerk dashboard.
- Navigate to `Configure` > `SSO Connections` > `GitHub`.
- Select `Use custom credentials`
- Enter your `Client ID` and `Client Secret` from the GitHub OAuth app you just created.
- Add `repo` to the `Scopes`
![Clerk Custom Credentials](https://github.com/user-attachments/assets/31d414e1-4e1e-4725-8649-ec1826c6e53e)Mailosaur
1. [Sign up](https://mailosaur.com/app/signup) for an account with Mailosaur.
2. Create a new Inbox/Server.
3. Go to [API Keys](https://mailosaur.com/app/keys) and create a standard key.
4. Update the environment variables:
- `MAILOSAUR_API_KEY`: Your API key
- `MAILOSAUR_SERVER_ID`: Your server IDThe email used to test the login flow will have the format `[email protected]`, where
`MAILOSAUR_SERVER_ID` is your server ID.
Make sure to add the email as a new user under the Clerk app.### Running locally
Run the development server:
```bash
pnpm dev
```Open [http://localhost:3000](http://localhost:3000) in your browser to see the app in action.
## Shortest CLI development
1. Make changes to the package source code in `packages/shortest/`
2. Build the package and test the changes:
```bash
# One-time build
pnpm cli:build# Watch mode (rebuilds on changes)
pnpm cli:dev# Test changes
pnpm shortest --help
```3. To test in another project:
```bash
# In Shortest package directory
cd packages/shortest
pnpm pack# In your test project
npm install /path/to/antiwork-shortest-{version}.tgz
npx shortest -h
```