Back to Repositories

Testing AdSense Frame Communication Workflow in NW.js

This test suite validates Google AdSense integration and iframe accessibility in NW.js applications. It focuses on verifying proper frame communication and top-level window access permissions, which are critical for ad implementation functionality.

Test Coverage Overview

The test suite provides comprehensive coverage of iframe-parent window interactions and AdSense compatibility scenarios.

  • Validates frame button click interactions
  • Tests iframe content accessibility
  • Verifies top-level window access permissions
  • Ensures proper communication between iframe and parent context

Implementation Analysis

The implementation uses Selenium WebDriver for automated browser testing, with Python as the scripting language. The test leverages Chrome options and custom server configuration to create a controlled testing environment.

  • Uses dynamic port allocation for test server
  • Implements template-based HTML generation
  • Employs explicit frame switching mechanisms
  • Handles cleanup through finally blocks

Technical Details

  • Selenium WebDriver for browser automation
  • Python HTTP server for local testing
  • Chrome WebDriver for browser control
  • Template-based test page generation
  • Dynamic port allocation for test isolation
  • Custom Chrome options for NW.js integration

Best Practices Demonstrated

The test implements several testing best practices for reliable browser automation and iframe testing.

  • Proper resource cleanup in finally blocks
  • Dynamic test environment configuration
  • Explicit wait strategies for element interaction
  • Isolated test server implementation
  • Clear assertion messages for debugging

nwjs/nwJs

test/sanity/issue6099-AdSense-trouble/test.py

            
import time
import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

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

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

os.chdir(testdir)

port = str(utils.free_port())
server = subprocess.Popen(['python3', 'http-server.py', port])

tpl = open('index.tpl', 'r')
content = tpl.read().replace('{port}', port)
tpl.close()

html = open('index.html', 'w')
html.write(content)
html.close()

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
driver.implicitly_wait(2)
try:
    print(driver.current_url)
    print('Click frame button')
    driver.find_element_by_id('refresh').click()
    output = driver.find_element_by_id('frameInfo').get_attribute('innerHTML')
    assert('Iframe top accessible: true' in output)
    print('Switch to iframe')
    driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
    print('Click iframe button')
    driver.find_element_by_id('refresh').click()
    result = driver.find_element_by_id('iframeInfo').get_attribute('innerHTML')
    assert('Top accessible: true' in result)
    print('document.querySelector("iframe").contentWindow.top is works')
finally:
    driver.quit()
    server.terminate()