Skip to content

Commit e3b55a2

Browse files
Augustin GottliebAugustin Gottlieb
authored andcommitted
set client window state implementation
1 parent 28f0f1d commit e3b55a2

File tree

5 files changed

+191
-10
lines changed

5 files changed

+191
-10
lines changed

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?) || windows.first
62+
end
6163
end # Browser
6264
end # BiDi
6365
end # WebDriver
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
def set_state(state:, width: nil, height: nil, x: nil, y: nil)
44+
params = {clientWindow: @handle, state: state.to_s, width: width, height: height, x: x, y: y}.compact
45+
46+
response = @bidi.send_cmd('browser.setClientWindowState', **params)
47+
update_attributes(state: state, width: width, height: height, x: x, y: y)
48+
response
49+
end
50+
51+
def maximize
52+
set_state(state: 'maximized')
53+
end
54+
55+
def minimize
56+
set_state(state: 'minimized')
57+
end
58+
59+
def fullscreen
60+
set_state(state: 'fullscreen')
61+
end
62+
63+
def resize(width:, height:, x: nil, y: nil)
64+
set_state(state: 'normal', width: width, height: height, x: x, y: y)
65+
end
66+
67+
private
68+
69+
def update_attributes(state:, width:, height:, x:, y:)
70+
@state = state.to_sym
71+
@width = width.to_i if width
72+
@height = height.to_i if height
73+
@x = x.to_i if x
74+
@y = y.to_i if y
75+
end
76+
end # Window
77+
end # Browser
78+
end # BiDi
79+
end # WebDriver
80+
end # Selenium

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ module Selenium
22
module WebDriver
33
class BiDi
44
class Browser
5-
Window: Selenium::WebDriver::BiDi::Browser::Window
6-
75
@bidi: BiDi
6+
@window: Window?
87

98
def initialize: (BiDi bidi) -> void
109

1110
def create_user_context: () -> Hash[String, String]
1211

13-
def user_contexts: () -> Array[Hash[String, String]]
12+
def user_contexts: () -> Hash[String, Array[Hash[String, String]]]
13+
14+
def remove_user_context: (String user_context) -> Hash[untyped, untyped]
15+
16+
def windows: () -> Array[Window]
1417

15-
def remove_user_context: (String user_context) -> Hash[nil, nil]
18+
def window: () -> Window?
1619
end
1720
end
1821
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Selenium
2+
module WebDriver
3+
class BiDi
4+
class Browser
5+
class Window
6+
attr_reader handle: String
7+
attr_reader active: bool
8+
attr_reader state: Symbol
9+
10+
attr_accessor height: Integer
11+
attr_accessor width: Integer
12+
attr_accessor x: Integer
13+
attr_accessor y: Integer
14+
15+
@bidi: BiDi
16+
@handle: String
17+
@active: bool
18+
@state: Symbol
19+
@height: Integer
20+
@width: Integer
21+
@x: Integer
22+
@y: Integer
23+
24+
def initialize: (BiDi bidi, handle: String, active: bool, height: Integer, width: Integer, x: Integer, y: Integer, state: String) -> void
25+
26+
def active?: () -> bool
27+
28+
def set_state: (state: String | Symbol, ?width: Integer?, ?height: Integer?, ?x: Integer?, ?y: Integer?) -> Hash[untyped, untyped]
29+
30+
def maximize: () -> Hash[untyped, untyped]
31+
32+
def minimize: () -> Hash[untyped, untyped]
33+
34+
def fullscreen: () -> Hash[untyped, untyped]
35+
36+
def resize: (width: Integer, height: Integer, ?x: Integer?, ?y: Integer?) -> Hash[untyped, untyped]
37+
38+
private
39+
40+
def update_attributes: (state: String | Symbol, width: Integer?, height: Integer?, x: Integer?, y: Integer?) -> void
41+
end
42+
end
43+
end
44+
end
45+
end

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

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

7474
expect(active_window).to be_a(Selenium::WebDriver::BiDi::Browser::Window)
7575
end
76+
77+
# Skip all browsers until browser.setClientWindowState is implemented
78+
it 'maximizes window', except: {browser: %i[chrome edge firefox]} do
79+
browser = described_class.new(bidi)
80+
window = browser.window
81+
82+
window.maximize
83+
expect(window.state).to eq(:maximized)
84+
end
85+
86+
# Skip all browsers until browser.setClientWindowState is implemented
87+
it 'minimizes window', except: {browser: %i[chrome edge firefox]} do
88+
browser = described_class.new(bidi)
89+
window = browser.window
90+
91+
window.minimize
92+
expect(window.state).to eq(:minimized)
93+
end
94+
95+
# Skip all browsers until browser.setClientWindowState is implemented
96+
it 'sets window to fullscreen', except: {browser: %i[chrome edge firefox]} do
97+
browser = described_class.new(bidi)
98+
window = browser.window
99+
100+
window.fullscreen
101+
expect(window.state).to eq(:fullscreen)
102+
end
103+
104+
# Skip all browsers until browser.setClientWindowState is implemented
105+
it 'resizes window', except: {browser: %i[chrome edge firefox]} do
106+
browser = described_class.new(bidi)
107+
window = browser.window
108+
109+
window.resize(width: 800, height: 600)
110+
expect(window.state).to eq(:normal)
111+
expect(window.width).to eq(800)
112+
expect(window.height).to eq(600)
113+
end
114+
115+
# Skip all browsers until browser.setClientWindowState is implemented
116+
it 'sets window state with position', except: {browser: %i[chrome edge firefox]} do
117+
browser = described_class.new(bidi)
118+
window = browser.window
119+
120+
window.set_state(state: 'normal', width: 1024, height: 768, x: 100, y: 50)
121+
expect(window.state).to eq(:normal)
122+
expect(window.width).to eq(1024)
123+
expect(window.height).to eq(768)
124+
expect(window.x).to eq(100)
125+
expect(window.y).to eq(50)
126+
end
76127
end
77128
end
78129
end

0 commit comments

Comments
 (0)