Headless Browser testing in Scala using Selenium Webdriver


Headless browser term is used which have no GUI(graphical user interface). Headless provide the same environment like a web browser to a web page for the testing.These are the program which are run on our machine without showing any GUI. Some of the famous headless browser are HtmlUnit, Phantom js and node js.

Headless browser is used in different different scenario like when we want to deploy our code in build tool like code ship and etc. so  there is no browser in build tool and still we want to run our selenium test cases

first of all we have to add selenium  and scala test dependency in build.sbt

libraryDependencies ++=  Seq(
jdbc,
ws,
cache,
“com.typesafe.slick” %% “slick” % “2.1.0”,
“org.scalatest” %   “scalatest_2.11″ %  “2.2.2” %  “test”,
“org.seleniumhq.selenium” % “selenium-java” % “2.45.0” % “test”,
)
<strong>selenium test cases for the headless browser testing using HtmlUnit-</strong>
package SeleniumTest

import java.util.concurrent.TimeUnit
import org.openqa.selenium.htmlunit.HtmlUnitDriver
import org.scalatest.FlatSpec
import play.api.test.FakeApplication
import play.api.test.Helpers.HTMLUNIT
import play.api.test.Helpers.inMemoryDatabase
import play.api.test.Helpers.running
import play.api.test.TestServer
import setup.Testsetup
import com.gargoylesoftware.htmlunit.javascript.host.css.BrowserConfiguration
import com.gargoylesoftware.htmlunit.BrowserVersion
class login extends FlatSpec  {
val port = 9000
val baseUrl =”<a href="https://www.facebook.com/?stype=lo&amp;jlou=AffcES-pE7ZOIAnyfQ0ri67sPADuX-tEjV5_ZeX7_JS13sVb-GRn0zhLg-nI_dvycS2rbtf6B8be-BuKlvZ336B-6KC_18qbGk5-6EbergdKJQ&amp;smuh=57639&amp;lh=Ac961TitQaPjB98k&amp;#8221" rel="nofollow">https://www.facebook.com/?stype=lo&amp;jlou=AffcES-pE7ZOIAnyfQ0ri67sPADuX-tEjV5_ZeX7_JS13sVb-GRn0zhLg-nI_dvycS2rbtf6B8be-BuKlvZ336B-6KC_18qbGk5-6EbergdKJQ&amp;smuh=57639&amp;lh=Ac961TitQaPjB98k&amp;#8221</a>;
running(TestServer(port, FakeApplication(additionalConfiguration = inMemoryDatabase())), HTMLUNIT) { browser =&gt;
“Application” should “for facebook login” in {
val driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_24);
driver.setJavascriptEnabled(true);
driver.manage().window().maximize()
driver.get(baseUrl)
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
driver.findElementById(“email”).sendKeys(“write.atfconnect1@gmail.com”)
driver.findElementById(“pass”).sendKeys(“writeatfconnect1″)
driver.findElementById(“loginbutton”).click()
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS)
driver.findElementByCssSelector(“BODY”).getText().contains(“Manoj Mathpal”)
}}}

Run the  Headless selenium test cases using HtmlUnit-

headless

Advertisements
This entry was posted in Scala and tagged , , , , . Bookmark the permalink.