Selenium WebDriver tare da Node.js kayan aiki ne mai ƙarfi don sarrafa gwajin aikace-aikacen yanar gizo. Ta amfani Selenium WebDriver da Node.js, zaku iya sarrafa masu bincike, yin hulɗa tare da abubuwa akan shafukan yanar gizo, da rubuta rubutun gwaji na atomatik cikin sauƙi. Tare da goyan bayan mashahuran masu bincike kamar Chrome, Firefox, da Safari, Selenium WebDriver yana ba ku damar gwada aikace-aikacen yanar gizo a kan dandamali da yawa.
Wannan labarin yana ba da cikakken jagora akan amfani Selenium WebDriver da Node.js, rufe shigarwa, daidaitawa, da misalai masu amfani don taimaka muku farawa tare da ingantaccen gwajin aikace-aikacen yanar gizo mai sarrafa kansa.
Jagora don amfani Selenium WebDriver tare da Node.js
Shigar Selenium WebDriver
da abin dogaro
Buɗe terminal
umarni ko umarni kuma kewaya zuwa kundin tsarin aikin ku.
Gudun umarni mai zuwa don shigarwa Selenium WebDriver
da abubuwan da suka dace:
npm install selenium-webdriver chromedriver
Wannan umarnin zai shigar Selenium WebDriver
don Node.js da direban Chrome(chromedriver) don sarrafa mai binciken Chrome.
Shigo da fara WebDriver
Shigo da abin da ake buƙata module
const { Builder, By, Key, until } = require('selenium-webdriver');
Fara abun WebDriver don mai binciken da ake so(misali, Chrome):
const driver = new Builder().forBrowser('chrome').build();
Yi amfani da WebDriver don mu'amala da mai lilo
Bude URL
await driver.get('https://www.example.com');
Nemo ku yi hulɗa tare da abubuwa:
// 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();
Kuna iya amfani da hanyoyi kamar findElement
, sendKeys
, click
, wait
, da sauransu, don mu'amala da abubuwa a shafin yanar gizon.
Rufe WebDriver
Rufe mai lilo kuma ƙare zaman:
await driver.quit();
Ga cikakken misali na ganowa da shigar da bayanai a cikin filin shigarwa akan shafin yanar gizon:
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();
A cikin wannan misalin, zamu sami abubuwan shigarwa ta ID( my-input-id
), sannan muyi amfani da sendKeys
hanyar shigar da bayanai cikin filin shigarwa. A ƙarshe, muna danna maɓallin Shigar ta amfani sendKeys(Key.ENTER)
da kuma rufe mai lilo da driver.quit()
.