CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-dcloudio--uni-automator

Automated testing framework for uni-app projects enabling cross-platform testing across H5, WeChat mini-programs, and mobile apps

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

program.mddocs/

Program Control

Main interface for controlling the uni-app application during testing, providing navigation, system integration, and testing utilities.

Capabilities

Navigation Methods

Core navigation functionality for moving between pages and managing the application's page stack.

/**
 * Navigate to a new page without closing the current page
 * @param url - Target page path (e.g., "/pages/detail/detail")
 * @returns Promise resolving to the new Page instance
 */
navigateTo(url: string): Promise<Page>;

/**
 * Redirect to a new page, closing the current page
 * @param url - Target page path
 * @returns Promise resolving to the new Page instance
 */
redirectTo(url: string): Promise<Page>;

/**
 * Navigate back in the page history stack
 * @returns Promise resolving to the previous Page instance
 */
navigateBack(): Promise<Page>;

/**
 * Restart the application and navigate to the specified page
 * @param url - Target page path
 * @returns Promise resolving to the new Page instance
 */
reLaunch(url: string): Promise<Page>;

/**
 * Switch to a tab page (for tab bar applications)
 * @param url - Target tab page path
 * @returns Promise resolving to the tab Page instance
 */
switchTab(url: string): Promise<Page>;

Usage Examples:

// Basic navigation
const detailPage = await program.navigateTo("/pages/detail/detail?id=123");
const listPage = await program.navigateBack();

// Application restart
const homePage = await program.reLaunch("/pages/index/index");

// Tab navigation
const profilePage = await program.switchTab("/pages/profile/profile");

Page Management

Methods for managing and inspecting the application's page stack and current page state.

/**
 * Get all pages in the current navigation stack
 * @returns Promise resolving to array of Page instances
 */
pageStack(): Promise<Page[]>;

/**
 * Get the currently active page
 * @returns Promise resolving to the current Page instance
 */
currentPage(): Promise<Page>;

Usage Examples:

// Check page stack depth
const pages = await program.pageStack();
console.log(`Current stack depth: ${pages.length}`);

// Get current page information
const current = await program.currentPage();
console.log(`Current page: ${current.path}`);

// Navigate and verify
await program.navigateTo("/pages/detail/detail");
const newCurrent = await program.currentPage();
expect(newCurrent.path).toBe("pages/detail/detail");

System Integration

Interface for calling uni-app APIs and accessing system information during testing.

/**
 * Get system information (device, screen dimensions, etc.)
 * @returns Promise resolving to SystemInfo object
 */
systemInfo(): Promise<SystemInfo>;

/**
 * Call any uni-app API method
 * @param method - uni-app API method name (e.g., "getStorageSync")
 * @param args - Arguments to pass to the method
 * @returns Promise resolving to the API method result
 */
callUniMethod(method: string, ...args: any[]): Promise<any>;

/**
 * Mock a uni-app API method for testing
 * @param method - Method name to mock
 * @param implementation - Mock implementation (function or return value, optional)
 * @param args - Additional arguments for function-based mocks
 * @returns Promise resolving when mock is set up
 */
mockUniMethod(method: string, implementation?: any, ...args: any[]): Promise<void>;

/**
 * Restore original implementation of a mocked uni-app method
 * @param method - Method name to restore
 * @returns Promise resolving when method is restored
 */
restoreUniMethod(method: string): Promise<void>;

/**
 * Execute JavaScript code in the application context
 * @param fn - Function to execute in app context
 * @param args - Arguments to pass to the function
 * @returns Promise resolving to the function's return value
 */
evaluate(fn: Function, ...args: any[]): Promise<any>;

/**
 * Scroll the page to specific vertical position
 * @param scrollTop - Vertical scroll position in pixels
 * @returns Promise resolving when scroll is complete
 */
pageScrollTo(scrollTop: number): Promise<void>;

Usage Examples:

// Get system information
const sysInfo = await program.systemInfo();
console.log(`Platform: ${sysInfo.platform}, Screen: ${sysInfo.screenWidth}x${sysInfo.screenHeight}`);

