Back to Repositories

Testing Remote Screen Capture Integration in NW.js

This test suite validates screen capture functionality for remote content in NW.js applications, specifically addressing issue #4579. It tests the integration between NW.js desktop capabilities and web content served through a local HTTP server.

Test Coverage Overview

The test suite covers remote content capture functionality in NW.js applications.

Key areas tested include:
  • Remote content loading through node-remote permissions
  • Screen capture API integration
  • Local HTTP server interaction
  • Cross-platform compatibility (excluding Linux)

Implementation Analysis

The test implements a Selenium WebDriver approach to automate UI interactions and validate screen capture functionality. It utilizes Python’s subprocess management to handle a local HTTP server and Chrome WebDriver for browser automation.

Key patterns include:
  • Dynamic port allocation for test server
  • Package.json manifest generation
  • Chrome WebDriver configuration with NW.js specifics
  • Automated UI interaction and result verification

Technical Details

Testing tools and components:
  • Selenium WebDriver with Chrome options
  • Python HTTP server for content serving
  • Chrome Driver for browser automation
  • Custom nw_util helper functions
Configuration includes:
  • Dynamic port assignment
  • node-remote permissions
  • Chrome WebDriver logging
  • Platform-specific execution paths

Best Practices Demonstrated

The test demonstrates robust automation practices with proper resource management and error handling.

Notable practices include:
  • Proper test cleanup and resource termination
  • Platform-specific test skipping
  • Dynamic configuration management
  • Explicit wait conditions for UI elements
  • Clear success criteria validation

nwjs/nwJs

test/sanity/issue4579-capture-remote/test.py

            
import time
import os
import subprocess
import platform
import sys

if platform.system() == 'Linux':
    print('Skipped for Linux platform')
    sys.exit(0)


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)

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

manifest = open('package.json', 'w')
manifest.write('''
{
  "name":"issue4579-capture-remote",
  "node-remote":"<all_urls>",
  "main":"http://localhost:%s/index.html"
}
''' % (port))
manifest.close()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(2)
try:
    print(driver.current_url)
    driver.find_element_by_id('start-share').click()
    result = driver.find_element_by_id('result')
    print(result.get_attribute('innerHTML'))
    assert("success" in result.get_attribute('innerHTML'))
finally:
    server.terminate()
    driver.quit()