Selenium VBA is a Windows COM library that uses the popular Selenium web testing tool.
It comes usefull to :
This project provides :
There are multiple ways to get documentation:
Here is an example which launches firefox, opens the science page of Yahoo news, the politics page and closes the browser :Public Sub TC_Browsers() Dim driver As New SeleniumWrapper.WebDriver driver.start "firefox", "http://news.yahoo.com" driver.open "/science" driver.open "/politics" driver.stop End Sub
There are 4 browsers that can be controlled locally that need to be installed to work with:
Another feature is the possibility to control a browser remotely using the the remote web driver server: https://code.google.com/p/selenium/wiki/RemoteWebDriverServer
Here is a example to remotely control Firefox on the station 192.168.0.26 :Public Sub TC_Browsers() Dim driver As New SeleniumWrapper.WebDriver driver.startRemotely "firefox", "http://192.168.0.26:4444/wd/hub" "http://news.yahoo.com" driver.open "/science" ... End Sub
Using Selenium IDE, copy the recorded selector in the field "Target".
In Firefox, right click on the element and click on inspect element
This example runs a simple web search and pastes a screenshot of the result in a worksheet:Public Sub TC001() Dim selenium As New SeleniumWrapper.WebDriver selenium.start "firefox", "http://www.google.com/" selenium.open "/" selenium.type "name=q", "Eiffel tower" selenium.click "name=btnG" selenium.wait 1000 selenium.getScreenshot().copy selenium.stop Sheets(1).Range("A10").PasteSpecial 'Paste the screenshoot at range A10 End Sub
In this example, a worksheet contains a list of Urls and expected titles. Each Url is oppend in Firefox, then the web page title is compared with the expected one and the result is sent back to the worksheet.Public Sub TC002() Dim selenium As New SeleniumWrapper.WebDriver, r As Range selenium.Start "firefox", "http://www.google.com" 'Launch Firefox For Each r In Range("MyValues").Rows 'Loop for each row in the range named "MyValues" selenium.open r.Cells(1, 1).Text 'open the link defined in the first column of "MyValues" selenium.waitForNotTitle "" 'wait for the title to load r.Cells(1, 3) = selenium.verifyTitle(r.Cells(1, 2)) 'Compare the page title with the second column and write the verification result in the third column Next selenium.stop End SubThis wrapper implements assertion and verification commands. When an assert fails (ex: assertTitle), an error popup appears and the execution is stopped. A verification command (ex: verifyTitle) just returns a string with the test result without breaking.
This scrip will help you to quickly inspect a list of web pages without having to navigate the website and wait for page loading. It automatically opens the webpage, takes a screenshot and saves it in a Pdf file:Set selenium = CreateObject("SeleniumWrapper.WebDriver") Set pdf = CreateObject("SeleniumWrapper.PdfFile") selenium.start "firefox", "http://www.google.com" selenium.open "search?q=eiffel+tower" pdf.addImage selenium.getScreenshot(), "Google search - Eiffel tower" selenium.open "http://maps.google.com/maps?q=eiffel+tower" pdf.addImage selenium.getScreenshot(), "Google map - Eiffel tower" pdf.saveAs "c:\selenium-capture.pdf" selenium.stop
This example gets all the world market indexes in the first worksheet and all the top stories tiltles in the second one:Public Sub TC003() Dim driver As New SeleniumWrapper.WebDriver driver.Start "chrome", "https://www.google.co.uk" 'Starts the browser driver.Open "/finance" 'Opens the finance page Dim data1, data2 data1 = driver.findElementByCssSelector("#markets table").AsTable.GetData() 'Gets the world market indexes from the table Sheet1.[A1].Resize(UBound(data2, 1), UBound(data2, 2)).Value = data1 'Writes the collected data in the first worksheet data2 = driver.findElementsByCssSelector("#market-news-stream a.title").GetData() 'Gets the top stories titles Sheet2.[A1].Resize(UBound(data2)).Value = data2 'Writes the collected data in the second worksheet driver.stop 'Stops the browser End Sub
Getting tired playing "Where is Wally?" for each delivery ?
The getPerformanceTiming command returns latency-related performance informations :
This example navigates through Yahoo news and writes the metrics for each web page in an Excel WorkSheet:Function NextRow() As Range Static i As Integer i = IIf(i = 0, 1, i + 1) 'Increments the row index Set NextRow = Range("A:I").Rows(i) 'Returns the next row End Function Public Sub GetPerformanceTiming() Dim driver As New SeleniumWrapper.WebDriver driver.Start "firefox", "http://uk.news.yahoo.com" 'Starts firefox 'Writes the titles on the first row NextRow() = Array("Page Url", "Page load", "Redirect", "DNS", "Connecting", "Waiting", "Receiving", "DOM", "Events") 'Opens each page and copies the metrics in the WorkSheet ( 1 row for each page opened) driver.Open "tech" NextRow() = driver.getPerformanceTiming() driver.Open "world" NextRow() = driver.getPerformanceTiming() driver.Open "opinion" NextRow() = driver.getPerformanceTiming() driver.Open "business" NextRow() = driver.getPerformanceTiming() driver.stop 'Closes Firefox End Sub