Back to Repositories

Validating Remote iframe Node.js Integration in NW.js

This test suite validates the behavior of remote iframes within NW.js applications, specifically focusing on Node.js integration and security boundaries. It uses Selenium WebDriver to automate browser interactions and verify proper isolation of Node.js context between main window and remote iframe content.

Test Coverage Overview

The test suite provides comprehensive coverage of iframe isolation and Node.js context verification.

Key areas tested include:
  • Node.js availability in main window context
  • Node.js isolation in remote iframe content
  • Cross-frame communication and security boundaries
  • Dynamic HTML generation and injection

Implementation Analysis

The implementation utilizes Selenium WebDriver with Python to automate browser testing. It creates a local HTTP server to serve remote content, generates test HTML dynamically, and validates Node.js context across different frames.

Notable patterns include:
  • Dynamic port allocation for test server
  • Frame switching and element detection
  • Timeout-based polling for async operations

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome
  • Python HTTP server for remote content
  • Custom NW.js utility functions
  • Chrome Options for NW.js app configuration
  • Element wait strategies with timeout handling

Best Practices Demonstrated

The test implementation showcases robust testing practices for web applications.

Key practices include:
  • Proper resource cleanup and driver management
  • Dynamic test environment setup and teardown
  • Explicit wait strategies for async operations
  • Error handling and timeout management
  • Isolation of test dependencies

nwjs/nwJs

test/sanity/iframe-remote/test.py

            
import time
import os
import subprocess
import sys

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common import utils
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

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)

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

html = open('index.html', 'w')
html.write('''
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf8">
    <title>iframe-remote negative test</title>
  </head>
  <body>
    <iframe src="http://localhost:%s/test.html"></iframe>
    <script>
      document.write('<h1 id="res1">Node is ' + (typeof nw === 'undefined' ? 'DISABLED': 'ENABLED') + '</h1>');
    </script>
  </body>
</html>
''' % (port))
    
html.close()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(5)
try:
    print(driver.current_url)
    result = driver.find_element_by_id('res1').get_attribute('innerHTML')
    print(result)
    assert("ENABLED" in result)
    timeout = 10
    ret = ''
    elem_id = 'res2'
    while timeout > 0:
        try:
            driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
            ret = driver.find_element_by_id(elem_id).get_attribute('innerHTML')
            break
        except selenium.common.exceptions.NoSuchElementException:
            pass
        time.sleep(1)
        timeout = timeout - 1
        if timeout <= 0:
             raise Exception('Timeout when waiting for element' + elem_id)
    print(ret)
    assert("ENABLED" in ret)
finally:
    server.terminate()
    driver.quit()