Selenium WebDriver karo Node.js minangka alat sing kuat kanggo ngotomatisasi tes aplikasi web. Kanthi nggunakake Selenium WebDriver Node.js, sampeyan bisa ngontrol browser, sesambungan karo unsur ing kaca web, lan nulis skrip tes otomatis kanthi gampang. Kanthi dhukungan kanggo browser populer kaya Chrome, Firefox, lan Safari, Selenium WebDriver ngidini sampeyan nyoba aplikasi web ing macem-macem platform.
Artikel iki menehi panuntun rinci babagan nggunakake Selenium WebDriver Node.js, kalebu instalasi, konfigurasi, lan conto praktis kanggo mbantu sampeyan miwiti nguji aplikasi web otomatis sing efisien.
Pandhuan kanggo nggunakake Selenium WebDriver karo Node.js
Instal Selenium WebDriver
lan dependensi
Bukak terminal
utawa command prompt lan navigasi menyang direktori proyek sampeyan.
Jalanake printah ing ngisor iki kanggo nginstal Selenium WebDriver
lan dependensi sing dibutuhake:
npm install selenium-webdriver chromedriver
Printah iki bakal diinstal Selenium WebDriver
kanggo Node.js lan driver Chrome(chromedriver) kanggo ngontrol browser Chrome.
Ngimpor lan miwiti WebDriver
Ngimpor sing dibutuhake module
const { Builder, By, Key, until } = require('selenium-webdriver');
Miwiti obyek WebDriver kanggo browser sing dikarepake(contone, Chrome):
const driver = new Builder().forBrowser('chrome').build();
Gunakake WebDriver kanggo sesambungan karo browser
Bukak URL
await driver.get('https://www.example.com');
Temokake lan sesambungan karo unsur:
// 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();
Sampeyan bisa nggunakake cara kaya findElement
, sendKeys
, click
, wait
, etc., kanggo sesambungan karo unsur ing kaca web.
Nutup WebDriver
Nutup browser lan mungkasi sesi:
await driver.quit();
Punika conto rinci babagan nemokake lan nglebokake data menyang kolom input ing kaca web:
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();
Ing conto iki, kita nemokake unsur input kanthi ID( my-input-id
), banjur gunakake sendKeys
cara kanggo ngetik data menyang kolom input. Pungkasan, kita pencet tombol Ketik nggunakake sendKeys(Key.ENTER)
lan nutup browser karo driver.quit()
.