DEV Community

cuongld2
cuongld2

Posted on • Edited on

Get rid of chrome restore bubble popup when automate GUI test using Selenium

Have you ever been annoyed by this restore session bubble in chrome:

Alt Text

This blog post I will show you how to handle that.

Usually when you automate the web app with browser, this popup appear because of driver.quit() and because you are using with user data directory. ( If you're not, this bubble will not appear as in my experience ).

Sometimes we need to use the user data directory, for example we need some specific extensions, or some logged in data.

1.Manually:

To disable the popup manually, in windows, you need to go to user data directory default.

For me, it's like :
C:\Users\your_name\AppData\Local\Chrome\Browser\User Data\Default
In this folder, open file Preferences

You need to modify these values to :

"exit_type": "none",

"exited_cleanly":true,

After save that, the next time it won't happen the next time.

But Chrome browser might override that value, so if you don't want to modify that forever, make sure you set the file as read-only.

2.Automation:

In the case you are doing automation test with selenium, you will need to do some code.

We will work on how to change the text in Preferences file.

def modify_file_as_text(text_file_path, text_to_search, replacement_text):
    with fileinput.FileInput(text_file_path, inplace=True, backup='.bak') as file:
        for line in file:
            print(line.replace(text_to_search, replacement_text), end='')

Enter fullscreen mode Exit fullscreen mode

This function will find the text_file_path, and replace the wanted_text_change with the new one

Currently, I'm using Selenium with pytest so the file setup before open browser will look like this:

@pytest.fixture(scope='session')
@pytest.mark.usefixtures('set_up_before_run_user_browser', 'get_use_data_path')
def browser(get_use_data_path):
    global driver
    global user_data_path
    global block_origin_extension_path
    if driver is None:
        chrome_options = sele_webdriver.ChromeOptions()
        chrome_options.add_argument("--window-size=1920,1080")
        chrome_options.add_argument("--proxy-server='direct://'")
        chrome_options.add_argument("--proxy-bypass-list=*")
        chrome_options.add_argument("--start-maximized")
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--ignore-certificate-errors')
        chrome_options.add_argument("--allow-insecure-localhost")
        chrome_options.add_argument('--disable-application-cache')
        chrome_options.add_argument("--disable-session-crashed-bubble")
        chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
        if get_use_data_path is True or get_use_data_path is None:
            import subprocess
            prog = subprocess.Popen("taskkill /im chrome.exe /f", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            prog.communicate()  # Returns (stdoutdata, stderrdata): stdout and stderr are ignored, here
            set_up_before_run_user_browser()
            chrome_options.add_argument('--user-data-dir=' + user_data_path)
            modify_file_as_text(user_data_path + '\\Default\\Preferences', 'Crashed', 'none')
        driver = webdriver.Chrome(options=chrome_options)
        driver.maximize_window()
        driver.set_page_load_timeout(40)
    yield driver
    driver.quit()

Enter fullscreen mode Exit fullscreen mode

There is a lot in the above but you only need to note 2 things,
First we need to set the user data directory

chrome_options.add_argument('--user-data-dir=' + user_data_path)
Enter fullscreen mode Exit fullscreen mode

Secondly,call the replace text function:

modify_file_as_text(user_data_path + '\\Default\\Preferences', 'Crashed', 'none')
Enter fullscreen mode Exit fullscreen mode

That's it.
I hope this helps.
Thank you guys and gals!

Notes: If you feel this blog help you and want to show the appreciation, feel free to drop by :

This will help me to contributing more valued contents.

Top comments (0)