Back to Repositories

Testing Page Capture and Image Comparison Implementation in NW.js

This test suite validates page capture functionality in NW.js using Selenium WebDriver and Python. It specifically tests the ability to capture and compare PNG and JPG image outputs within a popup window context.

Test Coverage Overview

The test suite focuses on verifying image capture capabilities in NW.js applications.

Key areas covered include:
  • Window switching and popup handling
  • Image element detection and validation
  • Size comparison between PNG and JPG formats
  • Browser window management and cleanup

Implementation Analysis

The implementation utilizes Selenium WebDriver with Chrome options for automated testing. The approach combines Python’s testing capabilities with Selenium’s browser automation features to validate image rendering.

Key patterns include:
  • Chrome WebDriver configuration with custom options
  • Window handle management
  • Element size assertion checks
  • Resource cleanup through try-finally blocks

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome
  • Custom Chrome options for NW.js app context
  • Page load strategy configuration
  • Environment-based ChromeDriver path
  • Verbose logging setup
  • Custom utility functions from nw_util

Best Practices Demonstrated

The test implementation showcases several testing best practices for UI automation.

Notable practices include:
  • Proper resource cleanup with try-finally blocks
  • Explicit wait conditions for window switching
  • Comparative assertions for image validation
  • Modular test utility integration
  • Detailed logging configuration

nwjs/nwJs

test/sanity/capture_page/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
chrome_options = Options()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))

capabilities = {"pageLoadStrategy": "none"}
driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options, desired_capabilities = capabilities, service_log_path="log", service_args=["--verbose"])

try:
    wait_switch_window_url(driver, 'popup.html')
    #driver.switch_to.window(driver.window_handles[-1])
    img = driver.find_element_by_id('png')
    assert(img.size['width'] > 50 and img.size['height'] > 50)
    img2 = driver.find_element_by_id('jpg')
    assert(img.size['height'] == img2.size['height'] and img.size['width'] == img2.size['width'])
finally:
    driver.quit()