Perform automatic browser tests with RSelenium
⚠️ THIS IS A WORK IN PROGRESS

What's Selenium?

Selenium is a classic tool for QA and it can help perform automatic checks on a website. This is an intro to how to use it

Why Selenium is an 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.

Let's start

The first step is, as always, to install and load the RSelenium package
1
#install to run once
2
install.packages("RSelenium")
3
library(RSelenium)
Copied!
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 R command:
1
rd <- rsDriver(browser = "firefox", port = 4444L)
Copied!
Could not load image
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"]]
Copied!
It’s now possible to send action to our browser. To open a website URL just type
1
remDr$navigate("http://www.bbc.com")
Copied!
Could not load image
You will notice the robot head icon which means that it is a remote-controlled browser
Here are some useful commands:
1
# find a dom element using the class selector and grab inner text
2
remDr$findElement(using = "class", value ="top-story")$getElementText()
3
4
5
# find a dom element using a class selector and click on it
6
remDr$findElement(using = "class", value ="top-story")$clickElement()
7
8
9
# get h1 textusing a tag selector
10
remDr$findElement(using ="tag", value = "h1")$getElementText()
11
12
13
# refresh browser
14
remDr$refresh()
Copied!
When you are done with it, don’t forget to
1
# close browser
2
remDr$close()
3
4
5
# stop the selenium server
6
rd[["server"]]$stop()
7
8
# and delete it
9
rm(rd)
Copied!
Otherwise, it’s gonna be a mess when you’ll get back on it

How to quickly configure a Selenium scenario?

You can check the documentation to learn all the function to find DOM elements and action but there is a quicker way:
Use a chrome extension like Katalon recorder, record and copy/paste the instructions directly
Last modified 7mo ago