Path: blob/trunk/py/test/selenium/webdriver/common/bidi_integration_tests.py
8671 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617import pytest1819from selenium.webdriver.common.by import By20from selenium.webdriver.common.window import WindowTypes21from selenium.webdriver.support.ui import WebDriverWait222324class TestBidiNetworkWithCookies:25"""Test integration of network and storage modules."""2627@pytest.fixture(autouse=True)28def setup(self, driver, pages):29"""Setup for each test in this class."""30pages.load("blank.html")31yield32# Cleanup: delete all cookies to prevent bleed-through33driver.delete_all_cookies()3435def test_cookies_interaction(self, driver, pages):36"""Test that cookies work with network operations."""37pages.load("blank.html")3839# Set a cookie40driver.add_cookie({"name": "test_cookie", "value": "test_value"})4142# Verify cookie is set43cookies = driver.get_cookies()44assert len(cookies) > 045assert any(c.get("name") == "test_cookie" for c in cookies)4647def test_cookie_modification(self, driver, pages):48"""Test that modifying cookies works properly."""49pages.load("blank.html")5051# Add first cookie52driver.add_cookie({"name": "cookie1", "value": "value1"})5354cookies_before = driver.get_cookies()55initial_count = len(cookies_before)5657# Add second cookie58driver.add_cookie({"name": "cookie2", "value": "value2"})5960cookies_after = driver.get_cookies()61assert len(cookies_after) > initial_count626364class TestBidiScriptWithNavigation:65"""Test integration of script execution and navigation."""6667@pytest.fixture(autouse=True)68def setup(self, driver, pages):69"""Setup for each test in this class."""70driver.delete_all_cookies()71pages.load("blank.html")72yield73# Cleanup: delete all cookies to prevent bleed-through74driver.delete_all_cookies()7576def test_script_execution_after_navigation(self, driver, pages):77"""Test script execution after page navigation."""78# First page79pages.load("blank.html")80driver.execute_script("window.page1_loaded = true;")8182# Navigate to different page83pages.load("blank.html")8485# Previous page variable should not exist86result = driver.execute_script("return window.page1_loaded;")87assert result is None8889# New variable should work90driver.execute_script("window.page2_loaded = true;")91result = driver.execute_script("return window.page2_loaded;")92assert result is True9394def test_global_variable_lifecycle(self, driver, pages):95"""Test global variable lifecycle across operations."""96pages.load("blank.html")9798# Set a global variable99driver.execute_script("window.test_var = {data: 'value'};")100101# Verify it exists102result = driver.execute_script("return window.test_var.data;")103assert result == "value"104105# Navigate away106driver.get("about:blank")107108# Variable should not exist anymore109result = driver.execute_script("return typeof window.test_var;")110assert result == "undefined"111112113class TestBidiEmulationWithNavigation:114"""Test integration of emulation and navigation."""115116@pytest.fixture(autouse=True)117def setup(self, driver, pages):118"""Setup for each test in this class."""119pages.load("blank.html")120yield121# Cleanup: delete all cookies to prevent bleed-through122driver.delete_all_cookies()123124def test_basic_navigation(self, driver, pages):125"""Test basic navigation."""126pages.load("blank.html")127assert driver.find_element(By.TAG_NAME, "body") is not None128129130class TestBidiContextManagement:131"""Test integration of context creation and management."""132133def test_create_and_close_context(self, driver):134"""Test creating and closing a user context."""135new_context = driver.browser.create_user_context()136137try:138assert new_context is not None139finally:140driver.browser.remove_user_context(new_context)141142def test_multiple_contexts_creation(self, driver):143"""Test creating multiple contexts."""144context1 = driver.browser.create_user_context()145context2 = driver.browser.create_user_context()146147try:148assert context1 is not None149assert context2 is not None150assert context1 != context2151finally:152driver.browser.remove_user_context(context1)153driver.browser.remove_user_context(context2)154155156class TestBidiEventHandlers:157"""Test integration of event handlers."""158159@pytest.fixture(autouse=True)160def setup(self, driver, pages):161"""Setup for each test in this class."""162pages.load("blank.html")163yield164# Cleanup: delete all cookies to prevent bleed-through165driver.delete_all_cookies()166167def test_multiple_console_handlers(self, driver):168"""Test multiple console message handlers."""169messages1 = []170messages2 = []171172handler1 = driver.script.add_console_message_handler(messages1.append)173handler2 = driver.script.add_console_message_handler(messages2.append)174175try:176driver.execute_script("console.log('test message');")177WebDriverWait(driver, 5).until(lambda _: len(messages1) > 0 and len(messages2) > 0)178179assert len(messages1) > 0180assert len(messages2) > 0181finally:182driver.script.remove_console_message_handler(handler1)183driver.script.remove_console_message_handler(handler2)184185186class TestBidiStorageOperations:187"""Test storage operations."""188189@pytest.fixture(autouse=True)190def setup(self, driver, pages):191"""Setup for each test in this class."""192driver.delete_all_cookies()193pages.load("blank.html")194yield195# Cleanup: delete all cookies to prevent bleed-through196driver.delete_all_cookies()197198def test_cookie_operations(self, driver, pages):199"""Test basic cookie operations."""200pages.load("blank.html")201202# Set cookie203driver.add_cookie({"name": "test", "value": "data"})204205# Get cookies206cookies = driver.get_cookies()207assert any(c.get("name") == "test" for c in cookies)208209# Delete cookie210driver.delete_cookie("test")211212# Verify deletion213cookies_after = driver.get_cookies()214assert not any(c.get("name") == "test" for c in cookies_after)215216def test_cookie_attributes(self, driver, pages):217"""Test cookie with various attributes."""218pages.load("blank.html")219220driver.add_cookie({"name": "attr_cookie", "value": "test_value", "path": "/", "secure": False})221222cookies = driver.get_cookies()223cookie = next((c for c in cookies if c.get("name") == "attr_cookie"), None)224225assert cookie is not None226assert cookie.get("value") == "test_value"227228229class TestBidiBrowsingContexts:230"""Test browsing context operations."""231232@pytest.fixture(autouse=True)233def setup(self, driver):234"""Setup for each test in this class."""235driver.delete_all_cookies()236yield237# Cleanup: delete all cookies to prevent bleed-through238driver.delete_all_cookies()239240def test_create_new_window(self, driver):241"""Test creating a new window context."""242# Create new tab243new_context = driver.browsing_context.create(type=WindowTypes.TAB)244245try:246assert new_context is not None247finally:248driver.browsing_context.close(new_context)249250def test_navigation_in_context(self, driver, pages):251"""Test navigation in a specific context."""252pages.load("blank.html")253254# Navigate using the BiDi API with the current context255driver.browsing_context.navigate(context=driver.current_window_handle, url=pages.url("blank.html"))256257# Verify page loaded258element = driver.find_element(By.TAG_NAME, "body")259assert element is not None260261262