How to use __set_last_page_screenshot method in SeleniumBase

Best Python code snippet using SeleniumBase

base_case.py

Source:base_case.py Github

copy

Full Screen

...2924 switch_to=True,2925 cap_file=self.cap_file,2926 disable_csp=self.disable_csp)2927 self._default_driver = self.driver2928 def __set_last_page_screenshot(self):2929 """ self.__last_page_screenshot is only for pytest html report logs2930 self.__last_page_screenshot_png is for all screenshot log files """2931 if not self.__last_page_screenshot and (2932 not self.__last_page_screenshot_png):2933 try:2934 element = self.driver.find_element_by_tag_name('body')2935 if self.is_pytest and self.report_on:2936 self.__last_page_screenshot_png = (2937 self.driver.get_screenshot_as_png())2938 self.__last_page_screenshot = element.screenshot_as_base642939 else:2940 self.__last_page_screenshot_png = element.screenshot_as_png2941 except Exception:2942 if not self.__last_page_screenshot:2943 if self.is_pytest and self.report_on:2944 try:2945 self.__last_page_screenshot = (2946 self.driver.get_screenshot_as_base64())2947 except Exception:2948 pass2949 if not self.__last_page_screenshot_png:2950 try:2951 self.__last_page_screenshot_png = (2952 self.driver.get_screenshot_as_png())2953 except Exception:2954 pass2955 def __insert_test_result(self, state, err):2956 data_payload = TestcaseDataPayload()2957 data_payload.runtime = int(time.time() * 1000) - self.case_start_time2958 data_payload.guid = self.testcase_guid2959 data_payload.execution_guid = self.execution_guid2960 data_payload.state = state2961 if err:2962 import traceback2963 tb_string = traceback.format_exc()2964 if "Message: " in tb_string:2965 data_payload.message = "Message: " + tb_string.split(2966 "Message: ")[-1]2967 elif "Exception: " in tb_string:2968 data_payload.message = tb_string.split("Exception: ")[-1]2969 elif "Error: " in tb_string:2970 data_payload.message = tb_string.split("Error: ")[-1]2971 else:2972 data_payload.message = "Unknown Error: See Stacktrace"2973 self.testcase_manager.update_testcase_data(data_payload)2974 def __add_pytest_html_extra(self):2975 try:2976 if self.with_selenium:2977 if not self.__last_page_screenshot:2978 self.__set_last_page_screenshot()2979 if self.report_on:2980 extra_url = {}2981 extra_url['name'] = 'URL'2982 extra_url['format'] = 'url'2983 extra_url['content'] = self.get_current_url()2984 extra_url['mime_type'] = None2985 extra_url['extension'] = None2986 extra_image = {}2987 extra_image['name'] = 'Screenshot'2988 extra_image['format'] = 'image'2989 extra_image['content'] = self.__last_page_screenshot2990 extra_image['mime_type'] = 'image/png'2991 extra_image['extension'] = 'png'2992 self._html_report_extra.append(extra_url)2993 self._html_report_extra.append(extra_image)2994 except Exception:2995 pass2996 def __quit_all_drivers(self):2997 # Close all open browser windows2998 self._drivers_list.reverse() # Last In, First Out2999 for driver in self._drivers_list:3000 try:3001 driver.quit()3002 except AttributeError:3003 pass3004 except Exception:3005 pass3006 self.driver = None3007 self._drivers_list = []3008 def tearDown(self):3009 """3010 Be careful if a subclass of BaseCase overrides setUp()3011 You'll need to add the following line to the subclass's tearDown():3012 super(SubClassOfBaseCase, self).tearDown()3013 """3014 has_exception = False3015 if sys.version.startswith('3') and hasattr(self, '_outcome'):3016 if hasattr(self._outcome, 'errors') and self._outcome.errors:3017 has_exception = True3018 else:3019 has_exception = sys.exc_info()[1] is not None3020 if self.__delayed_assert_failures:3021 print(3022 "\nWhen using self.delayed_assert_*() methods in your tests, "3023 "remember to call self.process_delayed_asserts() afterwards. "3024 "Now calling in tearDown()...\nFailures Detected:")3025 if not has_exception:3026 self.process_delayed_asserts()3027 else:3028 self.process_delayed_asserts(print_only=True)3029 self.is_pytest = None3030 try:3031 # This raises an exception if the test is not coming from pytest3032 self.is_pytest = sb_config.is_pytest3033 except Exception:3034 # Not using pytest (probably nosetests)3035 self.is_pytest = False3036 if self.is_pytest:3037 # pytest-specific code3038 test_id = "%s.%s.%s" % (self.__class__.__module__,3039 self.__class__.__name__,3040 self._testMethodName)3041 try:3042 with_selenium = self.with_selenium3043 except Exception:3044 sub_class_name = str(3045 self.__class__.__bases__[0]).split('.')[-1].split("'")[0]3046 sub_file_name = str(self.__class__.__bases__[0]).split('.')[-2]3047 sub_file_name = sub_file_name + ".py"3048 class_name = str(self.__class__).split('.')[-1].split("'")[0]3049 file_name = str(self.__class__).split('.')[-2] + ".py"3050 class_name_used = sub_class_name3051 file_name_used = sub_file_name3052 if sub_class_name == "BaseCase":3053 class_name_used = class_name3054 file_name_used = file_name3055 fix_setup = "super(%s, self).setUp()" % class_name_used3056 fix_teardown = "super(%s, self).tearDown()" % class_name_used3057 message = ("You're overriding SeleniumBase's BaseCase setUp() "3058 "method with your own setUp() method, which breaks "3059 "SeleniumBase. You can fix this by going to your "3060 "%s class located in your %s file and adding the "3061 "following line of code AT THE BEGINNING of your "3062 "setUp() method:\n%s\n\nAlso make sure "3063 "you have added the following line of code AT THE "3064 "END of your tearDown() method:\n%s\n"3065 % (class_name_used, file_name_used,3066 fix_setup, fix_teardown))3067 raise Exception(message)3068 if with_selenium:3069 # Save a screenshot if logging is on when an exception occurs3070 if has_exception:3071 self.__add_pytest_html_extra()3072 if self.with_testing_base and not has_exception and (3073 self.save_screenshot_after_test):3074 test_logpath = self.log_path + "/" + test_id3075 if not os.path.exists(test_logpath):3076 try:3077 os.makedirs(test_logpath)3078 except Exception:3079 pass # Only reachable during multi-threaded runs3080 if not self.__last_page_screenshot_png:3081 self.__set_last_page_screenshot()3082 log_helper.log_screenshot(3083 test_logpath,3084 self.driver,3085 self.__last_page_screenshot_png)3086 self.__add_pytest_html_extra()3087 if self.with_testing_base and has_exception:3088 test_logpath = self.log_path + "/" + test_id3089 if not os.path.exists(test_logpath):3090 try:3091 os.makedirs(test_logpath)3092 except Exception:3093 pass # Only reachable during multi-threaded runs3094 if ((not self.with_screen_shots) and (3095 not self.with_basic_test_info) and (3096 not self.with_page_source)):3097 # Log everything if nothing specified (if testing_base)3098 if not self.__last_page_screenshot_png:3099 self.__set_last_page_screenshot()3100 log_helper.log_screenshot(3101 test_logpath,3102 self.driver,3103 self.__last_page_screenshot_png)3104 log_helper.log_test_failure_data(3105 self, test_logpath, self.driver, self.browser)3106 log_helper.log_page_source(test_logpath, self.driver)3107 else:3108 if self.with_screen_shots:3109 if not self.__last_page_screenshot_png:3110 self.__set_last_page_screenshot()3111 log_helper.log_screenshot(3112 test_logpath,3113 self.driver,3114 self.__last_page_screenshot_png)3115 if self.with_basic_test_info:3116 log_helper.log_test_failure_data(3117 self, test_logpath, self.driver, self.browser)3118 if self.with_page_source:3119 log_helper.log_page_source(3120 test_logpath, self.driver)3121 # (Pytest) Finally close all open browser windows3122 self.__quit_all_drivers()3123 if self.headless:3124 if self.headless_active:3125 self.display.stop()3126 self.display = None3127 if self.with_db_reporting:3128 if has_exception:3129 self.__insert_test_result(constants.State.ERROR, True)3130 else:3131 self.__insert_test_result(constants.State.PASS, False)3132 runtime = int(time.time() * 1000) - self.execution_start_time3133 self.testcase_manager.update_execution_data(3134 self.execution_guid, runtime)3135 if self.with_s3_logging and has_exception:3136 """ If enabled, upload logs to S3 during test exceptions. """3137 from seleniumbase.core.s3_manager import S3LoggingBucket3138 s3_bucket = S3LoggingBucket()3139 guid = str(uuid.uuid4().hex)3140 path = "%s/%s" % (self.log_path, test_id)3141 uploaded_files = []3142 for logfile in os.listdir(path):3143 logfile_name = "%s/%s/%s" % (guid,3144 test_id,3145 logfile.split(path)[-1])3146 s3_bucket.upload_file(logfile_name,3147 "%s/%s" % (path, logfile))3148 uploaded_files.append(logfile_name)3149 s3_bucket.save_uploaded_file_names(uploaded_files)3150 index_file = s3_bucket.upload_index_file(test_id, guid)3151 print("\n\n*** Log files uploaded: ***\n%s\n" % index_file)3152 logging.error(3153 "\n\n*** Log files uploaded: ***\n%s\n" % index_file)3154 if self.with_db_reporting:3155 self.testcase_manager = TestcaseManager(self.database_env)3156 data_payload = TestcaseDataPayload()3157 data_payload.guid = self.testcase_guid3158 data_payload.logURL = index_file3159 self.testcase_manager.update_testcase_log_url(data_payload)3160 else:3161 # (Nosetests)3162 if has_exception:3163 test_id = "%s.%s.%s" % (self.__class__.__module__,3164 self.__class__.__name__,3165 self._testMethodName)3166 test_logpath = "latest_logs/" + test_id3167 if not os.path.exists(test_logpath):3168 try:3169 os.makedirs(test_logpath)3170 except Exception:3171 pass # Only reachable during multi-threaded runs3172 log_helper.log_test_failure_data(3173 self, test_logpath, self.driver, self.browser)3174 if len(self._drivers_list) > 0:3175 if not self.__last_page_screenshot_png:3176 self.__set_last_page_screenshot()3177 log_helper.log_screenshot(3178 test_logpath,3179 self.driver,3180 self.__last_page_screenshot_png)3181 log_helper.log_page_source(test_logpath, self.driver)3182 elif self.save_screenshot_after_test:3183 test_id = "%s.%s.%s" % (self.__class__.__module__,3184 self.__class__.__name__,3185 self._testMethodName)3186 test_logpath = "latest_logs/" + test_id3187 if not os.path.exists(test_logpath):3188 try:3189 os.makedirs(test_logpath)3190 except Exception:3191 pass # Only reachable during multi-threaded runs3192 if not self.__last_page_screenshot_png:3193 self.__set_last_page_screenshot()3194 log_helper.log_screenshot(3195 test_logpath,3196 self.driver,3197 self.__last_page_screenshot_png)3198 if self.report_on:3199 self._last_page_screenshot = self.__last_page_screenshot_png3200 try:3201 self._last_page_url = self.get_current_url()3202 except Exception:3203 self._last_page_url = "(Error: Unknown URL)"3204 # Finally close all open browser windows...

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 SeleniumBase 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