@appium/css-locator-to-native

中文文档
1.0.7发布于 2026/09/01Apache-2.0TypeScriptES Module构建来源已验证近 30 天下载 77.1万

Platform-agnostic CSS selector parsing and normalization for native locator transformation

原始使用文档
文档目录11 节
  1. Installation
  2. Quick start
  3. API
  4. ParsedSelector IR
  5. Attribute schemas
  6. Multi-strategy routing
  7. Driver integration pattern
  8. Supported CSS subset
  9. Development
  10. Requirements
  11. License

@appium/css-locator-to-native

Platform-agnostic CSS selector parsing and normalization for native locator transformation.

Installation

npm install @appium/css-locator-to-native

Quick start

import {
  createCssTransformer,
  type AttributeSchema,
  type ParsedSelector,
  type StrategyKey,
} from '@appium/css-locator-to-native';

const schema: AttributeSchema = {
  attributes: {
    visible: {type: 'boolean'},
    name: {type: 'string', aliases: ['id']},
    index: {type: 'numeric', aliases: ['nth-child']},
  },
  booleanFormat: 'zero-one',
};

const emitters = {
  native: {
    strategy: 'my-native-strategy',
    emit(parsed: ParsedSelector) {
      // Map ParsedSelector IR to your platform's selector syntax
      return parsed.rule.tag ?? '*';
    },
  },
};

const transformCss = createCssTransformer({
  schema,
  emitters,
  resolveStrategy(): StrategyKey<typeof emitters> {
    return 'native';
  },
});

const {strategy, selector} = transformCss('window#foo[visible]');
// => { strategy: 'my-native-strategy', selector: '...' }

API

Everything is exported from a single entry point:

ExportDescription
normalizeCssSelector(css, schema)Parse and normalize a CSS selector into a ParsedSelector IR
createCssTransformer(config)Returns a function that parses CSS and produces a NativeLocator
InvalidSelectorErrorCSS syntax cannot be parsed
UnsupportedSelectorErrorParsed CSS uses unsupported features or unknown attributes
UnresolvedStrategyErrorNo matching emitter for the resolved strategy key

ParsedSelector IR

Normalization produces a platform-agnostic intermediate representation. The IR captures CSS structure — tags, classes, #id, attributes, pseudos, combinators — validated against your schema. It does not encode iOS class chain, UiAutomator, or any other native syntax.

interface ParsedSelector {
  rule: ParsedRule; // first comma-separated rule only
}

interface ParsedRule {
  combinator?: 'descendant' | 'child';
  tag?: string;           // raw CSS tag (may be '*')
  classes: string[];      // raw class tokens
  id?: string;            // raw #id value
  attributes: ParsedAttribute[];
  pseudos: ParsedAttribute[];
  nested?: ParsedRule;
}

Attribute schemas

Drivers declare which CSS attributes are valid and how booleans are coerced:

interface AttributeSchema {
  attributes: Record<string, {type: 'boolean' | 'string' | 'numeric'; aliases?: string[]}>;
  booleanFormat?: 'zero-one' | 'true-false' | 'literal';
}
  • zero-one — true/1/empty → '1', false/0 → '0'
  • true-false — true/empty → 'true', false → 'false'
  • literal (default) — keep the raw attribute value unchanged; implicit booleans (no value) stay unset

Tag, class, and #id mapping (e.g. XCUIElementType* prefixing, resourceId prefixing) is entirely the driver's responsibility in emitters.

Multi-strategy routing

createCssTransformer requires a registry of StrategyEmitters and a resolveStrategy callback. The resolver picks which emitter to use based on selector shape; the transformer returns both the target strategy name and the native selector string.

interface NativeLocator {
  strategy: string;
  selector: string;
}

A driver with a single target strategy uses a one-entry registry and a trivial resolver.

Driver integration pattern

Platform logic stays in the driver. A typical layout:

lib/css/
  schema.ts          # AttributeSchema for the platform
  *-emitter.ts       # StrategyEmitter implementations
  resolve-helpers.ts # isSimpleIdSelector, etc.
  index.ts           # createCssTransformer wrapper

In find commands, use the strategy from the transform result instead of hardcoding it:

if (strategy === 'css selector') {
  ({strategy, selector} = cssToNativeLocator(selector));
}

Supported CSS subset

Matches the subset accepted by existing Appium drivers:

  • Tags (including *), #id, classes, attribute selectors (=, *=, ^=, $=, ~=)
  • Child (>) and descendant (space) combinators
  • Pseudo-classes accepted as attributes per schema (e.g. :visible, :nth-child(2))
  • First comma-separated rule only (additional rules are ignored)
  • Unsupported: + / ~ combinators, pseudo-elements, nesting (&), namespaces

Development

npm install --no-package-lock
npm run typecheck   # type-check lib + test
npm run build       # compile to build/
npm test            # unit tests (compiled ESM)
npm run lint
npm run format:check

Requirements

  • Node.js ^20.19.0 || ^22.12.0 || >=24.0.0
  • npm >=10

License

Apache-2.0

版本与发布记录

@appium/css-locator-to-native 共发布 8 个版本,首次发布于 2026/06/08;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.7latest2026/09/012 个月前
1.0.62026/07/293 个月前
1.0.52026/07/074 个月前
1.0.42026/07/074 个月前
1.0.32026/07/024 个月前
1.0.22026/07/024 个月前

关于 @appium/css-locator-to-native 的常见问题

@appium/css-locator-to-native 是什么?

Platform-agnostic CSS selector parsing and normalization for native locator transformation

如何安装 @appium/css-locator-to-native?

在项目目录运行 npm install @appium/css-locator-to-native,或使用 pnpm add / yarn add / bun add。

@appium/css-locator-to-native 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@appium/css-locator-to-native 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@appium/css-locator-to-native 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js ^20.19.0 || ^22.12.0 || >=24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@appium/css-locator-to-native 可以商用吗?

@appium/css-locator-to-native 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@appium/css-locator-to-native 的下载量有多少?

根据 npm 官方下载统计,@appium/css-locator-to-native 在 2026/10/07 – 2026/11/05 这 30 天内被下载 770,639 次,日均约 25,688 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@appium/css-locator-to-native 还在维护吗?

最新版本 1.0.7 发布于 2026/09/01(2 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 8 个;首次发布于 2026/06/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。