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

ब्राउज़र वस्तु

विस्तार: इवेंटएमिटर

ब्राउज़र ऑब्जेक्ट वह सत्र उदाहरण है जिसका उपयोग आप ब्राउज़र या मोबाइल डिवाइस को नियंत्रित करने के लिए करते हैं। यदि आप WDIO टेस्ट रनर का उपयोग करते हैं, तो आप वैश्विक browser या driver ऑब्जेक्ट के माध्यम से वेबड्राइवर इंस्टेंस तक पहुंच सकते हैं या इसे @wdio/globals. का उपयोग करके आयात कर सकते हैं। यदि आप स्टैंडअलोन मोड में WebdriverIO का उपयोग करते हैं, तो ब्राउज़र ऑब्जेक्ट remote विधि द्वारा लौटाया जाता है।

सेशन को टेस्ट रनर द्वारा इनिशियलाइज़ किया जाता है। वही सत्र समाप्त करने के लिए जाता है। यह टेस्ट रनर प्रक्रिया द्वारा भी किया जाता है।

विशेषताएं

ब्राउज़र ऑब्जेक्ट में निम्नलिखित गुण होते हैं:

नामप्रकारविवरण
क्षमताएंवस्तुAssigned capabilities from the remote server.
Example:
{
acceptInsecureCerts: false,
browserName: 'chrome',
browserVersion: '105.0.5195.125',
chrome: {
chromedriverVersion: '105.0.5195.52',
userDataDir: '/var/folders/3_/pzc_f56j15vbd9z3r0j050sh0000gn/T/.com.google.Chrome.76HD3S'
},
'goog:chromeOptions': { debuggerAddress: 'localhost:64679' },
networkConnectionEnabled: false,
pageLoadStrategy: 'normal',
platformName: 'mac os x',
proxy: {},
setWindowRect: true,
strictFileInteractability: false,
timeouts: { implicit: 0, pageLoad: 300000, script: 30000 },
unhandledPromptBehavior: 'dismiss and notify',
'webauthn:extension:credBlob': true,
'webauthn:extension:largeBlob': true,
'webauthn:virtualAuthenticators': true
}
अनुरोधित क्षमताएंवस्तुदूरस्थ सर्वर से अनुरोधित क्षमताएं।
उदाहरण:
{ browserName: 'chrome' }
sessionIdStringदूरस्थ सर्वर से निर्दिष्ट सत्र आईडी।
optionsObjectWebdriverIO विकल्प ब्राउज़र ऑब्जेक्ट कैसे बनाया गया था इसके आधार पर। अधिक सेटअप प्रकार देखें
commandListString[]ब्राउज़र उदाहरण के लिए पंजीकृत आदेशों की सूची
isW3CBooleanIndicates if this is a W3C session
isChromeBooleanIndicates if this Chrome instance
isFirefoxBooleanIndicates if this Firefox instance
isBidiBooleanIndicates if this session uses Bidi
isSauceBooleanIndicates if this session is Running on Sauce Labs
isMacAppBooleanIndicates if this session is Running for a native Mac App
isWindowsAppBooleanIndicates if this session is Running for a native Windows App
isMobileBooleanएक मोबाइल सत्र का संकेत देता है। मोबाइल फ़्लैग्सके अंतर्गत और देखें।
isIOSBooleanएक आईओएस सत्र इंगित करता है। मोबाइल फ़्लैग्सके अंतर्गत और देखें।
isAndroidBooleanएक एंड्राइड सत्र का संकेत देता है। मोबाइल फ़्लैग्सके अंतर्गत और देखें।
isNativeContextBooleanIndicates if the mobile is in the NATIVE_APP context. See more under Mobile Flags.
mobileContextstringThe will provide the current context the driver is in, for example NATIVE_APP, WEBVIEW_<packageName> for Android or WEBVIEW_<pid> for iOS. It will save an extra WebDriver to driver.getContext(). See more under Mobile Flags.

विधियां

