def _do_visit(self): with Sniffer(path=self.job.pcap_file, filter=cm.DEFAULT_FILTER, device=self.device, dumpcap_log=self.job.pcap_log): sleep(1) # make sure dumpcap is running try: screenshot_count = 0 with ut.timeout(cm.HARD_VISIT_TIMEOUT): # begin loading page self.driver.get(self.job.url) sleep(1) # sleep to catch some lingering AJAX-type traffic # take first screenshot if self.screenshots: try: self.driver.get_screenshot_as_file( self.job.png_file(screenshot_count)) screenshot_count += 1 except WebDriverException: wl_log.error("Cannot get screenshot.") except (cm.HardTimeoutException, TimeoutException): wl_log.error("Visit to %s reached hard timeout!", self.job.url) except Exception as exc: wl_log.error("Unknown exception: %s", exc)
def test_cancel_timeout(self): try: with ut.timeout(1): sleep(0.9) except ut.TimeoutException: self.fail("Cannot cancel timeout") else: pass # test passes
def test_timeout(self): try: with ut.timeout(1): sleep(1.1) except ut.TimeoutException: pass # test passes else: self.fail("Cannot set timeout")
def close_all_streams(self): """Close all streams of a controller.""" print("Closing all streams") try: with ut.timeout(cm.STREAM_CLOSE_TIMEOUT): for stream in self.controller.get_streams(): print("Closing stream %s %s %s " % (stream.id, stream.purpose, stream.target_address)) self.controller.close_stream(stream.id) # MISC reason except ut.TimeoutException: print("Closing streams timed out!") except: print("Exception closing stream")
def _do_visit(self): with Sniffer(path=self.job.pcap_file, filter=cm.DEFAULT_FILTER, device=self.device, dumpcap_log=self.job.pcap_log): sleep(1) # make sure dumpcap is running try: screenshot_count = 0 with ut.timeout(cm.HARD_VISIT_TIMEOUT): # begin loading page self.driver.get(self.job.url) # take first screenshot if self.screenshots: try: self.driver.get_screenshot_as_file( self.job.png_file(screenshot_count)) screenshot_count += 1 except WebDriverException: wl_log.error("Cannot get screenshot.") # check video player status status_to_string = [ 'ended', 'played', 'paused', 'buffered', 'queued', 'unstarted' ] js = "return document.getElementById('movie_player').getPlayerState()" player_status = self.driver.execute_script(js) # continue visit capture until video is has fully played ts = time() while player_status != 0: # attempt to simulate user skipping add if player_status == -1: try: skipAds = self.driver.find_elements( By.XPATH, "//button[@class=\"ytp-ad-skip-button ytp-button\"]" ) wl_log.info(len(skipAds)) for skipAd in skipAds: skipAd.click() except WebDriverException as e: pass # unpause video if state is unstarted or is for some reason paused if player_status == -1 or player_status == 2: self.driver.execute_script( "return document.getElementById('movie_player').playVideo()" ) # busy loop delay sleep(1) # check video state again new_ps = self.driver.execute_script(js) # print progress updates every time the video state changes # or on the screenshot interval ts_new = time() if player_status != new_ps or ts_new - ts > cm.SCREENSHOT_INTERVAL: wl_log.debug( 'youtube status: {} for {:.2f} seconds'.format( status_to_string[player_status], ts_new - ts)) ts = ts_new # take periodic screenshots if self.screenshots: try: self.driver.get_screenshot_as_file( self.job.png_file(screenshot_count)) screenshot_count += 1 except WebDriverException: wl_log.error("Cannot get screenshot.") player_status = new_ps except (cm.HardTimeoutException, TimeoutException): wl_log.error("Visit to %s reached hard timeout!", self.job.url) except Exception as exc: wl_log.error("Unknown exception: %s", exc)