Best Python code snippet using SeleniumBase
set.py
Source:set.py
1# META: timeout=long2import pytest3from tests.support.asserts import assert_error, assert_success4def set_window_rect(session, rect):5 return session.transport.send(6 "POST", "session/{session_id}/window/rect".format(**vars(session)),7 rect)8def is_fullscreen(session):9 # At the time of writing, WebKit does not conform to the10 # Fullscreen API specification.11 #12 # Remove the prefixed fallback when13 # https://bugs.webkit.org/show_bug.cgi?id=158125 is fixed.14 return session.execute_script("""15 return !!(window.fullScreen || document.webkitIsFullScreen)16 """)17def test_no_browsing_context(session, closed_window):18 response = set_window_rect(session, {})19 assert_error(response, "no such window")20@pytest.mark.parametrize("rect", [21 {"width": "a"},22 {"height": "b"},23 {"width": "a", "height": "b"},24 {"x": "a"},25 {"y": "b"},26 {"x": "a", "y": "b"},27 {"width": "a", "height": "b", "x": "a", "y": "b"},28 {"width": True},29 {"height": False},30 {"width": True, "height": False},31 {"x": True},32 {"y": False},33 {"x": True, "y": False},34 {"width": True, "height": False, "x": True, "y": False},35 {"width": []},36 {"height": []},37 {"width": [], "height": []},38 {"x": []},39 {"y": []},40 {"x": [], "y": []},41 {"width": [], "height": [], "x": [], "y": []},42 {"height": {}},43 {"width": {}},44 {"height": {}, "width": {}},45 {"x": {}},46 {"y": {}},47 {"x": {}, "y": {}},48 {"width": {}, "height": {}, "x": {}, "y": {}},49])50def test_invalid_types(session, rect):51 response = set_window_rect(session, rect)52 assert_error(response, "invalid argument")53@pytest.mark.parametrize("rect", [54 {"width": -1},55 {"height": -2},56 {"width": -1, "height": -2},57])58def test_out_of_bounds(session, rect):59 response = set_window_rect(session, rect)60 assert_error(response, "invalid argument")61def test_width_height_floats(session):62 response = set_window_rect(session, {"width": 500.5, "height": 420})63 value = assert_success(response)64 assert value["width"] == 50065 assert value["height"] == 42066 response = set_window_rect(session, {"width": 500, "height": 450.5})67 value = assert_success(response)68 assert value["width"] == 50069 assert value["height"] == 45070def test_x_y_floats(session):71 response = set_window_rect(session, {"x": 0.5, "y": 420})72 value = assert_success(response)73 assert value["x"] == 074 assert value["y"] == 42075 response = set_window_rect(session, {"x": 100, "y": 450.5})76 value = assert_success(response)77 assert value["x"] == 10078 assert value["y"] == 45079@pytest.mark.parametrize("rect", [80 {},81 {"width": None},82 {"height": None},83 {"width": None, "height": None},84 {"x": None},85 {"y": None},86 {"x": None, "y": None},87 {"width": None, "x": None},88 {"width": None, "y": None},89 {"height": None, "x": None},90 {"height": None, "Y": None},91 {"width": None, "height": None, "x": None, "y": None},92 {"width": 200},93 {"height": 200},94 {"x": 200},95 {"y": 200},96 {"width": 200, "x": 200},97 {"height": 200, "x": 200},98 {"width": 200, "y": 200},99 {"height": 200, "y": 200},100])101def test_no_change(session, rect):102 original = session.window.rect103 response = set_window_rect(session, rect)104 assert_success(response, original)105def test_fully_exit_fullscreen(session):106 session.window.fullscreen()107 assert is_fullscreen(session) is True108 response = set_window_rect(session, {"width": 400, "height": 400})109 value = assert_success(response)110 assert value["width"] == 400111 assert value["height"] == 400112 assert is_fullscreen(session) is False113def test_restore_from_minimized(session):114 session.window.minimize()115 assert session.execute_script("return document.hidden") is True116 response = set_window_rect(session, {"width": 450, "height": 450})117 value = assert_success(response)118 assert value["width"] == 450119 assert value["height"] == 450120 assert session.execute_script("return document.hidden") is False121def test_restore_from_maximized(session):122 original_size = session.window.size123 session.window.maximize()124 assert session.window.size != original_size125 response = set_window_rect(session, {"width": 400, "height": 400})126 value = assert_success(response)127 assert value["width"] == 400128 assert value["height"] == 400129def test_height_width(session):130 original = session.window.rect131 max = session.execute_script("""132 return {133 width: window.screen.availWidth,134 height: window.screen.availHeight,135 }""")136 response = set_window_rect(session, {137 "width": max["width"] - 100,138 "height": max["height"] - 100139 })140 assert_success(response, {141 "x": original["x"],142 "y": original["y"],143 "width": max["width"] - 100,144 "height": max["height"] - 100145 })146def test_height_width_larger_than_max(session):147 max = session.execute_script("""148 return {149 width: window.screen.availWidth,150 height: window.screen.availHeight,151 }""")152 response = set_window_rect(session, {153 "width": max["width"] + 100,154 "height": max["height"] + 100155 })156 rect = assert_success(response)157 assert rect["width"] >= max["width"]158 assert rect["height"] >= max["height"]159def test_height_width_as_current(session):160 original = session.window.rect161 response = set_window_rect(session, {162 "width": original["width"],163 "height": original["height"]164 })165 assert_success(response, {166 "x": original["x"],167 "y": original["y"],168 "width": original["width"],169 "height": original["height"]170 })171def test_x_y(session):172 original = session.window.rect173 response = set_window_rect(session, {174 "x": original["x"] + 10,175 "y": original["y"] + 10176 })177 assert_success(response, {178 "x": original["x"] + 10,179 "y": original["y"] + 10,180 "width": original["width"],181 "height": original["height"]182 })183def test_negative_x_y(session):184 original = session.window.rect185 response = set_window_rect(session, {"x": - 8, "y": - 8})186 os = session.capabilities["platformName"]187 # certain WMs prohibit windows from being moved off-screen188 if os == "linux":189 rect = assert_success(response)190 assert rect["x"] <= 0191 assert rect["y"] <= 0192 assert rect["width"] == original["width"]193 assert rect["height"] == original["height"]194 # On macOS, windows can only be moved off the screen on the195 # horizontal axis. The system menu bar also blocks windows from196 # being moved to (0,0).197 elif os == "mac":198 assert_success(response, {"x": -8,199 "y": 23,200 "width": original["width"],201 "height": original["height"]})202 # It turns out that Windows is the only platform on which the203 # window can be reliably positioned off-screen.204 elif os == "windows":205 assert_success(response, {"x": -8,206 "y": -8,207 "width": original["width"],208 "height": original["height"]})209def test_move_to_same_position(session):210 original_position = session.window.position211 position = session.window.position = original_position212 assert position == original_position213def test_move_to_same_x(session):214 original_x = session.window.position[0]215 position = session.window.position = (original_x, 345)216 assert position == (original_x, 345)217def test_move_to_same_y(session):218 original_y = session.window.position[1]219 position = session.window.position = (456, original_y)220 assert position == (456, original_y)221def test_resize_to_same_size(session):222 original_size = session.window.size223 size = session.window.size = original_size224 assert size == original_size225def test_resize_to_same_width(session):226 original_width = session.window.size[0]227 size = session.window.size = (original_width, 345)228 assert size == (original_width, 345)229def test_resize_to_same_height(session):230 original_height = session.window.size[1]231 size = session.window.size = (456, original_height)232 assert size == (456, original_height)233"""234TODO(ato):235 Disable test because the while statements are wrong.236 To fix this properly we need to write an explicit wait utility.237def test_resize_by_script(session):238 # setting the window size by JS is asynchronous239 # so we poll waiting for the results240 size0 = session.window.size241 session.execute_script("window.resizeTo(700, 800)")242 size1 = session.window.size243 while size0 == size1:244 size1 = session.window.size245 assert size1 == (700, 800)246 session.execute_script("window.resizeTo(800, 900)")247 size2 = session.window.size248 while size1 == size2:249 size2 = session.window.size250 assert size2 == (800, 900)251 assert size2 == {"width": 200, "height": 100}252"""253def test_payload(session):254 response = set_window_rect(session, {"x": 400, "y": 400})255 assert response.status == 200256 assert isinstance(response.body["value"], dict)257 value = response.body["value"]258 assert "width" in value259 assert "height" in value260 assert "x" in value261 assert "y" in value262 assert isinstance(value["width"], int)263 assert isinstance(value["height"], int)264 assert isinstance(value["x"], int)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!