Skip to content

Commit f8be682

Browse files
Augustin GottliebAugustin Gottlieb
authored andcommitted
set window state rb bidi
1 parent 28f0f1d commit f8be682

File tree

5 files changed

+132
-7
lines changed

5 files changed

+132
-7
lines changed

rb/.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"rubyLsp.testLibrary": "rspec"
3+
}

rb/Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'rspec/core/rake_task'
55
desc 'Setup everything to run tests in RubyMine'
66
task :update do
77
cmd = [
8-
'bazel build @bundle//:bundle',
8+
' @bundle//:bundle',
99
'//rb:selenium-devtools',
1010
'//rb:selenium-webdriver',
1111
'//java/src/org/openqa/selenium/grid:executable-grid'

rb/lib/selenium/webdriver/bidi/browser.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717
# specific language governing permissions and limitations
1818
# under the License.
1919

20+
require_relative 'browser/window'
21+
2022
module Selenium
2123
module WebDriver
2224
class BiDi
2325
class Browser
24-
Window = Struct.new(:handle, :active, :height, :width, :x, :y, :state) do
25-
def active?
26-
active
27-
end
28-
end
2926
def initialize(bidi)
3027
@bidi = bidi
28+
@window = nil
3129
end
3230

3331
def create_user_context
@@ -55,9 +53,13 @@ def windows
5553
y: win_data['y'],
5654
state: win_data['state']
5755
}
58-
Window.new(**attributes)
56+
Window.new(@bidi, **attributes)
5957
end
6058
end
59+
60+
def window
61+
@window ||= windows.find(&:active?)
62+
end
6163
end # Browser
6264
end # BiDi
6365
end # WebDriver
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed to the Software Freedom Conservancy (SFC) under one
4+
# or more contributor license agreements. See the NOTICE file
5+
# distributed with this work for additional information
6+
# regarding copyright ownership. The SFC licenses this file
7+
# to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance
9+
# with the License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing,
14+
# software distributed under the License is distributed on an
15+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
# KIND, either express or implied. See the License for the
17+
# specific language governing permissions and limitations
18+
# under the License.
19+
20+
module Selenium
21+
module WebDriver
22+
class BiDi
23+
class Browser
24+
class Window
25+
attr_reader :handle, :active, :state
26+
attr_accessor :height, :width, :x, :y
27+
28+
def initialize(bidi, **opts)
29+
@bidi = bidi
30+
@handle = opts[:handle]
31+
@active = opts[:active]
32+
@height = opts[:height].to_i
33+
@width = opts[:width].to_i
34+
@x = opts[:x].to_i
35+
@y = opts[:y].to_i
36+
@state = opts[:state].to_sym
37+
end
38+
39+
def active?
40+
@active
41+
end
42+
43+
# Set the client window state
44+
# @param state [String, Symbol] One of: "fullscreen", "maximized", "minimized", "normal"
45+
# @param width [Integer, nil] Width in pixels (only for "normal" state)
46+
# @param height [Integer, nil] Height in pixels (only for "normal" state)
47+
# @param x [Integer, nil] X position (only for "normal" state)
48+
# @param y [Integer, nil] Y position (only for "normal" state)
49+
def set_state(state:, width: nil, height: nil, x: nil, y: nil)
50+
params = {clientWindow: @handle, state: state.to_s, width: width, height: height, x: x, y: y}.compact
51+
52+
response = @bidi.send_cmd('browser.setClientWindowState', **params)
53+
update_attributes(state: state, width: width, height: height, x: x, y: y)
54+
response
55+
end
56+
57+
private
58+
59+
def update_attributes(state:, width:, height:, x:, y:)
60+
@state = state.to_sym
61+
@width = width.to_i if width
62+
@height = height.to_i if height
63+
@x = x.to_i if x
64+
@y = y.to_i if y
65+
end
66+
67+
public
68+
69+
# Convenience methods for common state changes
70+
def maximize
71+
set_state(state: 'maximized')
72+
end
73+
74+
def minimize
75+
set_state(state: 'minimized')
76+
end
77+
78+
def fullscreen
79+
set_state(state: 'fullscreen')
80+
end
81+
82+
def resize(width:, height:, x: nil, y: nil)
83+
set_state(state: 'normal', width: width, height: height, x: x, y: y)
84+
end
85+
end # Window
86+
end # Browser
87+
end # BiDi
88+
end # WebDriver
89+
end # Selenium

rb/spec/integration/selenium/webdriver/bidi/browser_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,37 @@ class BiDi
7373

7474
expect(active_window).to be_a(Selenium::WebDriver::BiDi::Browser::Window)
7575
end
76+
77+
it 'set window state' do
78+
browser = described_class.new(bidi)
79+
window = browser.window
80+
81+
# Test maximize
82+
window.maximize
83+
expect(window.state).to eq(:maximized)
84+
85+
# Test minimize
86+
window.minimize
87+
expect(window.state).to eq(:minimized)
88+
89+
# Test fullscreen
90+
window.fullscreen
91+
expect(window.state).to eq(:fullscreen)
92+
93+
# Test resize with normal state
94+
window.resize(width: 800, height: 600)
95+
expect(window.state).to eq(:normal)
96+
expect(window.width).to eq(800)
97+
expect(window.height).to eq(600)
98+
99+
# Test set_state with position
100+
window.set_state(state: 'normal', width: 1024, height: 768, x: 100, y: 50)
101+
expect(window.state).to eq(:normal)
102+
expect(window.width).to eq(1024)
103+
expect(window.height).to eq(768)
104+
expect(window.x).to eq(100)
105+
expect(window.y).to eq(50)
106+
end
76107
end
77108
end
78109
end

0 commit comments

Comments
 (0)