Selenium is a very classic tool for QA and it can help perform automatic checks on a website. This is an intro of how to use it:
The first step is, as always, to install and load the RSelenium package
1 2 3 |
#install to run once install.packages("RSelenium") library(RSelenium) |
We’ll launch a selenium server with a Firefox browser in a controlled mode.
It will take quite some time the first time but after it will load in a few seconds.
here is the command:
1 |
rd <- rsDriver(browser = "firefox", port = 4444L) |
At the end of the process, it should open a firefox window like this one
Then we’ll grab the instance to be able to control our browser
1 |
remDr <- rd[["client"]] |
It’s now possible to send action to our browser.
To open a website URL just type
1 |
remDr$navigate("http://www.bbc.com") |
You will notice the robot head icon which means that it is a remote-controlled browser
Here are some useful commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# find a dom element using the class selector and grab inner text remDr$findElement(using = "class", value ="top-story")$getElementText() # find a dom element using a class selector and click on it remDr$findElement(using = "class", value ="top-story")$clickElement() # get h1 textusing a tag selector remDr$findElement(using ="tag", value = "h1")$getElementText() # refresh browser remDr$refresh() |
When you are done with it, don’t forget to
1 2 3 4 5 6 7 8 9 |
# close browser remDr$close() # stop the selenium server rd[["server"]]$stop() # and delete it rm(rd) |
Otherwise, it’s gonna be a mess when you’ll get back on it
Why Selenium is a very interesting solution?
One of the great advantages of using Selenium is that you can alternate automatic and manual actions in the same session.
For example, you can log on somewhere and run an automatic script after pretty easily or… fill in a captcha and run your script.