Best Python code snippet using toolium_python
test_environment.py
Source:test_environment.py
1# -*- coding: utf-8 -*-2"""3Copyright 2016 Telefónica Investigación y Desarrollo, S.A.U.4This file is part of Toolium.5Licensed under the Apache License, Version 2.0 (the "License");6you may not use this file except in compliance with the License.7You may obtain a copy of the License at8 http://www.apache.org/licenses/LICENSE-2.09Unless required by applicable law or agreed to in writing, software10distributed under the License is distributed on an "AS IS" BASIS,11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12See the License for the specific language governing permissions and13limitations under the License.14"""15import os16import mock17import pytest18from toolium.behave.environment import (get_jira_key_from_scenario, before_all, before_feature, before_scenario,19 after_scenario, after_feature, after_all)20from toolium.config_files import ConfigFiles21from toolium.config_parser import ExtendedConfigParser22tags = (23 (["jira('PROJECT-32')"], 'PROJECT-32'),24 (["jira=PROJECT-32"], 'PROJECT-32'),25 (["jira(PROJECT-32)"], 'PROJECT-32'),26 (["jira='PROJECT-32'"], 'PROJECT-32'),27 (["jiraPROJECT-32"], 'PROJECT-32'),28 (["jira"], None),29 (["PROJECT-32"], None),30 (['slow', "jira('PROJECT-32')", 'critical'], 'PROJECT-32'),31 (['slow', "PROJECT-32", 'critical'], None),32 (['slow', "jira('PROJECT-32')", "jira('PROJECT-33')"], 'PROJECT-32'),33)34@pytest.mark.parametrize("tag_list, jira_key", tags)35def test_get_jira_key_from_scenario(tag_list, jira_key):36 scenario = mock.Mock()37 scenario.tags = tag_list38 # Extract Jira key and compare with expected key39 assert jira_key == get_jira_key_from_scenario(scenario)40@mock.patch('toolium.behave.environment.create_and_configure_wrapper')41def test_before_all(create_and_configure_wrapper):42 # Create context mock43 context = mock.MagicMock()44 context.config.userdata.get.return_value = None45 context.config_files = ConfigFiles()46 before_all(context)47 # Check that configuration folder is the same as environment folder48 expected_config_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'conf')49 assert context.config_files.config_directory == expected_config_directory50 assert context.config_files.config_properties_filenames is None51 assert context.config_files.config_log_filename is None52properties = (53 'TOOLIUM_CONFIG_ENVIRONMENT',54 'Config_environment',55 'env'56)57@pytest.mark.parametrize("property_name", properties)58@mock.patch('toolium.behave.environment.create_and_configure_wrapper')59def test_before_all_config_environment(create_and_configure_wrapper, property_name):60 # Create context mock61 context = mock.MagicMock()62 context.config.userdata.get.side_effect = lambda x: 'os' if x == property_name else None63 context.config_files = ConfigFiles()64 before_all(context)65 # Check that configuration folder is the same as environment folder and property 'TOOLIUM_CONFIG_ENVIRONMENT' is66 # configured67 expected_config_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'conf')68 assert context.config_files.config_directory == expected_config_directory69 assert context.config_files.config_properties_filenames == 'properties.cfg;os-properties.cfg;local-os-properties.cfg'70 assert context.config_files.config_log_filename is None71 assert os.environ['TOOLIUM_CONFIG_ENVIRONMENT'] == 'os'72 del os.environ['TOOLIUM_CONFIG_ENVIRONMENT']73@mock.patch('toolium.behave.environment.start_driver')74def test_before_feature(start_driver):75 # Create context mock76 context = mock.MagicMock()77 context.toolium_config = ExtendedConfigParser()78 feature = mock.MagicMock()79 feature.tags = ['a', 'b']80 before_feature(context, feature)81 # Check that start_driver is not called82 start_driver.assert_not_called()83@mock.patch('toolium.behave.environment.start_driver')84def test_before_feature_reuse_driver(start_driver):85 # Create context mock86 context = mock.MagicMock()87 context.toolium_config = ExtendedConfigParser()88 feature = mock.MagicMock()89 feature.tags = ['a', 'reuse_driver', 'b']90 before_feature(context, feature)91 # Check that start_driver is called when reuse_driver tag92 start_driver.assert_called_once_with(context, False)93 assert context.reuse_driver_from_tags is True94@mock.patch('toolium.behave.environment.start_driver')95def test_before_feature_reuse_driver_no_driver(start_driver):96 # Create context mock97 context = mock.MagicMock()98 context.toolium_config = ExtendedConfigParser()99 feature = mock.MagicMock()100 feature.tags = ['a', 'reuse_driver', 'b', 'no_driver']101 before_feature(context, feature)102 # Check that start_driver is called when reuse_driver tag, with True no_driver param103 start_driver.assert_called_once_with(context, True)104 assert context.reuse_driver_from_tags is True105@mock.patch('toolium.behave.environment.add_assert_screenshot_methods')106@mock.patch('toolium.behave.environment.DriverWrappersPool')107@mock.patch('toolium.behave.environment.start_driver')108def test_before_scenario(start_driver, DriverWrappersPool, add_assert_screenshot_methods):109 # Create context mock110 context = mock.MagicMock()111 context.toolium_config = ExtendedConfigParser()112 scenario = mock.MagicMock()113 scenario.tags = ['a', 'b']114 before_scenario(context, scenario)115 # Check that start_driver is called116 start_driver.assert_called_once_with(context, False)117 DriverWrappersPool.stop_drivers.assert_not_called()118@mock.patch('toolium.behave.environment.add_assert_screenshot_methods')119@mock.patch('toolium.behave.environment.DriverWrappersPool')120@mock.patch('toolium.behave.environment.start_driver')121def test_before_scenario_reset_driver(start_driver, DriverWrappersPool, add_assert_screenshot_methods):122 # Create context mock123 context = mock.MagicMock()124 context.toolium_config = ExtendedConfigParser()125 scenario = mock.MagicMock()126 scenario.tags = ['a', 'reset_driver', 'b']127 before_scenario(context, scenario)128 # Check that start_driver and stop drivers are called129 start_driver.assert_called_once_with(context, False)130 DriverWrappersPool.stop_drivers.assert_called_once_with()131@mock.patch('toolium.behave.environment.add_assert_screenshot_methods')132@mock.patch('toolium.behave.environment.start_driver')133def test_before_scenario_no_driver(start_driver, add_assert_screenshot_methods):134 # Create context mock135 context = mock.MagicMock()136 context.toolium_config = ExtendedConfigParser()137 scenario = mock.MagicMock()138 scenario.tags = ['a', 'no_driver', 'b']139 before_scenario(context, scenario)140 # Check that start_driver is called141 start_driver.assert_called_once_with(context, True)142@mock.patch('toolium.behave.environment.add_assert_screenshot_methods')143@mock.patch('toolium.behave.environment.start_driver')144def test_before_scenario_no_driver_feature(start_driver, add_assert_screenshot_methods):145 # Create context mock146 context = mock.MagicMock()147 context.toolium_config = ExtendedConfigParser()148 scenario = mock.MagicMock()149 scenario.tags = ['a', 'b']150 scenario.feature.tags = ['no_driver']151 before_scenario(context, scenario)152 # Check that start_driver is called153 start_driver.assert_called_once_with(context, True)154@mock.patch('toolium.behave.environment.DriverWrappersPool')155def test_after_scenario_passed(DriverWrappersPool):156 # Create context mock157 context = mock.MagicMock()158 context.global_status = {'test_passed': True}159 scenario = mock.MagicMock()160 scenario.name = 'name'161 scenario.status = 'passed'162 after_scenario(context, scenario)163 # Check that close_drivers is called164 assert context.global_status['test_passed'] is True165 DriverWrappersPool.close_drivers.assert_called_once_with(context=context, scope='function', test_name='name',166 test_passed=True)167@mock.patch('toolium.behave.environment.DriverWrappersPool')168def test_after_scenario_failed(DriverWrappersPool):169 # Create context mock170 context = mock.MagicMock()171 context.global_status = {'test_passed': True}172 scenario = mock.MagicMock()173 scenario.name = 'name'174 scenario.status = 'failed'175 after_scenario(context, scenario)176 # Check that close_drivers is called177 assert context.global_status['test_passed'] is False178 DriverWrappersPool.close_drivers.assert_called_once_with(context=context, scope='function', test_name='name',179 test_passed=False)180@mock.patch('toolium.behave.environment.DriverWrappersPool')181def test_after_scenario_skipped(DriverWrappersPool):182 # Create context mock183 context = mock.MagicMock()184 context.global_status = {'test_passed': True}185 scenario = mock.MagicMock()186 scenario.name = 'name'187 scenario.status = 'skipped'188 after_scenario(context, scenario)189 # Check that close_drivers is not called190 assert context.global_status['test_passed'] is True191 DriverWrappersPool.close_drivers.assert_not_called()192@mock.patch('toolium.behave.environment.DriverWrappersPool')193def test_after_feature(DriverWrappersPool):194 # Create context mock195 context = mock.MagicMock()196 context.global_status = {'test_passed': True}197 feature = mock.MagicMock()198 feature.name = 'name'199 after_feature(context, feature)200 # Check that close_drivers is called201 DriverWrappersPool.close_drivers.assert_called_once_with(scope='module', test_name='name', test_passed=True)202@mock.patch('toolium.behave.environment.DriverWrappersPool')203def test_after_all(DriverWrappersPool):204 # Create context mock205 context = mock.MagicMock()206 context.global_status = {'test_passed': True}207 after_all(context)208 # Check that close_drivers is called209 DriverWrappersPool.close_drivers.assert_called_once_with(scope='session', test_name='multiple_tests',...
appiummanager.py
Source:appiummanager.py
...35 for line in f:36 if rx.search(line):37 return True38 return False39def start_driver(packbund, path=None, port=None):40 global _desired_capacities, _ports41 logger.debug('start_driver: packbund=%s, path=%s, port=%s' % (packbund, path, port))42 wd = None43 dc = {'bundleId': packbund}44 if path:45 dc['app'] = path46 dc.update(_desired_capacities)47 logger.debug('start_driver: desired_capabilities=%s' % dc)48 if port is None:49 logger.debug('start_driver: finding available port in list=%s' % _ports)50 for p, d in _ports.iteritems():51 if d is None:52 port = p53 break...
conftest.py
Source:conftest.py
...7import pytest8import os9logger = logger.getLogger()10@pytest.fixture(scope="module")11def start_driver():12 logger.info("Start of fixture create_driver")13 driver = DriverFactory.create_driver(driver_id=int(ReadConfig.get_driver_id()), is_headless=False)14 driver.get(ReadConfig.get_application_url())15 driver.maximize_window()16 yield driver17 driver.quit()18 logger.info("End of fixture create_driver")19@pytest.fixture(scope="module", autouse=True)20def clear_screenshot_folder_before_run():21 filelist = [file for file in os.listdir(f"{ROOT_DIR}/tests/screenshots") if file.endswith(".jpg")]22 for file in filelist:23 os.remove(os.path.join(f"{ROOT_DIR}/tests/screenshots", file))24 yield25@pytest.fixture(autouse=True)...
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!!