Back to Repositories

Testing Menu Hotkey Toggle Functionality in NW.js

This test suite validates menu hotkey functionality in NW.js applications using Selenium WebDriver and PyAutoGUI for keyboard input simulation. It verifies the enabling and disabling of menu shortcuts through automated browser interactions.

Test Coverage Overview

The test suite provides comprehensive coverage of menu hotkey functionality, focusing on toggle behavior.

Key areas tested include:
  • Enabling menu hotkey functionality
  • Disabling menu hotkey functionality
  • Re-enabling menu hotkey functionality
  • Verification of hotkey response states

Implementation Analysis

The implementation uses a hybrid approach combining Selenium WebDriver for DOM manipulation and PyAutoGUI for system-level keyboard input simulation. The test follows a clear pattern of action-verification cycles, utilizing Chrome WebDriver for browser automation and explicit assertions for result validation.

Framework features leveraged:
  • Selenium implicit waits for element synchronization
  • PyAutoGUI hotkey simulation
  • Chrome WebDriver options configuration

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome driver
  • PyAutoGUI for keyboard input simulation
  • Custom Chrome options for NW.js app loading
  • Environment-configured ChromeDriver path
  • Implicit wait timeouts for element synchronization

Best Practices Demonstrated

The test implements several testing best practices for robust automation.

Notable practices include:
  • Proper test cleanup in finally block
  • Explicit element verification
  • Controlled timing with implicit waits
  • Clear test case separation for enable/disable scenarios
  • Proper exception handling

nwjs/nwJs

test/full/issue2261-disable-menu-hotkey/test.py

            
import time
import os
import pyautogui

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__)))

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
driver.implicitly_wait(2)
time.sleep(1)
try:
    driver.implicitly_wait(10)
    print driver.current_url
    # try enable
    driver.find_element_by_id('enable-item').click()
    pyautogui.hotkey('ctrl', 'alt', 'f', interval=0.1)
    result = driver.find_element_by_id('result_1').get_attribute('innerHTML')
    print result
    assert('success' in result)
    # try disable
    driver.find_element_by_id('disable-item').click()
    pyautogui.hotkey('ctrl', 'alt', 'f', interval=0.1)
    elements = driver.find_elements_by_id('result_2')
    assert(len(elements) == 0)
    # try enable again
    driver.find_element_by_id('enable-item').click()
    pyautogui.hotkey('ctrl', 'alt', 'f', interval=0.1)
    result = driver.find_element_by_id('result_2').get_attribute('innerHTML')
    print result
    assert('success' in result)
finally:
    driver.quit()