कस्टम कमांड
यदि आप browser
उदाहरण को अपने स्वयं के आदेशों के सेट के साथ विस्तारित करना चाहते हैं, तो ब्राउज़र विधि addCommand
यहां आपके लिए है। आप अपनी कमांड को एसिंक्रोनस तरीके से लिख सकते हैं, ठीक अपने स्पेक्स की तरह।
पैरामीटर
कमान का नाम
एक नाम जो कमांड को परिभाषित करता है और ब्राउजर या एलिमेंट स्कोप से जुड़ा होगा।
प्रकार: String
कस्टम फंक्शन
एक फ़ंक्शन जिसे कमांड कहे जाने पर निष्पादित किया जा रहा है। this
स्कोप या तो WebdriverIO.Browser
या WebdriverIO.Element
है जो इस बात पर निर्भर करता है कि कमांड ब्राउज़र या एलिमेंट स्कोप से जुड़ी है या नहीं।
टाइप: Function
टारगेट स्कोप
यह तय करने के लिए फ्लैग करें कि कमांड को ब्राउजर या एलिमेंट स्कोप से अटैच करना है या नहीं। अगर true
पर सेट किया जाता है तो कमांड एक एलिमेंट कमांड होगी।
Type: Boolean
Default: false
उदाहरण
यह उदाहरण दिखाता है कि एक नया कमांड कैसे जोड़ा जाए जो वर्तमान यूआरएल और शीर्षक को एक परिणाम के रूप में लौटाता है। स्कोप (this
) एक WebdriverIO.Browser
ऑब्जेक्ट है।
browser.addCommand('getUrlAndTitle', async function (customVar) {
// `this` refers to the `browser` scope
return {
url: await this.getUrl(),
title: await this.getTitle(),
customVar: customVar
}
})
इसके अतिरिक्त, आप अंतिम तर्क के रूप में true
पास करके, अपने स्वयं के कमांड के सेट के साथ तत्व उदाहरण का विस्तार कर सकते हैं। इस मामले में दायरा (this
) एक WebdriverIO.Element
ऑब्जेक्ट है।
browser.addCommand("waitAndClick", async function () {
// `this` is return value of $(selector)
await this.waitForDisplayed()
await this.click()
}, true)
कस्टम कमांड आपको एक कॉल के रूप में अक्सर उपयोग किए जाने वाले कमांड के विशिष्ट अनुक्रम को बंडल करने का अवसर देते हैं। आप अपने टेस्ट सूट में किसी भी समय कस्टम कमांड को परिभाषित कर सकते हैं; बस यह सुनिश्चित कर लें कि कमांड को इसके पहले उपयोग के पहले पहले परिभाषित किया गया है। (आपके wdio.conf.js
में before
उन्हें बनाने के लिए एक अच्छी जगह है।)
एक बार परिभाषित करने के बाद, आप उन्हें निम्नानुसार उपयोग कर सकते हैं:
it('should use my custom command', async () => {
await browser.url('http://www.github.com')
const result = await browser.getUrlAndTitle('foobar')
assert.strictEqual(result.url, 'https://github.com/')
assert.strictEqual(result.title, 'GitHub · Where software is built')
assert.strictEqual(result.customVar, 'foobar')
})
Note: यदि आप browser
स्कोप में एक कस्टम कमांड पंजीकृत करते हैं, तो कमांड तत्वों के लिए एक्सेस योग्य नहीं होगा। इसी तरह, यदि आप एलिमेंट स्कोप में कमांड रजिस्टर करते हैं, तो यह browser
स्कोप में एक्सेस नहीं किया जा सकेगा:
browser.addCommand("myCustomBrowserCommand", () => { return 1 })
const elem = await $('body')
console.log(typeof browser.myCustomBrowserCommand) // outputs "function"
console.log(typeof elem.myCustomBrowserCommand()) // outputs "undefined"
browser.addCommand("myCustomElementCommand", () => { return 1 }, true)
const elem2 = await $('body')
console.log(typeof browser.myCustomElementCommand) // outputs "undefined"
console.log(await elem2.myCustomElementCommand('foobar')) // outputs "1"
const elem3 = await $('body')
elem3.addCommand("myCustomElementCommand2", () => { return 2 })
console.log(typeof browser.myCustomElementCommand2) // outputs "undefined"
console.log(await elem3.myCustomElementCommand2('foobar')) // outputs "2"
Note: यदि आपको कस्टम कमांड को चेन करने की आवश्यकता है, तो कमांड $
के साथ समाप्त होनी चाहिए,
browser.addCommand("user$", (locator) => { return ele })
browser.addCommand("user$", (locator) => { return ele }, true)
await browser.user$('foo').user$('bar').click()
बहुत सारे कस्टम कमांड के साथ browser
स्कोप को ओवरलोड न करने के लिए सावधान रहें।
हम पेज ऑब्जेक्टमें कस्टम लॉजिक को परिभाषित करने की सलाह देते हैं, ताकि वे एक विशिष्ट पेज से बंधे रहें।
प्रकार परिभाषाएँ बढ़ाएँ
टाइपस्क्रिप्ट के साथ, WebdriverIO इंटरफेस का विस्तार करना आसान है। इस तरह अपने कस्टम कमांड में प्रकार जोड़ें:
-
एक प्रकार की परिभाषा फ़ाइल बनाएँ (उदाहरण के लिए,
./src/types/wdio.d.ts
) -
ए यदि मॉड्यूल-शैली प्रकार की परिभाषा फ़ाइल का उपयोग कर रहे हैं (आयात/निर्यात का उपयोग करके और
declare global WebdriverIO
करें), फ़ाइल पथ कोtsconfig.json
include
संपत्ति में शामिल करना सुनिश्चित करें।बी। यदि परिवेश-शैली प्रकार की परिभाषा फ़ाइलों का उपयोग कर रहे हैं (प्रकार परिभाषा फ़ाइलों में कोई आयात/निर्यात नहीं है और
declare namespace WebdriverIO
घोषित करें), सुनिश्चित करें किtsconfig.json
नहीं में कोईinclude
है, क्योंकि यह होगा में सूचीबद्ध नहीं होने वाली सभी प्रकार की परिभाषा फ़ाइलों में अनुभागinclude
हैं जिन्हें टाइपस्क्रिप्ट द्वारा पहचाना नहीं जा सकता है।
- Modules (using import/export)
- Ambient Type Definitions (no tsconfig include)
{
"compilerOptions": { ... },
"include": [
"./test/**/*.ts",
"./src/types/**/*.ts"
]
}
{
"compilerOptions": { ... }
}
- अपने निष्पादन मोड के अनुसार अपने कमांड के लिए परिभाषाएँ जोड़ें।
- Modules (using import/export)
- Ambient Type Definitions
declare global {
namespace WebdriverIO {
interface Browser {
browserCustomCommand: (arg: any) => Promise<void>
}
interface MultiRemoteBrowser {
browserCustomCommand: (arg: any) => Promise<void>
}
interface Element {
elementCustomCommand: (arg: any) => Promise<number>
}
}
}
declare namespace WebdriverIO {
interface Browser {
browserCustomCommand: (arg: any) => Promise<void>
}
interface MultiRemoteBrowser {
browserCustomCommand: (arg: any) => Promise<void>
}
interface Element {
elementCustomCommand: (arg: any) => Promise<number>
}
}