Skip to content

Instantly share code, notes, and snippets.

@omaciel
Created May 6, 2012 23:37
Show Gist options
  • Save omaciel/2625008 to your computer and use it in GitHub Desktop.
Save omaciel/2625008 to your computer and use it in GitHub Desktop.
A Selenium WebDriver Singleton class
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# vim: ts=4 sw=4 expandtab ai
from robot.api import logger
from robot.utils import asserts
from selenium import webdriver
class Singleton(object):
instance = None
def __new__(cls, base_url, browser='firefox'):
if cls.instance is None:
i = object.__new__(cls)
cls.instance = i
cls.base_url = base_url
cls.browser = browser
if browser == "firefox":
# Create a new instance of the Firefox driver
cls.driver = webdriver.Firefox()
elif browser == "remote":
# Create a new instance of the Chrome driver
cls.driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.HTMLUNITWITHJS)
else:
# Sorry, we can't help you right now.
asserts.fail("Support for Firefox or Remote only!")
else:
i = cls.instance
return i
@souravjamwal77
Copy link

Hi, could you please explain how does this work? I am trying to get selenium chrome driver in multiple modules but failed on almost every solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment