Best Python code snippet using toolium_python
test_visual_test.py
Source:test_visual_test.py
...201 PilEngine().assertSameFiles(image, expected_image, threshold)202def test_crop_element(driver_wrapper):203 # Create element mock204 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0205 web_element = get_mock_element(x=250, y=40, height=40, width=300)206 visual = VisualTest(driver_wrapper)207 # Resize image208 img = Image.open(file_v1)209 img = visual.crop_element(img, web_element)210 # Assert output image211 assert_image(visual, img, 'report_name', 'register_cropped_element')212def test_crop_element_oversize(driver_wrapper):213 # Create element mock214 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0215 web_element = get_mock_element(x=250, y=200, height=400, width=300)216 # y + width > img.size[1] --> 600 > 388217 visual = VisualTest(driver_wrapper)218 # Resize image219 img = Image.open(file_v1)220 img = visual.crop_element(img, web_element)221 # Assert output image222 assert_image(visual, img, 'report_name', 'register_cropped_element_oversize')223def test_get_scrolls_size(driver_wrapper):224 # Update conf and create a new VisualTest instance225 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth226 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 400, 900]227 driver_wrapper.config.set('Driver', 'type', 'chrome')228 visual = VisualTest(driver_wrapper)229 # Check scrolls230 assert visual.get_scrolls_size() == {'x': 17, 'y': 17}231def test_get_scrolls_size_y(driver_wrapper):232 # Update conf and create a new VisualTest instance233 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth234 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 400, 1200]235 driver_wrapper.config.set('Driver', 'type', 'chrome')236 visual = VisualTest(driver_wrapper)237 # Check scrolls238 assert visual.get_scrolls_size() == {'x': 0, 'y': 17}239def test_get_scrolls_size_without_scrolls(driver_wrapper):240 # Update conf and create a new VisualTest instance241 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth242 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 600, 1200]243 driver_wrapper.config.set('Driver', 'type', 'chrome')244 visual = VisualTest(driver_wrapper)245 # Check scrolls246 assert visual.get_scrolls_size() == {'x': 0, 'y': 0}247def test_get_scrolls_size_iexplore(driver_wrapper):248 # Update conf and create a new VisualTest instance249 # Mock scrollHeight, scrollWidth, innerHeight, innerWidth250 driver_wrapper.driver.execute_script.side_effect = [600, 1200, 400, 900]251 driver_wrapper.config.set('Driver', 'type', 'iexplore')252 visual = VisualTest(driver_wrapper)253 # Check scrolls254 assert visual.get_scrolls_size() == {'x': 21, 'y': 21}255def test_get_scrolls_size_firefox(driver_wrapper):256 # Update conf and create a new VisualTest instance257 driver_wrapper.config.set('Driver', 'type', 'firefox')258 visual = VisualTest(driver_wrapper)259 # Check scrolls260 assert visual.get_scrolls_size() == {'x': 0, 'y': 0}261def test_remove_scrolls(driver_wrapper):262 # Create a new VisualTest instance263 visual = VisualTest(driver_wrapper)264 visual.get_scrolls_size = lambda: {'x': 0, 'y': 17}265 # Remove scroll266 img = Image.open(file_scroll)267 img = visual.remove_scrolls(img)268 # Assert output image269 assert_image(visual, img, 'report_name', 'register_chrome_scroll_removed')270def test_remove_scrolls_without_scroll(driver_wrapper):271 # Create a new VisualTest instance272 visual = VisualTest(driver_wrapper)273 visual.get_scrolls_size = lambda: {'x': 0, 'y': 0}274 # Remove scroll275 img = Image.open(file_scroll)276 img = visual.remove_scrolls(img)277 # Assert output image278 assert_image(visual, img, 'report_name', 'register_chrome_scroll')279def test_mobile_resize(driver_wrapper):280 # Update conf and create a new VisualTest instance281 driver_wrapper.config.set('Driver', 'type', 'ios')282 driver_wrapper.utils.get_window_size.return_value = {'width': 375, 'height': 667}283 visual = VisualTest(driver_wrapper)284 # Resize image285 img = Image.open(file_ios)286 img = visual.mobile_resize(img)287 # Assert output image288 assert_image(visual, img, 'report_name', 'ios_resized')289def test_mobile_no_resize(driver_wrapper):290 # Update conf and create a new VisualTest instance291 driver_wrapper.config.set('Driver', 'type', 'ios')292 driver_wrapper.utils.get_window_size.return_value = {'width': 750, 'height': 1334}293 visual = VisualTest(driver_wrapper)294 # Resize image295 orig_img = Image.open(file_ios)296 img = visual.mobile_resize(orig_img)297 # Assert that image object has not been modified298 assert orig_img == img299def test_desktop_resize(driver_wrapper):300 # Update conf and create a new VisualTest instance301 driver_wrapper.is_mac_test = mock.MagicMock(return_value=True)302 driver_wrapper.utils.get_window_size.return_value = {'width': 1280, 'height': 1024}303 visual = VisualTest(driver_wrapper)304 # Resize image305 img = Image.open(file_mac)306 img = visual.desktop_resize(img)307 # Assert output image308 assert_image(visual, img, 'report_name', 'mac_os_retina_resized')309def test_desktop_no_resize(driver_wrapper):310 # Update conf and create a new VisualTest instance311 driver_wrapper.is_mac_test = mock.MagicMock(return_value=True)312 driver_wrapper.utils.get_window_size.return_value = {'width': 3840, 'height': 2102}313 visual = VisualTest(driver_wrapper)314 # Resize image315 orig_img = Image.open(file_ios)316 img = visual.mobile_resize(orig_img)317 # Assert that image object has not been modified318 assert orig_img == img319def test_exclude_elements(driver_wrapper):320 # Create elements mock321 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0322 visual = VisualTest(driver_wrapper)323 web_elements = [get_mock_element(x=250, y=40, height=40, width=300),324 get_mock_element(x=250, y=90, height=20, width=100)]325 img = Image.open(file_v1) # Exclude elements326 img = visual.exclude_elements(img, web_elements)327 # Assert output image328 assert_image(visual, img, 'report_name', 'register_exclude')329def test_exclude_element_outofimage(driver_wrapper):330 # Create elements mock331 visual = VisualTest(driver_wrapper)332 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0333 web_elements = [get_mock_element(x=250, y=40, height=40, width=1500)]334 img = Image.open(file_v1)335 # Exclude elements336 img = visual.exclude_elements(img, web_elements)337 # Assert output image338 assert_image(visual, img, 'report_name', 'register_exclude_outofimage')339def test_exclude_no_elements(driver_wrapper):340 # Exclude no elements341 visual = VisualTest(driver_wrapper)342 img = Image.open(file_v1)343 img = visual.exclude_elements(img, [])344 # Assert output image345 assert_image(visual, img, 'report_name', 'register')346def test_assert_screenshot_no_enabled_force(driver_wrapper):347 # Configure driver mock348 with open(file_v1, "rb") as f:349 image_data = f.read()350 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data351 # Update conf and create a new VisualTest instance352 driver_wrapper.config.set('VisualTests', 'enabled', 'false')353 visual = VisualTest(driver_wrapper, force=True)354 # Add v1 baseline image355 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')356 shutil.copyfile(file_v1, baseline_file)357 # Assert screenshot358 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')359 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()360def test_assert_screenshot_no_enabled_force_fail(driver_wrapper):361 # Configure driver mock362 with open(file_v1, "rb") as f:363 image_data = f.read()364 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data365 # Update conf and create a new VisualTest instance366 driver_wrapper.config.set('VisualTests', 'fail', 'false')367 driver_wrapper.config.set('VisualTests', 'enabled', 'false')368 visual = VisualTest(driver_wrapper, force=True)369 # Add v2 baseline image370 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')371 shutil.copyfile(file_v2, baseline_file)372 # Assert screenshot373 with pytest.raises(AssertionError) as exc:374 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')375 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()376 assert str(exc.value).endswith("did not match the baseline '%s' (by a distance of 522.65)" % baseline_file)377def test_assert_screenshot_full_and_save_baseline(driver_wrapper):378 # Configure driver mock379 with open(file_v1, "rb") as f:380 image_data = f.read()381 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data382 driver_wrapper.config.set('VisualTests', 'save', 'true')383 visual = VisualTest(driver_wrapper)384 # Assert screenshot385 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')386 output_file = os.path.join(visual.output_directory, '01_screenshot_full__screenshot_suffix.png')387 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()388 # Output image and new baseline image must be equal389 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')390 compare_image_files(output_file, baseline_file)391def test_assert_screenshot_element_and_save_baseline(driver_wrapper):392 # Create element mock393 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0394 web_element = get_mock_element(x=250, y=40, height=40, width=300)395 # Configure driver mock396 with open(file_v1, "rb") as f:397 image_data = f.read()398 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data399 driver_wrapper.config.set('VisualTests', 'save', 'true')400 visual = VisualTest(driver_wrapper)401 # Assert screenshot402 visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix')403 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()404 # Check cropped image405 expected_image = os.path.join(root_path, 'resources', 'register_cropped_element.png')406 output_file = os.path.join(visual.output_directory, '01_screenshot_elem__screenshot_suffix.png')407 compare_image_files(output_file, expected_image)408 # Output image and new baseline image must be equal409 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')410 compare_image_files(output_file, baseline_file)411def test_assert_screenshot_full_and_compare(driver_wrapper):412 # Configure driver mock413 with open(file_v1, "rb") as f:414 image_data = f.read()415 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data416 visual = VisualTest(driver_wrapper)417 # Add baseline image418 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')419 shutil.copyfile(file_v1, baseline_file)420 # Assert screenshot421 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')422 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()423def test_assert_screenshot_element_and_compare(driver_wrapper):424 # Add baseline image425 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0426 visual = VisualTest(driver_wrapper)427 expected_image = os.path.join(root_path, 'resources', 'register_cropped_element.png')428 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')429 shutil.copyfile(expected_image, baseline_file)430 # Create element mock431 web_element = get_mock_element(x=250, y=40, height=40, width=300)432 # Configure driver mock433 with open(file_v1, "rb") as f:434 image_data = f.read()435 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data436 # Assert screenshot437 visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix')438 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()439def test_assert_screenshot_full_without_baseline(driver_wrapper):440 # Configure driver mock441 with open(file_v1, "rb") as f:442 image_data = f.read()443 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data444 driver_wrapper.config.set('VisualTests', 'fail', 'true')445 visual = VisualTest(driver_wrapper)446 # Assert screenshot447 with pytest.raises(AssertionError) as exc:448 visual.assert_screenshot(None, filename='screenshot_full', file_suffix='screenshot_suffix')449 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()450 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_full.png')451 assert str(exc.value) == 'Baseline file not found: %s' % baseline_file452def test_assert_screenshot_element_without_baseline(driver_wrapper):453 # Add baseline image454 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0455 driver_wrapper.config.set('VisualTests', 'fail', 'true')456 visual = VisualTest(driver_wrapper)457 # Create element mock458 web_element = get_mock_element(x=250, y=40, height=40, width=300)459 # Configure driver mock460 with open(file_v1, "rb") as f:461 image_data = f.read()462 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data463 # Assert screenshot464 with pytest.raises(AssertionError) as exc:465 visual.assert_screenshot(web_element, filename='screenshot_elem', file_suffix='screenshot_suffix')466 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()467 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_elem.png')468 assert str(exc.value) == 'Baseline file not found: %s' % baseline_file469def test_assert_screenshot_mobile_resize_and_exclude(driver_wrapper):470 # Create elements mock471 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0472 exclude_elements = [get_mock_element(x=0, y=0, height=24, width=375)]473 # Configure driver mock474 with open(file_ios, "rb") as f:475 image_data = f.read()476 driver_wrapper.utils.get_window_size.return_value = {'width': 375, 'height': 667}477 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data478 # Update conf and create a new VisualTest instance479 driver_wrapper.config.set('Driver', 'type', 'ios')480 driver_wrapper.config.set('VisualTests', 'save', 'true')481 visual = VisualTest(driver_wrapper)482 # Assert screenshot483 visual.assert_screenshot(None, filename='screenshot_ios', file_suffix='screenshot_suffix',484 exclude_elements=exclude_elements)485 driver_wrapper.driver.get_screenshot_as_png.assert_called_once_with()486 # Check cropped image487 expected_image = os.path.join(root_path, 'resources', 'ios_excluded.png')488 output_file = os.path.join(visual.output_directory, '01_screenshot_ios__screenshot_suffix.png')489 compare_image_files(output_file, expected_image)490 # Output image and new baseline image must be equal491 baseline_file = os.path.join(root_path, 'output', 'visualtests', 'baseline', 'firefox', 'screenshot_ios.png')492 compare_image_files(output_file, baseline_file)493def test_assert_screenshot_mobile_web_resize_and_exclude(driver_wrapper):494 # Create elements mock495 driver_wrapper.driver.execute_script.return_value = 0 # scrollX=0 and scrollY=0496 form_element = get_mock_element(x=0, y=0, height=559, width=375)497 exclude_elements = [get_mock_element(x=15, y=296.515625, height=32, width=345)]498 # Configure driver mock499 file_ios_web = os.path.join(root_path, 'resources', 'ios_web.png')500 with open(file_ios_web, "rb") as f:501 image_data = f.read()502 driver_wrapper.utils.get_window_size.return_value = {'width': 375, 'height': 667}503 driver_wrapper.driver.get_screenshot_as_png.return_value = image_data504 # Update conf and create a new VisualTest instance505 driver_wrapper.config.set('Driver', 'type', 'ios')506 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'safari')507 driver_wrapper.config.set('VisualTests', 'save', 'true')508 visual = VisualTest(driver_wrapper)509 # Assert screenshot510 visual.assert_screenshot(form_element, filename='screenshot_ios_web', file_suffix='screenshot_suffix',511 exclude_elements=exclude_elements)...
test_driver_utils.py
Source:test_driver_utils.py
...38 ('chrome', 'chrome'),39 ('iexplore-11', 'iexplore'),40 ('iexplore-11-on-WIN10', 'iexplore'),41)42def get_mock_element(x, y, height, width):43 """Create a mock element with custom location and size44 :param x: x location45 :param y: y location46 :param height: element height47 :param width: element width48 :returns: mock element49 """50 mock_element = mock.MagicMock(spec=WebElement)51 mock_element.location = {'x': x, 'y': y}52 mock_element.size = {'height': height, 'width': width}53 return mock_element54@pytest.fixture55def driver_wrapper():56 # Reset wrappers pool values57 DriverWrappersPool._empty_pool()58 DriverWrapper.config_properties_filenames = None59 # Create a new wrapper60 driver_wrapper = DriverWrappersPool.get_default_wrapper()61 driver_wrapper.driver = mock.MagicMock()62 # Configure properties63 root_path = os.path.dirname(os.path.realpath(__file__))64 config_files = ConfigFiles()65 config_files.set_config_directory(os.path.join(root_path, 'conf'))66 config_files.set_config_properties_filenames('properties.cfg')67 config_files.set_output_directory(os.path.join(root_path, 'output'))68 driver_wrapper.configure(config_files)69 yield driver_wrapper70 # Reset wrappers pool values71 DriverWrappersPool._empty_pool()72 DriverWrapper.config_properties_filenames = None73@pytest.fixture74def utils():75 # Create a new Utils instance76 return Utils()77@pytest.mark.parametrize("driver_type, driver_name", driver_name_tests)78def test_get_driver_name(driver_type, driver_name, driver_wrapper, utils):79 driver_wrapper.config.set('Driver', 'type', driver_type)80 assert utils.get_driver_name() == driver_name81def test_get_available_log_types_one_log_type(driver_wrapper, utils):82 # Configure mock83 log_types_mock = mock.PropertyMock(return_value=['client', 'server'])84 type(driver_wrapper.driver).log_types = log_types_mock85 driver_wrapper.config.set('Server', 'log_types', 'client')86 log_types = utils.get_available_log_types()87 log_types_mock.assert_not_called()88 assert log_types == ['client']89def test_get_available_log_types_multiple_log_types(driver_wrapper, utils):90 # Configure mock91 log_types_mock = mock.PropertyMock(return_value=['client', 'server'])92 type(driver_wrapper.driver).log_types = log_types_mock93 driver_wrapper.config.set('Server', 'log_types', 'client,server,browser')94 log_types = utils.get_available_log_types()95 log_types_mock.assert_not_called()96 assert log_types == ['client', 'server', 'browser']97def test_get_available_log_types_multiple_log_types_with_spaces(driver_wrapper, utils):98 # Configure mock99 log_types_mock = mock.PropertyMock(return_value=['client', 'server'])100 type(driver_wrapper.driver).log_types = log_types_mock101 driver_wrapper.config.set('Server', 'log_types', 'client, server , browser')102 log_types = utils.get_available_log_types()103 log_types_mock.assert_not_called()104 assert log_types == ['client', 'server', 'browser']105def test_get_available_log_types_none_log_type(driver_wrapper, utils):106 # Configure mock107 log_types_mock = mock.PropertyMock(return_value=['client', 'server'])108 type(driver_wrapper.driver).log_types = log_types_mock109 driver_wrapper.config.set('Server', 'log_types', '')110 log_types = utils.get_available_log_types()111 log_types_mock.assert_not_called()112 assert log_types == []113def test_get_available_log_types_all_log_type(driver_wrapper, utils):114 # Configure mock115 log_types_mock = mock.PropertyMock(return_value=['client', 'server'])116 type(driver_wrapper.driver).log_types = log_types_mock117 driver_wrapper.config.set('Server', 'log_types', 'all')118 log_types = utils.get_available_log_types()119 log_types_mock.assert_called_once_with()120 assert log_types == ['client', 'server']121def test_get_available_log_types_without_log_types(driver_wrapper, utils):122 # Configure mock123 log_types_mock = mock.PropertyMock(return_value=['client', 'server'])124 type(driver_wrapper.driver).log_types = log_types_mock125 log_types = utils.get_available_log_types()126 log_types_mock.assert_called_once_with()127 assert log_types == ['client', 'server']128def test_save_webdriver_logs_all_log_type(utils):129 # Configure mock130 Utils.save_webdriver_logs_by_type = mock.MagicMock()131 Utils.get_available_log_types = mock.MagicMock(return_value=['client', 'server'])132 utils.save_webdriver_logs('test_name')133 Utils.save_webdriver_logs_by_type.assert_has_calls([mock.call('client', 'test_name'),134 mock.call('server', 'test_name')])135def test_save_webdriver_logs_without_log_types(utils):136 # Configure mock137 Utils.save_webdriver_logs_by_type = mock.MagicMock()138 Utils.get_available_log_types = mock.MagicMock(return_value=[])139 utils.save_webdriver_logs('test_name')140 Utils.save_webdriver_logs_by_type.assert_not_called()141def test_get_remote_node(driver_wrapper, utils):142 # Configure mock143 driver_wrapper.driver.session_id = '5af'144 url = 'http://{}:{}/grid/api/testsession?session={}'.format('localhost', 4444, '5af')145 grid_response_json = {'session': 'e2', 'proxyId': 'http://10.20.30.40:5555', 'msg': 'slot found !',146 'inactivityTime': 78, 'success': True, 'internalKey': '7a'}147 with requests_mock.mock() as req_mock:148 req_mock.get(url, json=grid_response_json)149 # Get remote node and check result150 assert utils.get_remote_node() == ('grid', '10.20.30.40')151 assert url == req_mock.request_history[0].url152def test_get_remote_node_selenium3(driver_wrapper, utils):153 # Configure mock154 driver_wrapper.driver.session_id = '5af'155 url = 'http://{}:{}/grid/api/testsession?session={}'.format('localhost', 4444, '5af')156 grid_response_json = {'session': 'e2', 'proxyId': '10.20.30.40', 'msg': 'slot found !',157 'inactivityTime': 78, 'success': True, 'internalKey': '7a'}158 with requests_mock.mock() as req_mock:159 req_mock.get(url, json=grid_response_json)160 # Get remote node and check result161 assert utils.get_remote_node() == ('grid', '10.20.30.40')162 assert url == req_mock.request_history[0].url163def test_get_remote_node_ggr(driver_wrapper, utils):164 # Configure mock165 driver_wrapper.driver.session_id = '5af'166 grid_url = 'http://{}:{}/grid/api/testsession?session={}'.format('localhost', 4444, '5af')167 ggr_url = 'http://{}:{}/host/{}'.format('localhost', 4444, '5af')168 ggr_response_json = {'Count': 3, 'Username': '', 'Scheme': '', 'VNC': '', 'Name': 'host_name', 'Password': '',169 'Port': 4500}170 with requests_mock.mock() as req_mock:171 req_mock.get(grid_url, text='non_json_response')172 req_mock.get(ggr_url, json=ggr_response_json)173 # Get remote node and check result174 assert utils.get_remote_node() == ('ggr', 'host_name')175 assert grid_url == req_mock.request_history[0].url176 assert ggr_url == req_mock.request_history[1].url177def test_get_remote_node_selenoid(driver_wrapper, utils):178 # Configure mock179 driver_wrapper.driver.session_id = '5af'180 grid_url = 'http://{}:{}/grid/api/testsession?session={}'.format('localhost', 4444, '5af')181 ggr_url = 'http://{}:{}/host/{}'.format('localhost', 4444, '5af')182 selenoid_url = 'http://{}:{}/status'.format('localhost', 4444)183 selenoid_response_json = {'total': 5, 'used': 0, 'queued': 0, 'pending': 0, 'browsers': {'firefox': {'59.0': {}}}}184 with requests_mock.mock() as req_mock:185 req_mock.get(grid_url, text='non_json_response')186 req_mock.get(ggr_url, json={})187 req_mock.get(selenoid_url, json=selenoid_response_json)188 # Get remote node and check result189 assert utils.get_remote_node() == ('selenoid', 'localhost')190 assert grid_url == req_mock.request_history[0].url191 assert ggr_url == req_mock.request_history[1].url192 assert selenoid_url == req_mock.request_history[2].url193def test_get_remote_node_non_grid(driver_wrapper, utils):194 # Configure mock195 driver_wrapper.driver.session_id = '5af'196 grid_url = 'http://{}:{}/grid/api/testsession?session={}'.format('localhost', 4444, '5af')197 ggr_url = 'http://{}:{}/host/{}'.format('localhost', 4444, '5af')198 selenoid_url = 'http://{}:{}/status'.format('localhost', 4444)199 with requests_mock.mock() as req_mock:200 req_mock.get(grid_url, text='non_json_response')201 req_mock.get(ggr_url, json={})202 req_mock.get(selenoid_url, json={})203 # Get remote node and check result204 assert utils.get_remote_node() == ('selenium', 'localhost')205 assert grid_url == req_mock.request_history[0].url206 assert ggr_url == req_mock.request_history[1].url207 assert selenoid_url == req_mock.request_history[2].url208def test_get_remote_node_local_execution(driver_wrapper, utils):209 driver_wrapper.config.set('Server', 'enabled', 'false')210 assert utils.get_remote_node() == ('local', None)211def test_get_remote_video_url(utils):212 # Configure mock213 url = 'http://{}:{}/video'.format('10.20.30.40', 3000)214 video_url = 'http://{}:{}/download_video/f4.mp4'.format('10.20.30.40', 3000)215 video_response_json = {'exit_code': 1, 'out': [],216 'error': ['Cannot call this endpoint without required parameters: session and action'],217 'available_videos': {'5af': {'size': 489701,218 'session': '5af',219 'last_modified': 1460041262558,220 'download_url': video_url,221 'absolute_path': 'C:\\f4.mp4'}},222 'current_videos': []}223 with requests_mock.mock() as req_mock:224 req_mock.get(url, json=video_response_json)225 # Get remote video url and check result226 assert utils._get_remote_video_url('10.20.30.40', '5af') == video_url227 assert url == req_mock.request_history[0].url228def test_get_remote_video_url_no_videos(utils):229 # Configure mock230 url = 'http://{}:{}/video'.format('10.20.30.40', 3000)231 video_response_json = {'exit_code': 1, 'out': [],232 'error': ['Cannot call this endpoint without required parameters: session and action'],233 'available_videos': {},234 'current_videos': []}235 with requests_mock.mock() as req_mock:236 req_mock.get(url, json=video_response_json)237 # Get remote video url and check result238 assert utils._get_remote_video_url('10.20.30.40', '5af') is None239 assert url == req_mock.request_history[0].url240def test_is_remote_video_enabled_grid(utils):241 # Configure mock242 url = 'http://{}:{}/config'.format('10.20.30.40', 3000)243 config_response_json = {'out': [], 'error': [], 'exit_code': 0,244 'filename': ['selenium_grid_extras_config.json'],245 'config_runtime': {'theConfigMap': {246 'video_recording_options': {'width': '1024', 'videos_to_keep': '5',247 'frames': '30',248 'record_test_videos': 'true'}}}}249 with requests_mock.mock() as req_mock:250 req_mock.get(url, json=config_response_json)251 # Get remote video configuration and check result252 assert utils.is_remote_video_enabled('grid', '10.20.30.40') is True253 assert url == req_mock.request_history[0].url254def test_is_remote_video_enabled_grid_disabled(utils):255 # Configure mock256 url = 'http://{}:{}/config'.format('10.20.30.40', 3000)257 config_response_json = {'out': [], 'error': [], 'exit_code': 0,258 'filename': ['selenium_grid_extras_config.json'],259 'config_runtime': {'theConfigMap': {260 'video_recording_options': {'width': '1024', 'videos_to_keep': '5',261 'frames': '30',262 'record_test_videos': 'false'}}}}263 with requests_mock.mock() as req_mock:264 req_mock.get(url, json=config_response_json)265 # Get remote video configuration and check result266 assert utils.is_remote_video_enabled('grid', '10.20.30.40') is False267 assert url == req_mock.request_history[0].url268@mock.patch('toolium.utils.driver_utils.requests.get')269def test_is_remote_video_enabled_non_grid_extras(req_get_mock, utils):270 # Configure mock271 req_get_mock.side_effect = ConnectionError('exception error')272 # Get remote video configuration and check result273 assert utils.is_remote_video_enabled('grid', '10.20.30.40') is False274def test_is_remote_video_enabled_grid_empty_node(utils):275 # Get remote video configuration and check result276 assert utils.is_remote_video_enabled('grid', '') is False277def test_is_remote_video_enabled_ggr(utils):278 # Get remote video configuration and check result279 assert utils.is_remote_video_enabled('ggr', '') is True280def test_is_remote_video_enabled_selenoid(utils):281 # Get remote video configuration and check result282 assert utils.is_remote_video_enabled('selenoid', '') is True283def test_is_remote_video_enabled_unknown_server(utils):284 # Get remote video configuration and check result285 assert utils.is_remote_video_enabled('unknown', '') is False286@pytest.mark.parametrize("driver_type, appium_app, appium_browser_name, bar_height", navigation_bar_tests)287def test_get_safari_navigation_bar_height(driver_type, appium_app, appium_browser_name, bar_height, driver_wrapper,288 utils):289 driver_wrapper.config.set('Driver', 'type', driver_type)290 if appium_app:291 driver_wrapper.config.set('AppiumCapabilities', 'app', appium_app)292 if appium_browser_name:293 driver_wrapper.config.set('AppiumCapabilities', 'browserName', appium_browser_name)294 assert utils.get_safari_navigation_bar_height() == bar_height295def test_get_window_size_android_native(driver_wrapper, utils):296 # Configure driver mock297 window_size = {'width': 375, 'height': 667}298 driver_wrapper.driver.get_window_size.return_value = window_size299 driver_wrapper.config.set('Driver', 'type', 'android')300 driver_wrapper.config.set('AppiumCapabilities', 'app', 'C:/Demo.apk')301 assert utils.get_window_size() == window_size302 driver_wrapper.driver.get_window_size.assert_called_once_with()303def test_get_window_size_android_native_two_times(driver_wrapper, utils):304 # Configure driver mock305 window_size = {'width': 375, 'height': 667}306 driver_wrapper.driver.get_window_size.return_value = window_size307 driver_wrapper.config.set('Driver', 'type', 'android')308 driver_wrapper.config.set('AppiumCapabilities', 'app', 'C:/Demo.apk')309 assert utils.get_window_size() == window_size310 assert utils.get_window_size() == window_size311 # Check that window size is calculated only one time312 driver_wrapper.driver.get_window_size.assert_called_once_with()313def test_get_window_size_android_web(driver_wrapper, utils):314 # Configure driver mock315 window_size = {'width': 375, 'height': 667}316 driver_wrapper.driver.current_context = 'WEBVIEW'317 driver_wrapper.driver.execute_script.side_effect = [window_size['width'], window_size['height']]318 driver_wrapper.config.set('Driver', 'type', 'android')319 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'chrome')320 assert utils.get_window_size() == window_size321 driver_wrapper.driver.execute_script.assert_has_calls(322 [mock.call('return window.screen.width'), mock.call('return window.screen.height')])323def test_get_window_size_android_web_two_times(driver_wrapper, utils):324 # Configure driver mock325 window_size = {'width': 375, 'height': 667}326 driver_wrapper.driver.current_context = 'WEBVIEW'327 driver_wrapper.driver.execute_script.side_effect = [window_size['width'], window_size['height']]328 driver_wrapper.config.set('Driver', 'type', 'android')329 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'chrome')330 assert utils.get_window_size() == window_size331 assert utils.get_window_size() == window_size332 # Check that window size is calculated only one time333 driver_wrapper.driver.execute_script.assert_has_calls(334 [mock.call('return window.screen.width'), mock.call('return window.screen.height')])335def test_get_native_coords_android_web(driver_wrapper, utils):336 # Configure driver mock337 web_window_size = {'width': 500, 'height': 667}338 native_window_size = {'width': 250, 'height': 450}339 driver_wrapper.driver.current_context = 'WEBVIEW'340 driver_wrapper.driver.execute_script.side_effect = [web_window_size['width'], web_window_size['height']]341 driver_wrapper.driver.get_window_size.side_effect = [native_window_size]342 driver_wrapper.config.set('Driver', 'type', 'android')343 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'chrome')344 web_coords = {'x': 105, 'y': 185}345 native_coords = {'x': 52.5, 'y': 92.5}346 assert utils.get_native_coords(web_coords) == native_coords347def test_get_native_coords_ios_web(driver_wrapper, utils):348 # Configure driver mock349 web_window_size = {'width': 500, 'height': 667}350 native_window_size = {'width': 250, 'height': 450}351 driver_wrapper.driver.get_window_size.return_value = native_window_size352 utils.get_window_size = mock.MagicMock(return_value=web_window_size)353 driver_wrapper.config.set('Driver', 'type', 'ios')354 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'safari')355 web_coords = {'x': 105, 'y': 185}356 native_coords = {'x': 52.5, 'y': 156.5}357 assert utils.get_native_coords(web_coords) == native_coords358def test_swipe_android_native(driver_wrapper, utils):359 # Configure driver mock360 web_window_size = {'width': 500, 'height': 667}361 native_window_size = {'width': 250, 'height': 450}362 driver_wrapper.driver.get_window_size.side_effect = [web_window_size, native_window_size]363 driver_wrapper.driver.current_context = 'NATIVE_APP'364 driver_wrapper.config.set('Driver', 'type', 'android')365 driver_wrapper.config.set('AppiumCapabilities', 'app', 'C:/Demo.apk')366 # Create element mock367 element = get_mock_element(x=250, y=40, height=40, width=300)368 utils.swipe(element, 50, 100)369 driver_wrapper.driver.swipe.assert_called_once_with(400, 60, 450, 160, None)370def test_swipe_android_web(driver_wrapper, utils):371 # Configure driver mock372 web_window_size = {'width': 500, 'height': 667}373 native_window_size = {'width': 250, 'height': 450}374 driver_wrapper.driver.current_context = 'WEBVIEW'375 driver_wrapper.driver.execute_script.side_effect = [web_window_size['width'], web_window_size['height']]376 driver_wrapper.driver.get_window_size.side_effect = [native_window_size]377 driver_wrapper.config.set('Driver', 'type', 'android')378 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'chrome')379 # Create element mock380 element = get_mock_element(x=250, y=40, height=40, width=300)381 utils.swipe(element, 50, 100)382 driver_wrapper.driver.swipe.assert_called_once_with(200, 30, 250, 130, None)383def test_swipe_android_hybrid(driver_wrapper, utils):384 # Configure driver mock385 web_window_size = {'width': 500, 'height': 667}386 native_window_size = {'width': 250, 'height': 450}387 # driver_wrapper.utils388 driver_wrapper.driver.get_window_size.side_effect = [web_window_size, native_window_size]389 driver_wrapper.driver.current_context = 'WEBVIEW'390 driver_wrapper.config.set('Driver', 'type', 'android')391 driver_wrapper.config.set('AppiumCapabilities', 'app', 'C:/Demo.apk')392 # Create element mock393 element = get_mock_element(x=250, y=40, height=40, width=300)394 utils.swipe(element, 50, 100)395 driver_wrapper.driver.swipe.assert_called_once_with(200, 30, 250, 130, None)396def test_swipe_ios_web(driver_wrapper, utils):397 # Configure driver mock398 web_window_size = {'width': 500, 'height': 667}399 native_window_size = {'width': 250, 'height': 450}400 driver_wrapper.driver.get_window_size.return_value = native_window_size401 utils.get_window_size = mock.MagicMock(return_value=web_window_size)402 driver_wrapper.config.set('Driver', 'type', 'ios')403 driver_wrapper.config.set('AppiumCapabilities', 'browserName', 'safari')404 # Create element mock405 element = get_mock_element(x=250, y=40, height=40, width=300)406 utils.swipe(element, 50, 100)407 driver_wrapper.driver.swipe.assert_called_once_with(200, 94, 50, 100, None)408def test_swipe_web(driver_wrapper, utils):409 # Configure driver mock410 driver_wrapper.config.set('Driver', 'type', 'firefox')411 # Create element mock412 element = get_mock_element(x=250, y=40, height=40, width=300)413 with pytest.raises(Exception) as excinfo:414 utils.swipe(element, 50, 100)415 assert 'Swipe method is not implemented in Selenium' == str(excinfo.value)416def test_get_web_element_from_web_element(utils):417 element = WebElement(None, 1)418 web_element = utils.get_web_element(element)419 assert element == web_element420def test_get_web_element_from_page_element(driver_wrapper, utils):421 # Mock Driver.save_web_element = True422 driver_wrapper.config = mock.MagicMock()423 driver_wrapper.config.getboolean_optional.return_value = True424 element = PageElement(By.ID, 'element_id')425 element._web_element = 'mock_element'426 web_element = utils.get_web_element(element)...
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!!