// Call uni-app APIs
await program.callUniMethod("setStorageSync", "key", "value");
const value = await program.callUniMethod("getStorageSync", "key");

// Mock API for testing
await program.mockUniMethod("showToast", { success: true });
await program.callUniMethod("showToast", { title: "Test" }); // Uses mock

// Restore original implementation
await program.restoreUniMethod("showToast");

// Execute custom code
const result = await program.evaluate(() => {
  return document.title;
});

// Scroll page to specific position
await program.pageScrollTo(500); // Scroll down 500px
await program.pageScrollTo(0);   // Scroll to top

Testing Utilities

Specialized methods for testing scenarios including screenshots, remote debugging, and function exposure.

/**
 * Capture a screenshot of the application
 * @param options - Screenshot configuration options
 * @returns Promise resolving to base64 image data or file path
 */
screenshot(options?: ScreenshotOptions): Promise<string>;

/**
 * Enable remote debugging with QR code display
 * @param autoconnect - Whether to auto-connect to remote session
 * @returns Promise resolving when remote debugging is enabled
 */
remote(autoconnect?: boolean): Promise<void>;

/**
 * Expose a function to the application context for testing
 * @param name - Function name to expose in app context
 * @param fn - Function to expose
 * @returns Promise resolving when function is exposed
 */
exposeFunction(name: string, fn: Function): Promise<void>;

/**
 * Get available test accounts (WeChat mini-program only)
 * @returns Promise resolving to array of test account information
 */
testAccounts(): Promise<TestAccount[]>;

/**
 * Check version compatibility between automator and application
 * @returns Promise resolving when version check is complete
 */
checkVersion(): Promise<void>;

Usage Examples:

// Take screenshot
const screenshotData = await program.screenshot({
  path: "test-results/homepage.png",
  fullPage: true
});

// Enable remote debugging (WeChat mini-program)
await program.remote(true); // Shows QR code and auto-connects

// Expose test helper function
await program.exposeFunction("testHelper", (data) => {
  console.log("Test data:", data);
  return { processed: true };
});

// Get WeChat test accounts
const accounts = await program.testAccounts();
console.log("Available test accounts:", accounts);

Lifecycle Management

Methods for managing the testing session lifecycle and cleanup.

/**
 * Close the application and terminate the testing session
 * @returns Promise resolving when application is closed
 */
close(): Promise<void>;

/**
 * Clean shutdown based on teardown configuration
 * @returns Promise resolving when teardown is complete
 */
teardown(): Promise<void>;

/**
 * Disconnect from runtime without closing the application
 * @returns Promise resolving when disconnected
 */
disconnect(): Promise<void>;

Usage Examples:

// Clean shutdown (recommended)
afterAll(async () => {
  await program.teardown();
});

// Force close application
await program.close();

// Disconnect but leave app running
await program.disconnect();

Event Handling

Listen to application events during testing for debugging and monitoring.

/**
 * Listen to application events
 * @param event - Event name to listen for
 * @param callback - Event handler function
 * @returns Program instance for chaining
 */
on(event: "console" | "exception", callback: Function): Program;

Usage Examples:

// Listen to console output
program.on("console", (message) => {
  console.log("App console:", message);
});

// Listen to exceptions
program.on("exception", (error) => {
  console.error("App exception:", error);
});

Types

interface SystemInfo {
  platform: string;
  system: string;
  version: string;
  screenWidth: number;
  screenHeight: number;
  windowWidth: number;
  windowHeight: number;
  pixelRatio: number;
  statusBarHeight: number;
  safeArea: {
    left: number;
    right: number;
    top: number;
    bottom: number;
    width: number;
    height: number;
  };
}

interface ScreenshotOptions {
  /** Output file path (optional, returns base64 if not specified) */
  path?: string;
  /** Capture full page including scrolled content */
  fullPage?: boolean;
}

interface TestAccount {
  account: string;
  nickName: string;
  avatar: string;
}

Install with Tessl CLI

npx tessl i tessl/npm-dcloudio--uni-automator

docs

element.md

index.md

launcher.md

page.md

program.md

tile.json