Back to Repositories

Testing Keyboard Shortcut Registration and Handling in NW.js

This test suite validates keyboard shortcut functionality in NW.js applications using Selenium WebDriver and PyAutoGUI. It implements comprehensive testing of single-key and multi-key combinations, with focus on registration and unregistration of keyboard shortcuts.

Test Coverage Overview

The test suite provides extensive coverage of keyboard shortcut handling in NW.js applications.

Key areas tested include:
  • Single key shortcuts (a, b, c)
  • Multi-key combinations with modifier keys (Ctrl, Shift, Alt)
  • Special key combinations (escape, backtick)
  • Registration and unregistration of shortcuts

Implementation Analysis

The implementation uses a hybrid approach combining Selenium WebDriver for browser automation and PyAutoGUI for native keyboard input simulation. The test structure follows a modular pattern with helper functions for shortcut registration and verification, allowing for consistent testing across different key combinations.

The framework leverages Chrome WebDriver with custom NW.js options for application loading and DOM manipulation.

Technical Details

Testing tools and libraries:
  • Selenium WebDriver with Chrome
  • PyAutoGUI for keyboard simulation
  • Custom Chrome options for NW.js integration
Configuration includes:
  • Dynamic path resolution for test directory
  • Implicit wait timeouts
  • Custom DOM assertion utilities

Best Practices Demonstrated

The test implementation showcases several testing best practices including proper test isolation, cleanup, and error handling.

Notable practices:
  • Consistent test pattern for registration/unregistration
  • Error handling with try/finally blocks
  • Modular test helper functions
  • Clear separation of test setup and execution

nwjs/nwJs

test/full/shortcut-normal/test.py

            
import time
import os
import pyautogui

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))

def assert_dom(id, expect):
    elem = driver.find_element_by_id(id)
    print elem.get_attribute('innerHTML')
    assert(expect in elem.get_attribute('innerHTML'))

def test_reg(keys, pykeys=None, expect="success"):
    key = '+'.join(keys)
    id = 'reg-' + '-'.join(keys)
    reg_script = 'reg("%s", "%s")' % (id, key)
    print reg_script
    driver.execute_script(reg_script)
    if pykeys is not None:
        pyautogui.hotkey(*pykeys)
    if expect is not None:
        assert_dom(id, expect)

def test_unreg(keys, expect="success"):
    key = '+'.join(keys)
    id = 'unreg-' + '-'.join(keys)
    unreg_script = 'unreg("%s", "%s")' % (id, key)
    print unreg_script
    driver.execute_script(unreg_script)
    assert_dom(id, expect)

def test_reg_unreg(keys, pykeys):
    test_reg(keys, pykeys)
    test_unreg(keys)

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
try:
    print driver.current_url
    time.sleep(1)
    driver.implicitly_wait(10)
    test_reg_unreg(['a'], ['a'])
    test_reg_unreg(['keyb'], ['b'])
    test_reg_unreg(['KeyC'], ['c'])
    test_reg_unreg(['ctrl', 'b'], ['ctrl', 'b'])
    test_reg_unreg(['ctrl','shift','b'], ['ctrl','shift','b'])
    test_reg_unreg(['ctrl','shift','alt','b'], ['ctrl','shift','alt','b'])
    test_reg_unreg(['ctrl','shift','alt','`'], ['ctrl','shift','alt','`'])
    test_reg_unreg(['ctrl','shift','alt','escape'], ['ctrl','shift','alt','escape'])
finally:
    driver.quit()