Path: blob/trunk/rb/lib/selenium/webdriver/bidi/browsing_context.rb
4050 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819module Selenium20module WebDriver21class BiDi22# Implements the BrowsingContext Module of the WebDriver-BiDi specification23# Continue to use functionality from existing `driver.navigate` method24#25# @api private26#27class BrowsingContext28READINESS_STATE = {29'none' => 'none',30'eager' => 'interactive',31'normal' => 'complete'32}.freeze3334# TODO: store current window handle in bridge object instead of always calling it35def initialize(bridge)36@bridge = bridge37@bidi = @bridge.bidi38page_load_strategy = bridge.capabilities[:page_load_strategy]39@readiness = READINESS_STATE[page_load_strategy]40end4142# Navigates to the specified URL in the given browsing context.43#44# @param url [String] The URL to navigate to.45# @param context_id [String, NilClass] The ID of the browsing context to navigate in.46# Defaults to the window handle of the current context.47def navigate(url, context_id: nil)48context_id ||= @bridge.window_handle49@bidi.send_cmd('browsingContext.navigate', context: context_id, url: url, wait: @readiness)50end5152# Traverses the browsing context history by a given delta.53#54# @param delta [Integer] The number of steps to traverse.55# Positive values go forwards, negative values go backwards.56# @param context_id [String, NilClass] The ID of the context to traverse.57# Defaults to the window handle of the current context.58def traverse_history(delta, context_id: nil)59context_id ||= @bridge.window_handle60@bidi.send_cmd('browsingContext.traverseHistory', context: context_id, delta: delta)61end6263# Reloads the browsing context.64# @param [String, NilClass] context_id The ID of the context to reload.65# Defaults to the window handle of the current context.66# @param [Boolean] ignore_cache Whether to bypass the cache when reloading.67# Defaults to false.68def reload(context_id: nil, ignore_cache: false)69context_id ||= @bridge.window_handle70params = {context: context_id, ignore_cache: ignore_cache, wait: @readiness}71@bidi.send_cmd('browsingContext.reload', **params)72end7374# Closes the browsing context.75#76# @param [String] context_id The ID of the context to close.77# Defaults to the window handle of the current context.78def close(context_id: nil)79context_id ||= @bridge.window_handle80@bidi.send_cmd('browsingContext.close', context: context_id)81end8283# Create a new browsing context.84#85# @param [Symbol] type The type of browsing context to create.86# Valid options are :tab and :window with :window being the default87# @param [String] context_id The reference context for the new browsing context.88# Defaults to the current window handle.89#90# @return [String] The context ID of the created browsing context.91def create(type: nil, context_id: nil)92type ||= :window93context_id ||= @bridge.window_handle94result = @bidi.send_cmd('browsingContext.create', type: type.to_s, referenceContext: context_id)95result['context']96end9798def set_viewport(context_id: nil, width: nil, height: nil, device_pixel_ratio: nil)99context_id ||= @bridge.window_handle100params = {context: context_id, viewport: {width:, height:}, device_pixel_ratio:}101@bidi.send_cmd('browsingContext.setViewport', **params)102end103104def handle_user_prompt(context_id, accept: true, text: nil)105@bidi.send_cmd('browsingContext.handleUserPrompt', context: context_id, accept: accept, text: text)106end107108def activate(context_id: nil)109context_id ||= @bridge.window_handle110@bidi.send_cmd('browsingContext.activate', context: context_id)111end112end # BrowsingContext113end # BiDi114end # WebDriver115end # Selenium116117118