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”,

)

selenium test cases for the headless browser testing using HtmlUnit-

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 ="https://www.facebook.com/?stype=lo&jlou=AffcES-pE7ZOIAnyfQ0ri67sPADuX-tEjV5_ZeX7_JS13sVb-GRn0zhLg-nI_dvycS2rbtf6B8be-BuKlvZ336B-6KC_18qbGk5-6EbergdKJQ&smuh=57639&lh=Ac961TitQaPjB98k"
running(TestServer(port, FakeApplication(additionalConfiguration = inMemoryDatabase())), HTMLUNIT) { browser =>

"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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s