Created
June 1, 2016 12:55
-
-
Save amoilanen/b1ed376bac560d0a5c3ffe4375cf0897 to your computer and use it in GitHub Desktop.
Demo of Webdriver API to fetch price and rating from Amazon.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Demo of using Webdriver to automate browser testing. | |
* Fetches price and rating for a book from Amazon.com for a given book title. | |
* | |
* Example: | |
* | |
* node fetch.price.js SurviveJS | |
*/ | |
var webdriver = require('selenium-webdriver'), | |
By = webdriver.By, | |
until = webdriver.until; | |
var TIMEOUT_MS = 5000; | |
var bookTitle = process.argv[2]; | |
var driver = new webdriver.Builder() | |
.forBrowser('firefox') | |
.build(); | |
console.log('Searching Amazon.com for "' + bookTitle + '"'); | |
driver.get('http://amazon.com'); | |
driver.wait(until.elementLocated(By.id('twotabsearchtextbox')), TIMEOUT_MS); | |
driver.findElement(By.id('twotabsearchtextbox')).sendKeys(bookTitle); | |
driver.findElement(By.css('input[value=Go]')).click(); | |
driver.wait(until.elementLocated(By.id('atfResults')), TIMEOUT_MS).then(function() { | |
var firstResult = driver.findElement(By.css('.s-result-item')); | |
var bookDetails = { | |
title: firstResult.findElement(By.css('.s-access-title')), | |
price: firstResult.findElement(By.css('.a-color-price')), | |
rating: firstResult.findElement(By.css('.a-icon-star .a-icon-alt')) | |
}; | |
['title', 'price', 'rating'].forEach(function(bookField) { | |
bookDetails[bookField].getAttribute('innerHTML').then(function(text) { | |
console.log(bookField + ' = ', text); | |
}); | |
}); | |
}).catch(function() { | |
console.log('No results found for "' + bookTitle + '\"'); | |
}).finally(function() { | |
driver.quit(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment