How to use create_and_configure_wrapper method in toolium

Best Python code snippet using toolium_python

environment.py

Source:environment.py Github

copy

Full Screen

...47 # By default config directory is located in environment path48 if not context.config_files.config_directory:49 context.config_files.set_config_directory(DriverWrappersPool.get_default_config_directory())50 context.global_status = {'test_passed': True}51 create_and_configure_wrapper(context)52 # Behave dynamic environment53 context.dyn_env = DynamicEnvironment(logger=context.logger)54 # Initialize dataset behave variables55 dataset.behave_context = context56 dataset.toolium_config = context.toolium_config57def before_feature(context, feature):58 """Feature initialization59 :param context: behave context60 :param feature: running feature61 """62 context.global_status = {'test_passed': True}63 # Start driver if it should be reused in feature64 context.reuse_driver_from_tags = 'reuse_driver' in feature.tags65 if context.toolium_config.getboolean_optional('Driver', 'reuse_driver') or context.reuse_driver_from_tags:66 no_driver = 'no_driver' in feature.tags67 start_driver(context, no_driver)68 # Behave dynamic environment69 context.dyn_env.get_steps_from_feature_description(feature.description)70 context.dyn_env.execute_before_feature_steps(context)71def before_scenario(context, scenario):72 """Scenario initialization73 :param context: behave context74 :param scenario: running scenario75 """76 # Configure reset properties from behave tags77 if 'no_reset_app' in scenario.tags:78 os.environ['TOOLIUM_APPIUMCAPABILITIES_NORESET'] = 'AppiumCapabilities_noReset=true'79 os.environ['TOOLIUM_APPIUMCAPABILITIES_FULLRESET'] = 'AppiumCapabilities_fullReset=false'80 elif 'reset_app' in scenario.tags:81 os.environ['TOOLIUM_APPIUMCAPABILITIES_NORESET'] = 'AppiumCapabilities_noReset=false'82 os.environ['TOOLIUM_APPIUMCAPABILITIES_FULLRESET'] = 'AppiumCapabilities_fullReset=false'83 elif 'full_reset_app' in scenario.tags:84 os.environ['TOOLIUM_APPIUMCAPABILITIES_NORESET'] = 'AppiumCapabilities_noReset=false'85 os.environ['TOOLIUM_APPIUMCAPABILITIES_FULLRESET'] = 'AppiumCapabilities_fullReset=true'86 # Force to reset driver before each scenario if it has @reset_driver tag87 if 'reset_driver' in scenario.tags:88 DriverWrappersPool.stop_drivers()89 DriverWrappersPool.download_videos('multiple tests', context.global_status['test_passed'])90 DriverWrappersPool.save_all_ggr_logs('multiple tests', context.global_status['test_passed'])91 DriverWrappersPool.remove_drivers()92 context.global_status['test_passed'] = True93 # Skip android_only or ios_only scenarios94 if 'android_only' in scenario.tags and context.driver_wrapper.is_ios_test():95 scenario.skip('Android scenario')96 return97 elif 'ios_only' in scenario.tags and context.driver_wrapper.is_android_test():98 scenario.skip('iOS scenario')99 return100 # Read @no_driver tag101 no_driver = 'no_driver' in scenario.tags or 'no_driver' in scenario.feature.tags102 bdd_common_before_scenario(context, scenario, no_driver)103 # Behave dynamic environment104 context.dyn_env.execute_before_scenario_steps(context)105def bdd_common_before_scenario(context_or_world, scenario, no_driver=False):106 """Common scenario initialization in behave or lettuce107 :param context_or_world: behave context or lettuce world108 :param scenario: running scenario109 :param no_driver: True if this is an api test and driver should not be started110 """111 # Initialize and connect driver wrapper112 start_driver(context_or_world, no_driver)113 # Add assert screenshot methods with scenario configuration114 add_assert_screenshot_methods(context_or_world, scenario)115 # Configure Jira properties116 save_jira_conf()117 context_or_world.logger.info("Running new scenario: %s", scenario.name)118def create_and_configure_wrapper(context_or_world):119 """Create and configure driver wrapper in behave or lettuce tests120 :param context_or_world: behave context or lettuce world121 """122 # Create default driver wrapper123 context_or_world.driver_wrapper = DriverWrappersPool.get_default_wrapper()124 context_or_world.utils = context_or_world.driver_wrapper.utils125 # Get behave userdata properties to override config properties126 try:127 behave_properties = context_or_world.config.userdata128 except AttributeError:129 behave_properties = None130 # Configure wrapper131 context_or_world.driver_wrapper.configure(context_or_world.config_files, behave_properties=behave_properties)132 # Copy config object133 context_or_world.toolium_config = context_or_world.driver_wrapper.config134 # Configure logger135 context_or_world.logger = logging.getLogger(__name__)136def connect_wrapper(context_or_world):137 """Connect driver in behave or lettuce tests138 :param context_or_world: behave context or lettuce world139 """140 # Create driver if it is not already created141 if context_or_world.driver_wrapper.driver:142 context_or_world.driver = context_or_world.driver_wrapper.driver143 else:144 context_or_world.driver = context_or_world.driver_wrapper.connect()145 # Copy app_strings object146 context_or_world.app_strings = context_or_world.driver_wrapper.app_strings147def add_assert_screenshot_methods(context_or_world, scenario):148 """Add assert screenshot methods to behave or lettuce object149 :param context_or_world: behave context or lettuce world150 :param scenario: running scenario151 """152 file_suffix = scenario.name153 def assert_screenshot(element_or_selector, filename, threshold=0, exclude_elements=[], driver_wrapper=None,154 force=False):155 VisualTest(driver_wrapper, force).assert_screenshot(element_or_selector, filename, file_suffix, threshold,156 exclude_elements)157 def assert_full_screenshot(filename, threshold=0, exclude_elements=[], driver_wrapper=None, force=False):158 VisualTest(driver_wrapper, force).assert_screenshot(None, filename, file_suffix, threshold, exclude_elements)159 # Monkey patching assert_screenshot method in PageElement to use the correct test name160 def assert_screenshot_page_element(self, filename, threshold=0, exclude_elements=[], force=False):161 VisualTest(self.driver_wrapper, force).assert_screenshot(self.web_element, filename, file_suffix, threshold,162 exclude_elements)163 context_or_world.assert_screenshot = assert_screenshot164 context_or_world.assert_full_screenshot = assert_full_screenshot165 PageElement.assert_screenshot = assert_screenshot_page_element166def after_scenario(context, scenario):167 """Clean method that will be executed after each scenario168 :param context: behave context169 :param scenario: running scenario170 """171 bdd_common_after_scenario(context, scenario, scenario.status)172def bdd_common_after_scenario(context_or_world, scenario, status):173 """Clean method that will be executed after each scenario in behave or lettuce174 :param context_or_world: behave context or lettuce world175 :param scenario: running scenario176 :param status: scenario status (passed, failed or skipped)177 """178 if status == 'skipped':179 return180 elif status == 'passed':181 test_status = 'Pass'182 test_comment = None183 context_or_world.logger.info("The scenario '%s' has passed", scenario.name)184 else:185 test_status = 'Fail'186 test_comment = "The scenario '%s' has failed" % scenario.name187 context_or_world.logger.error("The scenario '%s' has failed", scenario.name)188 context_or_world.global_status['test_passed'] = False189 # Close drivers190 DriverWrappersPool.close_drivers(scope='function', test_name=scenario.name, test_passed=status == 'passed',191 context=context_or_world)192 # Save test status to be updated later193 add_jira_status(get_jira_key_from_scenario(scenario), test_status, test_comment)194def get_jira_key_from_scenario(scenario):195 """Extract Jira Test Case key from scenario tags.196 Two tag formats are allowed:197 @jira('PROJECT-32')198 @jira=PROJECT-32199 :param scenario: behave scenario200 :returns: Jira test case key201 """202 jira_regex = re.compile(r'jira[=\(\']*([A-Z]+\-[0-9]+)[\'\)]*$')203 for tag in scenario.tags:204 match = jira_regex.search(tag)205 if match:206 return match.group(1)207 return None208def after_feature(context, feature):209 """Clean method that will be executed after each feature210 :param context: behave context211 :param feature: running feature212 """213 # Behave dynamic environment214 context.dyn_env.execute_after_feature_steps(context)215 # Close drivers216 DriverWrappersPool.close_drivers(scope='module', test_name=feature.name,217 test_passed=context.global_status['test_passed'])218def after_all(context):219 """Clean method that will be executed after all features are finished220 :param context: behave context221 """222 bdd_common_after_all(context)223def bdd_common_after_all(context_or_world):224 """Common after all method in behave or lettuce225 :param context_or_world: behave context or lettuce world226 """227 # Close drivers228 DriverWrappersPool.close_drivers(scope='session', test_name='multiple_tests',229 test_passed=context_or_world.global_status['test_passed'])230 # Update tests status in Jira231 change_all_jira_status()232def start_driver(context, no_driver):233 """Start driver with configured values234 :param context: behave context235 :param no_driver: True if this is an api test and driver should not be started236 """237 create_and_configure_wrapper(context)238 if not no_driver:...

Full Screen

Full Screen

test_environment.py

Source:test_environment.py Github

copy

Full Screen

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',...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run toolium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful