Running Selenium Functional Test Scripts in Headless Mode.

In this post, I want to share How to run the Selenium scripts in Headless mode (With out opening the Browser).

First of all Why we need to run the selenium scripts in Headless mode. Its required when the scripts need to be run at console (nonUI) based operating system like Linux or Scripts integrated to Continuous Integration system which relies on console (nonUI) environment. The advantage of using Headless mode is

  • scripts will be executed and run with out opening browser (we cannot open browser in console OS).
  • the test script execution is a bit faster in Headless mode.

Let`s see how to execute the below test script in Headless mode in python programming languages.

Note:

-the example script which I am using here will perform a basic operation like opening browser and Google home page.

-and the browser used was firefox and google chrome will be covered in later posts.

Python:

Step 1: Save the Below as Google.py

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class Test(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = “https://www.google.com.sg
self.verificationErrors = []
self.accept_next_alert = true

def test_google(self):
driver = self.driver

def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)

if __name__ == “__main__”:
unittest.main()

Step 2:

In order to run the above script in firefox browser and  Linux OS we need to install few python libraries. Please find the steps below.

Prerequisite: Python 2.5 and above

  1. Install the Py virtual display module from http://pypi.python.org/pypi/PyVirtualDisplay.
  2. Download the zip file and extract it. Run the  setup.py file by using the command “python setup.py install”
  3. This will install Py virtual display module.

Note: there will be some dependency libraries like xvfb and others. Please install the libraries.

Now modify the script like below :

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
from pyvirtualdisplay import Display

class Test(unittest.TestCase):
def setUp(self):
self.display = Display(visible=0, size=(800, 600))
self.display.start()
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = “https://www.google.com.sg
self.verificationErrors = []
self.accept_next_alert = true

def test_google(self):
driver = self.driver

def tearDown(self):
self.driver.quit()
self.display.stop()
self.assertEqual([], self.verificationErrors)

if __name__ == “__main__”:
unittest.main()

Step 3: Save and run the file.

About these ads

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