आपके सत्र के लिए उपयोग किए गए ऑटोमेशन बैकएंड के आधार पर, WebdriverIO पहचानता है कि कौन से प्रोटोकॉल कमांड को ब्राउज़र ऑब्जेक्टसे जोड़ा जाएगा। उदाहरण के लिए यदि आप क्रोम में एक स्वचालित सत्र चलाते हैं, तो आपके पास क्रोमियम विशिष्ट कमांड जैसे elementHover तक पहुंच होगी, लेकिन एपियम कमांडमें से कोई भी नहीं।

इसके अलावा WebdriverIO पृष्ठ पर ब्राउज़र या तत्वों के साथ इंटरैक्ट करने के लिए उपयोग करने के लिए अनुशंसित सुविधाजनक तरीकों का एक सेट प्रदान करता है।

इसके अतिरिक्त निम्नलिखित आदेश उपलब्ध हैं:

नामपैरामीटरविवरण
addCommand- commandName (Type: String)
- fn (Type: Function)
- attachToElement (Type: boolean)
कस्टम कमांड को परिभाषित करने की अनुमति देता है जिसे रचना उद्देश्यों के लिए ब्राउज़र ऑब्जेक्ट से कॉल किया जा सकता है। कस्टम कमांड गाइड में और पढ़ें।
overwriteCommand- commandName (Type: String)
- fn (Type: Function)
- attachToElement (Type: boolean)
Allows to overwrite any browser command with custom functionality. सावधानी से उपयोग करें क्योंकि यह फ्रेमवर्क उपयोगकर्ताओं को भ्रमित कर सकता है। कस्टम कमांड गाइड में और पढ़ें।
addLocatorStrategy- strategyName (Type: String)
- fn (Type: Function)
Allows to define a custom selector strategy, read more in the Selectors guide.

टिप्पणियां

मोबाइल फ्लेग

यदि आपको इस आधार पर अपने परीक्षण को संशोधित करने की आवश्यकता है कि आपका सत्र मोबाइल डिवाइस पर चलता है या नहीं, तो आप जांच करने के लिए मोबाइल फ़्लैग्स तक पहुंच सकते हैं।

उदाहरण के लिए, यह कॉन्फ़िगरेशन दिया गया है:

// wdio.conf.js
export const config = {
// ...
capabilities: \\{
platformName: 'iOS',
app: 'net.company.SafariLauncher',
udid: '123123123123abc',
deviceName: 'iPhone',
// ...
}
// ...
}

आप इन झंडों को अपने परीक्षण में इस प्रकार एक्सेस कर सकते हैं:

// Note: `driver` is the equivalent to the `browser` object but semantically more correct
// you can choose which global variable you want to use
console.log(driver.isMobile) // outputs: true
console.log(driver.isIOS) // outputs: true
console.log(driver.isAndroid) // outputs: false

यह उपयोगी हो सकता है यदि, उदाहरण के लिए, आप डिवाइस प्रकार के आधार पर अपने पेज ऑब्जेक्ट में चयनकर्ताओं को परिभाषित करना चाहते हैं, जैसे:

// mypageobject.page.js
import Page from './page'

class LoginPage extends Page {
// ...
get username() {
const selectorAndroid = 'new UiSelector().text("Cancel").className("android.widget.Button")'
const selectorIOS = 'UIATarget.localTarget().frontMostApp().mainWindow().buttons()[0]'
const selectorType = driver.isAndroid ? 'android' : 'ios'
const selector = driver.isAndroid ? selectorAndroid : selectorIOS
return $(`${selectorType}=${selector}`)
}
// ...
}

आप कुछ डिवाइस प्रकारों के लिए केवल कुछ परीक्षण चलाने के लिए भी इन झंडों का उपयोग कर सकते हैं:

// mytest.e2e.js
describe('my test', () => {
// ...
// only run test with Android devices
if (driver.isAndroid) {
it('tests something only for Android', () => {
// ...
})
}
// ...
})

आयोजन

ब्राउज़र ऑब्जेक्ट एक EventEmitter है और आपके उपयोग के मामलों के लिए कुछ ईवेंट उत्सर्जित होते हैं।

