मुख्य कॉन्टेंट में जाएँ

टाइपस्क्रिप्ट सेटअप

स्वत: पूर्णता और टाइप सुरक्षा प्राप्त करने के लिए आप टाइपस्क्रिप्ट का उपयोग करके परीक्षण लिख सकते हैं।

You will need tsx installed in devDependencies, via:

$ npm install tsx --save-dev

WebdriverIO स्वचालित रूप से पता लगाएगा कि क्या ये निर्भरताएँ स्थापित हैं और आपके लिए आपके कॉन्फ़िगरेशन और परीक्षणों को संकलित करेगा। Ensure to have a tsconfig.json in the same directory as your WDIO config.

Custom TSConfig

If you need to set a different path for tsconfig.json please set the TSCONFIG_PATH environment variable with your desired path, or use wdio config's tsConfigPath setting.

Alternatively, you can use the environment variable for tsx.

Type Checking

Note that tsx does not support type-checking - if you wish to check your types then you will need to do this in a separate step with tsc.

Framework Setup

Your tsconfig.json needs the following:

tsconfig.json
{
"compilerOptions": {
"types": ["node", "@wdio/globals/types"]
}
}

Please avoid importing webdriverio or @wdio/sync explicitly. WebdriverIO and WebDriver types are accessible from anywhere once added to types in tsconfig.json. If you use additional WebdriverIO services, plugins or the devtools automation package, please also add them to the types list as many provide additional typings.

Framework Types

Depending on the framework you use, you will need to add the types for that framework to your tsconfig.json types property, as well as install its type definitions. This is especially important if you want to have type support for the built-in assertion library expect-webdriverio.

For instance, if you decide to use the Mocha framework, you need to install @types/mocha and add it like this to have all types globally available:

tsconfig.json
{
"compilerOptions": {
"types": ["node", "@wdio/globals/types", "@wdio/mocha-framework"]
}
}

Services

यदि आप ऐसी सेवाओं का उपयोग करते हैं जो ब्राउज़र क्षेत्र में कमांड जोड़ती हैं तो आपको इन्हें अपने tsconfig.jsonमें भी शामिल करना होगा। उदाहरण के लिए यदि आप @wdio/lighthouse-service का उपयोग करते हैं तो सुनिश्चित करें कि आप इसे types में भी जोड़ते हैं, जैसे:

tsconfig.json
{
"compilerOptions": {
"types": [
"node",
"@wdio/globals/types",
"@wdio/mocha-framework",
"@wdio/lighthouse-service"
]
}
}

Adding services and reporters to your TypeScript config also strengthen the type safety of your WebdriverIO config file.

Type Definitions

When running WebdriverIO commands all properties are usually typed so that you don't have to deal with importing additional types. However there are cases where you want to define variables upfront. To ensure that these are type safe you can use all types defined in the @wdio/types package. For example if you like to define the remote option for webdriverio you can do:

import type { Options } from '@wdio/types'

// Here is an example where you might want to import the types directly
const remoteConfig: Options.WebdriverIO = {
hostname: 'http://localhost',
port: '4444' // Error: Type 'string' is not assignable to type 'number'.ts(2322)
capabilities: {
browserName: 'chrome'
}
}

// For other cases, you can use the `WebdriverIO` namespace
export const config: WebdriverIO.Config = {
...remoteConfig
// Other configs options
}

युक्तियाँ और संकेत

संकलन & लिंट

पूरी तरह से सुरक्षित होने के लिए, आप सर्वोत्तम प्रथाओं का पालन करने पर विचार कर सकते हैं: अपने कोड को टाइपस्क्रिप्ट कंपाइलर ( tsc या npx tscचलाएँ) के साथ संकलित करें और प्री-कमिट हुकपर एस्लिंट चलाएँ।

Welcome! How can I help?

WebdriverIO AI Copilot