Back to Repositories

Testing Cross-Site Cookie Persistence and Security in NW.js

This test suite validates cross-site cookie persistence and security behavior in NW.js applications through Selenium WebDriver automation. It specifically tests cookie handling between different origins and verifies proper cookie retention across page refreshes.

Test Coverage Overview

The test coverage focuses on cross-site cookie management and persistence verification. Key areas include:

  • Cookie retention across page refreshes
  • Cross-origin cookie access validation
  • Server-side cookie handling verification
  • Cookie security protocol compliance

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Python to automate browser interactions and validate cookie behavior. The implementation leverages:

  • Frame switching for cross-origin testing
  • Dynamic port allocation for test server
  • Template-based HTML generation
  • Server log validation against expected results

Technical Details

Testing infrastructure includes:

  • Python with Selenium WebDriver
  • Custom HTTPS test server
  • Chrome WebDriver configuration
  • Dynamic test content generation
  • Log comparison utilities

Best Practices Demonstrated

The test implementation showcases several quality practices:

  • Proper test cleanup and resource management
  • Dynamic port handling to prevent conflicts
  • Explicit wait strategies for reliability
  • Comprehensive error handling
  • Server-client validation correlation

nwjs/nwJs

test/sanity/issue7305-nwfaketop-cookie-neg/test.py

            
import time
import os
import subprocess
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

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

testdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(testdir)

try:
    os.remove('svrlog.txt')
except:
    pass

port = str(utils.free_port())
server = subprocess.Popen(['python3', 'https-server.py', port])

tpl = open('index.tpl', 'r')
content = tpl.read().replace('{port}', port)
tpl.close()

html = open('index.html', 'w')
html.write(content)
html.close()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
try:
    print(driver.current_url)
    driver.implicitly_wait(10)
    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert(result == 'cross-site-cookie=bar')
    driver.refresh()
    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
    result = driver.find_element_by_id('result').get_attribute('innerHTML')
    print(result)
    assert(result == 'cross-site-cookie=bar')

    f = open('svrlog.txt', 'r')
    svrlog = ''.join(f.readlines())
    f = open('expected.txt', 'r')
    expected = ''.join(f.readlines())
    assert (svrlog == expected)

finally:
    server.terminate()
    driver.quit()