Back to Repositories

Testing Node.js Permission Requests in Webview Contexts for NW.js

This test suite validates Node.js permission handling in NW.js webview contexts using Selenium WebDriver. It specifically examines the behavior of request permissions and element interactions within the application window.

Test Coverage Overview

The test ensures proper handling of permission requests in both normal and webview contexts.

Key areas covered include:
  • Permission request handling in standard windows
  • Permission request handling in webview contexts
  • Element visibility and interaction timing
  • Asynchronous request processing validation

Implementation Analysis

The test implements a Selenium WebDriver approach to automate browser interactions and validate permission requests.

It utilizes Chrome options configuration for NW.js testing and implements a polling mechanism to handle asynchronous element loading. The implementation includes timeout handling and element state verification patterns specific to webview contexts.

Technical Details

Testing tools and configuration:
  • Selenium WebDriver with Chrome driver
  • Custom Chrome options for NW.js app testing
  • Implicit wait configuration of 3 seconds
  • Page load strategy set to ‘none’
  • Dynamic timeout handling with 10-second maximum

Best Practices Demonstrated

The test exemplifies robust automation practices through proper resource cleanup and error handling.

Notable practices include:
  • Proper driver cleanup in finally block
  • Explicit timeout handling for async operations
  • Dynamic element state checking
  • Clear assertion conditions for test validation

nwjs/nwJs

test/sanity/issue5730-add-node-in-permissions/test.py

            
import time
import os

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)
#chrome_options.add_experimental_option("windowTypes", ["webview"])

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"])
driver.implicitly_wait(3)
try:
    #print(driver.current_url)
    time.sleep(3)
    ret1 = ''
    ret2 = ''
    timeout = 10
    while timeout > 0:
        try:
            ret2 = driver.find_element_by_id('request2').get_attribute('innerHTML')
            ret1 = driver.find_element_by_id('request1').get_attribute('innerHTML')
            if (ret2 != ''):
                print('show webview delayed value')
                print(ret2)
                print('show request normatl value')
                print(ret1)
                break
        except selenium.common.exceptions.NoSuchElementException:
            pass

        time.sleep(1)
        timeout = timeout - 1
        if timeout <= 0:
            raise Exception('Timeout when waiting for element' + elem_id)

    assert('onRequest' in ret2)
    assert('onRequest' in ret1)

finally:
    driver.quit()