Back to Repositories

Testing Window.getPrinters Array Response Implementation in NW.js

This test suite validates the window.getPrinters functionality in NW.js by automating browser interactions and verifying printer array responses. It demonstrates integration between Selenium WebDriver and NW.js for testing desktop printing capabilities.

Test Coverage Overview

The test provides comprehensive coverage of the window.getPrinters API functionality in NW.js, ensuring it returns the expected array format of printer objects.

  • Validates printer list retrieval through DevTools console
  • Verifies array response structure
  • Handles both populated and empty printer lists
  • Tests integration between NW.js window API and system printer services

Implementation Analysis

The implementation utilizes Selenium WebDriver with Python to automate browser interactions and validate printer API responses. The test leverages Chrome DevTools integration to execute and verify the getPrinters functionality.

  • Uses Chrome options for NW.js app configuration
  • Implements window handle management for DevTools access
  • Employs console message parsing for output validation

Technical Details

  • Python with Selenium WebDriver for test automation
  • Chrome DevTools Protocol integration
  • Custom nw_util helper functions for NW.js specific operations
  • Environment-configured ChromeDriver path
  • Implicit wait timing for UI synchronization
  • Class-based element selection for console output verification

Best Practices Demonstrated

The test exemplifies robust automation practices with proper resource cleanup and error handling. It demonstrates effective use of wait strategies and element location techniques.

  • Proper test cleanup with driver quit in finally block
  • Defensive coding with multiple output verification approaches
  • Clear logging of test progression steps
  • Modular utility function usage

nwjs/nwJs

test/sanity/issue6663-window-getPrinters-return-array/test.py

            
import os
import sys

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from nw_util import *

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

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

driver = webdriver.Chrome(executable_path=os.environ["CHROMEDRIVER"], chrome_options=chrome_options, service_log_path="log", service_args=["--verbose"])
driver.implicitly_wait(2)
try:
    switch_to_app(driver)
    print(driver.current_url)
    print("wait for devtools open")
    wait_window_handles(driver, 2)
    print(driver.window_handles)
    print("switch to devtools")
    switch_to_devtools(driver)
    print("click Console panel")
    devtools_click_tab(driver, "console")
    print("check if win.getPrinters receive an array of JSON objects")
    elems = driver.find_elements_by_class_name("console-message-text")
    output = ""
    if len(elems) > 1:
        for i in range(len(elems)):
            if "Array" in elems[i].get_attribute("innerHTML"):
                output = elems[i].get_attribute("innerHTML")
                break
    else:
        output = elems[0].get_attribute("innerHTML")
    print(output)
    assert("Array" in output or '[]' in output)
finally:
    driver.quit()