किसी भी वेब डेवलपर के लिए यह सुनिश्चित करने के लिए कि उनके एप्लिकेशन सही ढंग से काम कर रहे हैं, स्वचालित ब्राउज़र परीक्षण आवश्यक है। इस पोस्ट में, हम एक सरल ब्राउज़र कार्य को स्वचालित करने के लिए जावास्क्रिप्ट के साथ सेलेनियम की स्थापना के बारे में जानेंगे: एक वेबपेज खोलना और एक बटन पर क्लिक करना।
आगे बढ़ने के लिए, आपको इसकी आवश्यकता होगी:
सबसे पहले, अपने प्रोजेक्ट के लिए एक नया फ़ोल्डर बनाएं। अपना टर्मिनल खोलें और चलाएं:
mkdir selenium-test cd selenium-test
इसके बाद, एक नया Node.js प्रोजेक्ट आरंभ करें:
npm init -y
यह कमांड एक package.json फ़ाइल बनाता है जो आपके प्रोजेक्ट की निर्भरता का ट्रैक रखता है।
हम सेलेनियम वेबड्राइवर और क्रोमड्राइवर स्थापित करने के लिए एनपीएम का उपयोग करेंगे:
npm install selenium-webdriver chromedriver --save
ये पैकेज सेलेनियम के साथ क्रोम को स्वचालित करने के लिए आवश्यक लाइब्रेरी और टूल प्रदान करते हैं।
अपने प्रोजेक्ट फ़ोल्डर में test.js नामक एक नई फ़ाइल बनाएं। यह स्क्रिप्ट एक वेबपेज खोलेगी, एक बटन के क्लिक करने योग्य होने की प्रतीक्षा करेगी और फिर उस पर क्लिक करेगी।
const { Builder, By, until } = require('selenium-webdriver'); const chrome = require('selenium-webdriver/chrome'); // Helper function to pause the script function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function runTest() { // Configure Chrome to suppress unwanted prompts let options = new chrome.Options(); options.addArguments('--no-default-browser-check', '--no-first-run', '--disable-default-apps', '--disable-infobars'); let driver = await new Builder() .forBrowser('chrome') .setChromeOptions(options) .build(); try { // Open the target webpage await driver.get('https://example.com'); // Change this URL to the site you want to test // Wait for an element to load await driver.wait(until.elementLocated(By.className('sample-class')), 10000); console.log('Found element with class "sample-class".'); // Generic wait for 6 seconds to handle any dynamic content await sleep(6000); // Wait for the button to be clickable await driver.wait(until.elementLocated(By.id('sample-button')), 10000); // Re-locate the button to ensure it’s still in the DOM let button = await driver.findElement(By.id('sample-button')); console.log('Button located:', button); // Click the button await button.click(); console.log('Button clicked successfully.'); // Wait for the next page or action to load await driver.wait(until.urlContains('new-page'), 10000); console.log('Navigation to new page was successful.'); } catch (error) { console.error('Error during the test:', error); } finally { // Always close the browser await driver.quit(); } } runTest();
अपनी स्क्रिप्ट निष्पादित करने के लिए, चलाएँ:
node test.js
क्रोम खुल जाएगा और आपकी स्क्रिप्ट में परिभाषित क्रियाएं निष्पादित करेगा। प्रत्येक चरण की प्रगति दर्शाने वाले लॉग के लिए कंसोल देखें।
अब आपके पास सेलेनियम और जावास्क्रिप्ट का उपयोग करके स्वचालित ब्राउज़र परीक्षण के लिए एक बुनियादी सेटअप है। अधिक जटिल इंटरैक्शन, जांच और सत्यापन चरणों को शामिल करने के लिए इस सेटअप को आसानी से विस्तारित किया जा सकता है।
अपने ड्राइवरों को अपने ब्राउज़र संस्करणों से मेल खाने के लिए अद्यतन रखना याद रखें और सीआई/सीडी वातावरण के लिए हेडलेस मोड का उपयोग करने पर विचार करें।
यदि आप इसे Azure में होस्ट करना चाहते हैं तो मेरी अन्य पोस्ट देखें: https://dev.to/iamrule/how-to-azure-host-a-selenium-javascript-node-application-in-azure-and -विफलताओं-2aio पर ईमेल-सूचनाएं भेजें
खुश परीक्षण!
अस्वीकरण: उपलब्ध कराए गए सभी संसाधन आंशिक रूप से इंटरनेट से हैं। यदि आपके कॉपीराइट या अन्य अधिकारों और हितों का कोई उल्लंघन होता है, तो कृपया विस्तृत कारण बताएं और कॉपीराइट या अधिकारों और हितों का प्रमाण प्रदान करें और फिर इसे ईमेल पर भेजें: [email protected] हम इसे आपके लिए यथाशीघ्र संभालेंगे।
Copyright© 2022 湘ICP备2022001581号-3