Making all tests use the same XVFB display

60 views
Skip to first unread message

Jorge Delgado

unread,
Jun 4, 2017, 12:26:09 AM6/4/17
to Selenium Users

I have about 20 selenium tests that I run frequently on a headless Ubuntu 14.04 VM. Below is an example:

import time
import random
import requests
from selenium import webdriver
import datetime
import MySQLdb as mdb
import sys
import socks
from pyvirtualdisplay import Display

if (sys.platform != "win32"):
    print("Starting display.")
    display = Display(visible=0, size=(800, 600))
    display.start()

ChromeOptions = webdriver.ChromeOptions()
ChromeOptions.add_argument('window-size=1200x600')
Prefs = {"profile.managed_default_content_settings.images": 2}
ChromeOptions.add_experimental_option("prefs", Prefs)

if (sys.platform != "win32"):
    print("Unix System being used, proceeding accordingly.")
    ChromeOptions.binary_location = '/usr/bin/google-chrome-unstable'
    ChromeOptions.add_argument("--disable-extensions")

global driver
driver = webdriver.Chrome(chrome_options=ChromeOptions)
driver.set_page_load_timeout(45)

driver.get("https://google.com")
print(driver.title)

As you can see, if the platform is not win32, it will create a display and start it:

if (sys.platform != "win32"):
    print("Starting display.")
    display = Display(visible=0, size=(800, 600))
    display.start()

Every single one of the 20 test suites creates a display, and starts it. Is there a way to make it so ALL of them connect to the same display? Thus, eliminating a ton if CPU/RAM usage as I won't have 20 instances of XVFB running? If so, can you provide a working example in python and any necessary unix commands needed to run it?

thi...@thoughtworks.com

unread,
Jun 5, 2017, 1:49:35 PM6/5/17
to Selenium Users
Hi,why are you setting XVFB inside your selenium script?
Instead,I suggest you to have it as a shell command or any gradle or maven task to be executed before you run your tests.
Reply all
Reply to author
Forward
0 new messages