Back to Repositories

Testing Cookie Persistence During DevTools Operations in NW.js

This test suite validates cookie persistence behavior in NW.js applications when DevTools are opened and closed. It ensures cookies remain intact during DevTools debugging sessions and verifies proper state management.

Test Coverage Overview

The test suite comprehensively checks cookie handling across different application states, focusing on persistence during DevTools operations.

  • Tests cookie initialization and setting
  • Verifies cookie retention after DevTools open/close
  • Validates window handle management
  • Ensures consistent cookie access across contexts

Implementation Analysis

Implements Selenium WebDriver for automated browser control and testing, utilizing Chrome options for NW.js-specific configuration. The test employs a systematic approach with sequential cookie verification steps and explicit window handle management.

  • Uses Python with Selenium WebDriver
  • Implements custom utility functions for NW.js testing
  • Manages dynamic port allocation for test isolation

Technical Details

  • Selenium WebDriver with Chrome options
  • Dynamic package.json configuration
  • Custom wait and switch utilities
  • Window handle management functions
  • Automated cleanup procedures

Best Practices Demonstrated

The test demonstrates robust error handling and resource cleanup through try-finally blocks, ensuring proper test isolation. It implements explicit waits for better reliability and uses modular helper functions for common operations.

  • Proper test cleanup and resource management
  • Explicit wait patterns for reliability
  • Modular test helper functions
  • Clear assertion messages

nwjs/nwJs

test/sanity/cookie-lost-devtools-close/test.py

            
import time
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common import utils

test_dir = os.path.dirname(os.path.abspath(__file__))
chrome_options = Options()
chrome_options.add_argument("nwapp=" + test_dir)

port = str(utils.free_port())

pkgjson = '''
{
  "name": "cookie-lost-devtools-close",
  "main": "index.html",
  "bg-script": "bg.js",
  "port": "%s"
}
''' % port

with open(os.path.join(test_dir, 'package.json'), 'w') as bg:
  bg.write(pkgjson)

idx = 1
def click_expect(expected):
    global idx
    driver.find_element_by_id('get-cookie').click()
    result = wait_for_element_id(driver, 'result-%s' % idx)
    idx += 1
    print(result)
    assert(expected in result)

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
driver.implicitly_wait(2)
try:
    switch_to_app(driver)
    print(driver.current_url)
    click_expect('undefined')
    click_expect('sid=1')
    driver.find_element_by_id('open-devtools').click()
    print('wait for devtools open')
    wait_window_handles(driver, 2)
    click_expect('sid=1')
    print('close devtools')
    switch_to_devtools(driver)
    driver.close()
    driver.switch_to.window(driver.window_handles[0])
    wait_window_handles(driver, 1)
    print('devtools closed')
    click_expect('sid=1')
    click_expect('sid=1')
finally:
    #time.sleep(50)
    driver.quit()