RSelenium, Otomasi Berselancar dengan R [1]

Anda membutuhkan perangkat yang bisa mengumpulkan data layaknya robot? Anda membutuhkan perangkat pelacak kesalahan pada web Anda? Anda pernah mendengar Selenium?

Selenium, Apa Itu?

Selenium automates browsers. That’s it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.

Yup, itu penjelasan sekilas di webnya. Intinya perangkat ini dibuat sebagai alat test terhadap aplikasi web yang dikembangkan, namun penggunaannya tidak terbatas pada itu.

Selenium di Docker

Selamat bagi yang terbiasa dengan Docker. Pengembang Selenium telah membuat image Docker yang bisa langsung digunakan melalui laman Docker Hub. Silakan gunakan dengan semestinya.

Dalam hal ini kami menggunakan image standalone-chrome. Silakan pull dengan perintah docker pull selenium/standalone-chrome.

Setelah selesai, silakan buat container dengan image tersebut.

yht@debian:~$ docker run -it –rm -p 4444:4444 selenium/standalone-chrome
09:09:11.867 INFO – Selenium build info: version: ‘3.4.0’, revision: ‘unknown’
09:09:11.868 INFO – Launching a standalone Selenium Server
2017-04-24 09:09:11.903:INFO::main: Logging initialized @415ms to org.seleniumhq.jetty9.util.log.StdErrLog
09:09:11.995 INFO – Driver provider org.openqa.selenium.ie.InternetExplorerDriver registration is skipped:
registration capabilities Capabilities [{ensureCleanSession=true, browserName=internet explorer, version=, platform=WINDOWS}] does not match the current platform LINUX
09:09:11.996 INFO – Driver provider org.openqa.selenium.edge.EdgeDriver registration is skipped:
registration capabilities Capabilities [{browserName=MicrosoftEdge, version=, platform=WINDOWS}] does not match the current platform LINUX
09:09:11.997 INFO – Driver class not found: com.opera.core.systems.OperaDriver
09:09:11.997 INFO – Driver provider com.opera.core.systems.OperaDriver registration is skipped:
Unable to create new instances on this machine.
09:09:11.997 INFO – Driver class not found: com.opera.core.systems.OperaDriver
09:09:11.998 INFO – Driver provider com.opera.core.systems.OperaDriver is not registered
09:09:11.999 INFO – Driver provider org.openqa.selenium.safari.SafariDriver registration is skipped:
registration capabilities Capabilities [{browserName=safari, version=, platform=MAC}] does not match the current platform LINUX
2017-04-24 09:09:12.072:INFO:osjs.Server:main: jetty-9.4.3.v20170317
2017-04-24 09:09:12.135:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@14acaea5{/,null,AVAILABLE}
2017-04-24 09:09:12.162:INFO:osjs.AbstractConnector:main: Started ServerConnector@754ba872{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2017-04-24 09:09:12.163:INFO:osjs.Server:main: Started @675ms
09:09:12.163 INFO – Selenium Server is up and running

Dengan ini Selenium telah berjalan dan siap menerima perintah.

RSelenium, Dukungan Selenium untuk R

Nah sekarang saatnya menggunakannya dalam platform. Tapi sebelumnya pastikan RSelenium telah terpasang atau pasang dengan perintah install.packages("RSelenium").

> require(RSelenium)
> rD <- remoteDriver(remoteServerAddr=”localhost”,
port=4444, browserName=”chrome”)

> rD$open()
[1] “Connecting to remote server”
$applicationCacheEnabled
[1] FALSE

$rotatable
[1] FALSE

$mobileEmulationEnabled
[1] FALSE

$networkConnectionEnabled
[1] TRUE

$chrome
$chrome$chromedriverVersion
[1] “2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5)”

$chrome$userDataDir
[1] “/tmp/.org.chromium.Chromium.Ib67gD”

$takesHeapSnapshot
[1] TRUE

$pageLoadStrategy
[1] “normal”

$unhandledPromptBehavior
[1] “”

$databaseEnabled
[1] FALSE

$handlesAlerts
[1] TRUE

$hasTouchScreen
[1] TRUE

$version
[1] “58.0.3029.81”

$platform
[1] “LINUX”

$browserConnectionEnabled
[1] FALSE

$nativeEvents
[1] TRUE

$acceptSslCerts
[1] TRUE

$webdriver.remote.sessionid
[1] “62b8da72-b23c-470d-ac43-cd20e5f55564”

$locationContextEnabled
[1] TRUE

$webStorageEnabled
[1] TRUE

$browserName
[1] “chrome”

$takesScreenshot
[1] TRUE

$javascriptEnabled
[1] TRUE

$cssSelectorsEnabled
[1] TRUE

$unexpectedAlertBehaviour
[1] “”

$id
[1] “62b8da72-b23c-470d-ac43-cd20e5f55564”

> rD$navigate(“http://www.b2b.web.id”)
> rD$getPageSource()
[[1]]
[1] “<!DOCTYPE html><!–[if IE 7]>\n<html class=\”ie ie7\” lang=\”en-US\”>\n<![endif]–><!–[if IE 8]>\n<html class=\”ie ie8\” lang=\”en-US\”>\n<![endif]–><!–[if !(IE 7) & !(IE 8)]><!–><html xmlns=\”http://www.w3.org/1999/xhtml\” lang=\”en-US\”><!–<![endif]–><head>\n\t<meta charset=\”UTF-8\” />\n\t<meta name=\”viewport\” content=\”width=device-width\” />\n\t<title>B2B</title>\n\t<link rel=\”profile\” href=\”http://gmpg.org/xfn/11\” />\n\t<link rel=\”pingback\” href=\”http://www.b2b.web.id/xmlrpc.php\” />\n\t<!–[if lt IE 9]>\n\t<script src=\”http://www.b2b.web.id/wp-content/themes/twentyfourteen/js/html5.js\”></script>\n\t<![endif]–>\n\t\n<!– All in One SEO Pack 2.3.9.2 by Michael Torbert of Semper Fi Web Design[297,329] –>\n<meta name=\”description\” content=\”B2B\” />\n\n<meta name=\”keywords\” content=\”B2B\” />\n<link rel=\”next\” href=\”http://www.b2b.web.id/page/2/\” />\n\n<link rel=\”canonical\” href=\”http://www.b2b.web.id/\” />\n<!– /all in one seo pack –>\n<link rel… <truncated>

> rD$closeall()
[[1]]
NULL

Nah, selamat mencoba membangun infrastruktur terlebih dahulu. Lain waktu akan kita akan membahas penggunaannya.

Leave a Reply

Your email address will not be published. Required fields are marked *