Using multiple browser instances

Last edited on

Overview

To start multiple instances of Firefox and Chrome each instance must be configured to use a different settings/user data/profile folder. The following example demonstrates how to achieve this by performing these steps:

Python

import os
import os.path
import shutil
import subprocess
import sys

squishserver_port_2_process = {}

def main():
    # Clone Squish settings folder prior to loadUrl() /
    # startBrowser() to ensure that no active session
    # data is being cloned (!):
    create_user_settings_master_clone()



    # Load page in default/initial browser instance:
    startBrowser("https://download.froglogic.com/support/html_test.html")

    # Fetch application context of first browser instance;
    # required to switch back and forth. Also see:
    # https://doc.froglogic.com/squish/6.0/ugs-appcontext.html#ugs-appcontext
    ctx_browser1 = currentApplicationContext()



    # Start second browser instance and fetch application
    # context of the new browser instance; required to
    # switch back and forth.
    #
    # Also see:
    # https://doc.froglogic.com/squish/6.0/ugs-appcontext.html#ugs-appcontext
    #
    # The TCP port provided must not be in use:
    ctx_browser2 = start_new_browser_instance("google-chrome", 9990)

    # Load page in new browser instance:
    startBrowser("www.froglogic.com")



    # Start third browser instance and fetch application
    # context of the new browser instance.
    # The TCP port provided must not be in use:
    ctx_browser2 = start_new_browser_instance("firefox", 9991)

    # Load page in new browser instance:
    startBrowser("www.froglogic.com/secure")

    return

def cleanup():
    # Close all browser instances:
    for ctx in applicationContextList():
        ctx.detach()

    # Ask each separately started squishserver to
    # shut down:
    global squishserver_port_2_process
    for port in squishserver_port_2_process.iterkeys():
        subprocess.Popen(["squishserver", "--port", str(port), "--stop"]).wait()
    snooze(1)

    # Kill each separately started squishserver:
    for process in squishserver_port_2_process.itervalues():
        process.kill()
    snooze(1)

def create_user_settings_master_clone():
    user_settings_path = _squish_user_settings_path()
    user_settings_clone_master = _clone_master_path()
    if os.path.exists(user_settings_clone_master):
        shutil.rmtree(user_settings_clone_master)
    shutil.copytree(user_settings_path, user_settings_clone_master, ignore=shutil.ignore_patterns("workspace-ver-4*", "squishlibrary*"))

def start_new_browser_instance(browser_id, squishserver_port):
    if browser_id not in ["firefox", "google-chrome", "ie"]:
        test.fatal("ERROR: Unknown browser ID: %s" % browser_id)
        return None

    global squishserver_port_2_process
    if squishserver_port in squishserver_port_2_process:
        test.fatal("ERROR: Port for squishserver already used: %s" % squishserver_port)
        return None

    squish_user_settings = _clone_squish_user_settings_from_master_clone(squishserver_port)

    squishserver_process = _start_squishserver(squishserver_port, squish_user_settings)
    squishserver_port_2_process["%s" % squishserver_port] = squishserver_process

    _set_default_browser(browser_id, squishserver_port, squish_user_settings)

    return startApplication("__squish__webhook", "localhost", squishserver_port)

def _set_default_browser(browser_id, squishserver_port, squish_user_settings):
    args = []
    args += ["squishrunner"]
    args += ["--host"]
    args += ["localhost"]
    args += ["--port"]
    args += [str(squishserver_port)]
    args += ["--config"]
    args += ["setDefaultWebBrowser"]
    args += [browser_id]

    env = os.environ.copy()
    env["SQUISH_USER_SETTINGS_DIR"] = squish_user_settings
    subprocess.Popen(args=args, env=env).wait()

def _start_squishserver(squishserver_port, squish_user_settings):
    env = os.environ.copy()
    env["SQUISH_USER_SETTINGS_DIR"] = squish_user_settings

    # Start any squishserver already running on this port:
    args = ["squishserver"]
    args += ["--port"]
    args += [str(squishserver_port)]
    args += ["--stop"]
    subprocess.Popen(args=args, env=env).wait()

    args = ["squishserver"]
    args += ["--verbose"]
    args += ["--port"]
    args += [str(squishserver_port)]
    return subprocess.Popen(args=args, env=env)

def _clone_squish_user_settings_from_master_clone(squishserver_port):
    user_settings_clone_master = _clone_master_path()
    user_settings_clone = "%s_clone_%s" % (_squish_user_settings_path(), squishserver_port)
    if os.path.exists(user_settings_clone):
        shutil.rmtree(user_settings_clone)
    shutil.copytree(user_settings_clone_master, user_settings_clone, ignore=shutil.ignore_patterns("workspace-ver-4*", "squishlibrary*"))
    return user_settings_clone

def _clone_master_path():
    return _squish_user_settings_path() + "_clone_master"

def _squish_user_settings_path():
    if sys.platform == "win32":
        return os.path.join(os.environ["APPDATA"], "froglogic", "Squish")
    else:
        return os.path.join(os.environ["HOME"], ".squish")
test.py