Back to Repositories

Testing Cookie Management and iframe Integration in nw.js

This test suite validates cookie behavior in nw.js applications, specifically focusing on same-site and cross-site cookie handling within iframes. It ensures proper cookie persistence and validation across page refreshes and iframe contexts.

Test Coverage Overview

The test suite provides comprehensive coverage of cookie management scenarios in nw.js applications. It validates:

  • Same-site cookie persistence
  • Cross-site cookie handling
  • Cookie behavior across iframe boundaries
  • Cookie state preservation during page refreshes

Implementation Analysis

The test implements a Selenium WebDriver approach using Python to automate browser interactions. It leverages Chrome options for nw.js app testing and incorporates iframe navigation patterns. The implementation includes server log validation and cleanup procedures.

Technical Details

Key technical components include:

  • Selenium WebDriver with Chrome
  • Python HTTP server for test endpoints
  • Custom nw.js app configuration
  • File-based logging and verification
  • Platform-specific process management

Best Practices Demonstrated

The test demonstrates several testing best practices:

  • Proper test setup and teardown
  • Resource cleanup
  • Error handling and assertions
  • Cross-platform compatibility considerations
  • Explicit waits for element availability

nwjs/nwJs

test/sanity/issue7305-nwfaketop-cookie/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
try:
    os.remove('port.txt')
except:
    pass

server = subprocess.Popen(['python', '../http-server-node.py', 'server.js'])

while not os.path.exists('port.txt') :
    time.sleep(1)

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 == 'same-site-cookie=foo; cross-site-cookie=bar; no-samesite-cookie=nee')
    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 == 'same-site-cookie=foo; cross-site-cookie=bar; no-samesite-cookie=nee')

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

finally:
    import platform
    if platform.system() == 'Windows':
        subprocess.call(['taskkill', '/F', '/T', '/PID', str(server.pid)])
    else:
        server.terminate()
    driver.quit()