Selenium WebDriver kwa kutumia Node.js Guide- Selenium WebDriver Node.js

Selenium WebDriver na Node.js ni zana yenye nguvu ya kufanya majaribio ya programu ya wavuti kiotomatiki. Kwa kutumia Selenium WebDriver Node.js, unaweza kudhibiti vivinjari, kuingiliana na vipengele kwenye kurasa za wavuti, na kuandika hati za majaribio otomatiki kwa urahisi. Kwa usaidizi wa vivinjari maarufu kama Chrome, Firefox, na Safari, Selenium WebDriver hukuruhusu kujaribu programu za wavuti kwenye mifumo mingi.

Makala haya yanatoa mwongozo wa kina wa kutumia Selenium WebDriver na Node.js, usakinishaji, usanidi, na mifano ya vitendo ili kukusaidia kuanza na majaribio bora ya programu ya wavuti otomatiki.

 

Mwongozo wa kutumia Selenium WebDriver na Node.js

Sakinisha Selenium WebDriver na utegemezi

Fungua terminal haraka yako au amri na uende kwenye saraka ya mradi wako.

Tumia amri ifuatayo ya kusakinisha Selenium WebDriver na utegemezi muhimu:

npm install selenium-webdriver chromedriver

Amri hii itasakinisha Selenium WebDriver kwa Node.js na kiendeshi cha Chrome(chromedriver) kwa kudhibiti kivinjari cha Chrome.

Ingiza na uanzishe WebDriver

Ingiza inayohitajika module

const { Builder, By, Key, until } = require('selenium-webdriver');

Anzisha kitu cha WebDriver kwa kivinjari unachotaka(kwa mfano, Chrome):

const driver = new Builder().forBrowser('chrome').build();

Tumia WebDriver kuingiliana na kivinjari

Fungua URL

await driver.get('https://www.example.com');

Tafuta na uingiliane na vipengele:

// Find an element by ID  
const element = await driver.findElement(By.id('my-element-id'));  
  
// Enter text into an input element  
await element.sendKeys('Hello, World!');  
  
// Press the Enter key  
await element.sendKeys(Key.ENTER);  
  
// Wait for an element to be located  
await driver.wait(until.elementLocated(By.css('.my-element-class')));  
  
// Click on an element  
await element.click();  

Unaweza kutumia mbinu kama findElement, sendKeys, click, wait, n.k., kuingiliana na vipengele kwenye ukurasa wa wavuti.

Funga WebDriver

Funga kivinjari na umalize kipindi:

await driver.quit();

 

Hapa kuna mfano wa kina wa kutafuta na kuingiza data kwenye uwanja wa uingizaji kwenye ukurasa wa wavuti:

const { Builder, By, Key, until } = require('selenium-webdriver');  
  
async function runTest() {  
  try {  
    const driver = new Builder().forBrowser('chrome').build();  
  
    await driver.get('https://www.example.com');  
  
    // Find the input element by ID  
    const inputElement = await driver.findElement(By.id('my-input-id'));  
  
    // Enter data into the input field  
    await inputElement.sendKeys('Hello, World!');  
  
    // Press the Enter key  
    await inputElement.sendKeys(Key.ENTER);  
  
    // Close the browser  
    await driver.quit();  
  } catch(error) {  
    console.error('Test failed:', error);  
  }  
}  
  
runTest();  

 

Katika mfano huu, tunapata kipengele cha ingizo kwa kitambulisho( my-input-id), kisha tumia sendKeys mbinu ya kuingiza data kwenye uga wa ingizo. Hatimaye, tunabonyeza kitufe cha Ingiza kwa kutumia sendKeys(Key.ENTER) na kufunga kivinjari kwa driver.quit().