Path: blob/trunk/rb/lib/selenium/webdriver/bidi/browsing_context.rb
1865 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#24# @api private25#26class BrowsingContext27READINESS_STATE = {28'none' => 'none',29'eager' => 'interactive',30'normal' => 'complete'31}.freeze3233# TODO: store current window handle in bridge object instead of always calling it34def initialize(bridge)35@bridge = bridge36@bidi = @bridge.bidi37page_load_strategy = bridge.capabilities[:page_load_strategy]38@readiness = READINESS_STATE[page_load_strategy]39end4041# Navigates to the specified URL in the given browsing context.42#43# @param url [String] The URL to navigate to.44# @param context_id [String, NilClass] The ID of the browsing context to navigate in.45# Defaults to the window handle of the current context.46def navigate(url, context_id: nil)47context_id ||= @bridge.window_handle48@bidi.send_cmd('browsingContext.navigate', context: context_id, url: url, wait: @readiness)49end5051# Traverses the browsing context history by a given delta.52#53# @param delta [Integer] The number of steps to traverse.54# Positive values go forwards, negative values go backwards.55# @param context_id [String, NilClass] The ID of the context to traverse.56# Defaults to the window handle of the current context.57def traverse_history(delta, context_id: nil)58context_id ||= @bridge.window_handle59@bidi.send_cmd('browsingContext.traverseHistory', context: context_id, delta: delta)60end6162# Reloads the browsing context.63# @param [String, NilClass] context_id The ID of the context to reload.64# Defaults to the window handle of the current context.65# @param [Boolean] ignore_cache Whether to bypass the cache when reloading.66# Defaults to false.67def reload(context_id: nil, ignore_cache: false)68context_id ||= @bridge.window_handle69params = {context: context_id, ignore_cache: ignore_cache, wait: @readiness}70@bidi.send_cmd('browsingContext.reload', **params)71end7273# Closes the browsing context.74#75# @param [String] context_id The ID of the context to close.76# Defaults to the window handle of the current context.77def close(context_id: nil)78context_id ||= @bridge.window_handle79@bidi.send_cmd('browsingContext.close', context: context_id)80end8182# Create a new browsing context.83#84# @param [Symbol] type The type of browsing context to create.85# Valid options are :tab and :window with :window being the default86# @param [String] context_id The reference context for the new browsing context.87# Defaults to the current window handle.88#89# @return [String] The context ID of the created browsing context.90def create(type: nil, context_id: nil)91type ||= :window92context_id ||= @bridge.window_handle93result = @bidi.send_cmd('browsingContext.create', type: type.to_s, referenceContext: context_id)94result['context']95end9697def set_viewport(context_id: nil, width: nil, height: nil, device_pixel_ratio: nil)98context_id ||= @bridge.window_handle99params = {context: context_id, viewport: {width:, height:}, device_pixel_ratio:}100@bidi.send_cmd('browsingContext.setViewport', **params)101end102103def handle_user_prompt(context_id, accept: true, text: nil)104@bidi.send_cmd('browsingContext.handleUserPrompt', context: context_id, accept: accept, text: text)105end106107def activate(context_id: nil)108context_id ||= @bridge.window_handle109@bidi.send_cmd('browsingContext.activate', context: context_id)110end111end112end # BiDi113end # WebDriver114end # Selenium115116117