यहाँ घटनाओं की एक सूची है। ध्यान रखें कि यह अभी उपलब्ध घटनाओं की पूरी सूची नहीं है। यहां अधिक घटनाओं के विवरण जोड़कर दस्तावेज़ को अद्यतन करने में योगदान करने के लिए स्वतंत्र महसूस करें।

request.start

This event is fired before a WebDriver request is sent to the driver. It contains information about the request and its payload.

browser.on('request.start', (ev: RequestInit) => {
// ...
})

request.end

This event is fired once the request to the driver received a response. The event object either contains the response body as result or an error if the WebDriver command failed.

browser.on('request.end', (ev: { result: unknown, error?: Error }) => {
// ...
})

request.retry

The retry event can notify you when WebdriverIO attempts to retry running the command, e.g. due to a network issue. It contains information about the error that caused the retry and the amount of retries already done.

browser.on('request.retry', (ev: { error: Error, retryCount: number }) => {
// ...
})

command

This event is emitted whenever WebdriverIO sends a WebDriver Classic command. It contains the following information:

  • command: the command name, e.g. navigateTo
  • method: the HTTP method used to send the command request, e.g. POST
  • endpoint: the command endpoint, e.g. /session/fc8dbda381a8bea36a225bd5fd0c069b/url
  • body: the command payload, e.g. { url: 'https://webdriver.io' }

result

This event is emitted whenever WebdriverIO receives a result of a WebDriver Classic command. It contains the same information as the command event with the addition of the following information:

  • result: the command result

bidiCommand

This event is emitted whenever WebdriverIO sends a WebDriver Bidi command to the browser driver. It contains information about:

  • method: WebDriver Bidi command method
  • params: associated command parameter (see API)

bidiResult

In case of a successful command execution, the event payload will be:

  • type: success
  • id: the command id
  • result: the command result (see API)

In case of a command error, the event payload will be:

  • type: error
  • id: the command id
  • error: the error code, e.g. invalid argument
  • message: details about the error
  • stacktrace: a stack trace

request.start

This event is fired before a WebDriver request is sent to the driver. It contains information about the request and its payload.

browser.on('request.start', (ev: RequestInit) => {
// ...
})

request.end

This event is fired once the request to the driver received a response. The event object either contains the response body as result or an error if the WebDriver command failed.

browser.on('request.end', (ev: { result: unknown, error?: Error }) => {
// ...
})

request.retry

The retry event can notify you when WebdriverIO attempts to retry running the command, e.g. due to a network issue. It contains information about the error that caused the retry and the amount of retries already done.

browser.on('request.retry', (ev: { error: Error, retryCount: number }) => {
// ...
})

request.performance

यह वेबड्राइवर स्तर के संचालन को मापने के लिए एक घटना है। जब भी WebdriverIO WebDriver बैकएंड को एक अनुरोध भेजता है, तो यह घटना कुछ उपयोगी जानकारी के साथ उत्सर्जित होगी:

  • durationMillisecond: मिलीसेकंड में अनुरोध की समय अवधि।
  • error: अनुरोध विफल होने पर त्रुटि वस्तु।
  • request: अनुरोध वस्तु। आप यूआरएल, विधि, शीर्षलेख इत्यादि पा सकते हैं।
  • retryCount: यदि यह 0है, तो अनुरोध पहला प्रयास था। जब WebDriverIO हुड के नीचे पुनः प्रयास करता है तो यह बढ़ जाएगा।
  • success: अनुरोध का प्रतिनिधित्व करने के लिए बूलियन सफल हुआ या नहीं। यदि यह false, error संपत्ति भी प्रदान की जाएगी।

एक उदाहरण घटना:

Object {
"durationMillisecond": 0.01770925521850586,
"error": [Error: Timeout],
"request": Object { ... },
"retryCount": 0,
"success": false,
},

कस्टम कमांड

आप आमतौर पर उपयोग किए जाने वाले वर्कफ़्लोज़ को अलग करने के लिए ब्राउज़र स्कोप पर कस्टम कमांड सेट कर सकते हैं। अधिक जानकारी के लिए कस्टम कमांड पर हमारी मार्गदर्शिका देखें।

Welcome! How can I help?

WebdriverIO AI Copilot