Back to Repositories

Testing Desktop Media Selection Integration in NW.js

This test suite validates the screen.chooseDesktopMedia functionality in NW.js using Selenium WebDriver for automated browser testing. It verifies the proper selection and handling of desktop media sources through Chrome’s implementation.

Test Coverage Overview

The test coverage focuses on validating the screen.chooseDesktopMedia API integration with Chrome’s media selection capabilities. Key functionality includes:

  • Desktop media source selection verification
  • Chrome WebDriver interaction testing
  • Success state validation through DOM elements
  • Proper cleanup and driver termination

Implementation Analysis

The testing approach utilizes Selenium WebDriver with Python to automate Chrome browser interactions. The implementation employs a try-finally pattern to ensure proper cleanup of browser resources, while leveraging Chrome options for NW.js app configuration.

Key patterns include:
  • Chrome WebDriver initialization with custom options
  • Implicit wait handling for element loading
  • DOM element verification using find_element_by_id
  • Assertion-based success validation

Technical Details

Testing tools and configuration:

  • Selenium WebDriver for browser automation
  • Chrome Options for NW.js app configuration
  • Environment variable-based ChromeDriver path
  • Implicit wait timeout of 10 seconds
  • DOM element success validation

Best Practices Demonstrated

The test implementation showcases several testing best practices for browser automation. Notable practices include proper resource cleanup, explicit success criteria validation, and robust error handling.

  • Resource cleanup in finally block
  • Clear success criteria definition
  • Environment-based configuration
  • Defensive element waiting strategy

nwjs/nwJs

test/sanity/screen-choosedekstopmedia/test.py

            
import time
import os

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

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
try:
    driver.implicitly_wait(10)
    print(driver.current_url)
    result = driver.find_element_by_id('result')
    print(result.get_attribute('innerHTML'))
    assert("success" in result.get_attribute('innerHTML'))
finally:
    driver.quit()