def verify_request(method, url, post_data=None, headers={}): parsed_url = urllib.parse.urlparse(url) user_agent = str(syn_webdriver.get_canary_user_agent_string()) if "User-Agent" in headers: headers["User-Agent"] = " ".join([user_agent, headers["User-Agent"]]) else: headers["User-Agent"] = "{}".format(user_agent) logger.info("Making request with Method: '%s' URL: %s: Data: %s Headers: %s" % ( method, url, json.dumps(post_data), json.dumps(headers))) if parsed_url.scheme == "https": conn = http.client.HTTPSConnection(parsed_url.hostname, parsed_url.port) else: conn = http.client.HTTPConnection(parsed_url.hostname, parsed_url.port) conn.request(method, url, str(post_data), headers) response = conn.getresponse() logger.info("Status Code: %s " % response.status) logger.info("Response Headers: %s" % json.dumps(response.headers.as_string())) if not response.status or response.status < 200 or response.status > 299: try: logger.error("Response: %s" % response.read().decode()) finally: if response.reason: conn.close() raise Exception("Failed: %s" % response.reason) else: conn.close() raise Exception("Failed with status code: %s" % response.status) logger.info("Response: %s" % response.read().decode()) logger.info("HTTP request successfully executed") conn.close()
def main(): url = 'https://example.com/' method = 'GET' postData = "" headers = {} verify_request(method, url, None, headers) logger.info("Canary successfully executed")
def navigate_to_dashboard(): logger.info( "### Navigate to the specific CloudWatch dashboard we are interested into" ) region = os.environ['AWS_REGION'] dashboard = os.environ['DASHBOARD'] dashboard_url = f'https://console.aws.amazon.com/cloudwatch/home?region={region}#dashboards:name={dashboard}' browser.get(dashboard_url) # We need to wait for some of the controls to appear on the UI # otherwise, the adjust_dashboard_ui() step will fail as it will # not find the HTML elements it needs: time.sleep(10)
def clean_interface(): logger.info("### Remove clutter from the user interface") try: # Accept all cookies: browser.find_element_by_xpath( '//span[text()="Accept all"]').click() except: logger.info("!!! Accept all cookies button not found") # Polaris UI opt-in: try: browser.find_element_by_class_name( "console-polaris-opt-in-link").click() except: logger.info("!!! Polaris opt-in button not found") # Remove the blue ribbons with notifications appearing to the top: try: flash_elements_list = browser.find_element_by_class_name( "awsui-flash-dismiss") # If there are several notifications, we want to remove them all: if isinstance(flash_elements_list, list): for element in flash_elements_list: element.click() else: flash_elements_list.click() except: logger.info("!!! Notification ribbon not found")
def main(): url = "https://${health_check_url}" # Set screenshot option takeScreenshot = True browser = syn_webdriver.Chrome() browser.get(url) if takeScreenshot: browser.save_screenshot("loaded.png") response_code = syn_webdriver.get_http_response(url) if not response_code or response_code < 200 or response_code > 299: raise Exception("Failed to load page!") logger.info("Canary successfully executed")
def handler(event, context): # user defined log statements using synthetics_logger logger.info("Selenium Python heartbeat canary") return main()
def navigate_to_dashboards_home(): logger.info("### Navigate to the AWS CloudWatch dashboards list") browser.get(signin_url) time.sleep(5)
async def get_dashboard_snapshot(signin_url): # Without a user_agent, some console UI are not rendered properly: user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36' syn_webdriver = webdriver viewport_height = int(os.environ['VIEWPORT_HEIGHT']) # User agents *MUST* be set before instanciating the browser object: await syn_webdriver.add_user_agent(user_agent) browser = syn_webdriver.Chrome() browser.set_viewport_size(1920, viewport_height) # Set CloudWatch synthetics configuration: synthetics_configuration.set_config({ "screenshot_on_step_start": False, "screenshot_on_step_success": True, "screenshot_on_step_failure": False }) # Defines the different actions we want to execute in this canary: def navigate_to_dashboards_home(): logger.info("### Navigate to the AWS CloudWatch dashboards list") browser.get(signin_url) time.sleep(5) def clean_interface(): logger.info("### Remove clutter from the user interface") try: # Accept all cookies: browser.find_element_by_xpath( '//span[text()="Accept all"]').click() except: logger.info("!!! Accept all cookies button not found") # Polaris UI opt-in: try: browser.find_element_by_class_name( "console-polaris-opt-in-link").click() except: logger.info("!!! Polaris opt-in button not found") # Remove the blue ribbons with notifications appearing to the top: try: flash_elements_list = browser.find_element_by_class_name( "awsui-flash-dismiss") # If there are several notifications, we want to remove them all: if isinstance(flash_elements_list, list): for element in flash_elements_list: element.click() else: flash_elements_list.click() except: logger.info("!!! Notification ribbon not found") def navigate_to_dashboard(): logger.info( "### Navigate to the specific CloudWatch dashboard we are interested into" ) region = os.environ['AWS_REGION'] dashboard = os.environ['DASHBOARD'] dashboard_url = f'https://console.aws.amazon.com/cloudwatch/home?region={region}#dashboards:name={dashboard}' browser.get(dashboard_url) # We need to wait for some of the controls to appear on the UI # otherwise, the adjust_dashboard_ui() step will fail as it will # not find the HTML elements it needs: time.sleep(10) def adjust_dashboard_ui(): logger.info("### Close the left menu-bar and switch to full screen") try: browser.find_element_by_class_name( "awsui-app-layout__close-button").click() except Exception as e: logger.info("!!! Left menu bar close button not found") logger.info(e) try: # browser.find_element_by_class_name("awsui-flash-action-button").click() browser.find_element_by_xpath( "//*[text()='Execute them all']").click() except Exception as e: logger.info("!!! Allow lambda executions button not found") logger.info(e) try: browser.find_element_by_class_name("fullscreen-button").click() except Exception as e: logger.info("!!! Full screen button not found") logger.info(e) #browser.find_element_by_xpath('//awsui-button[@data-test-id="refresh-dashboard-button"]').click() dashboard_type = os.environ['DASHBOARD_TYPE'] if dashboard_type == 'ModelEvaluation': time.sleep(90) else: time.sleep(30) # Runs the action in sequence: await syn_webdriver.execute_step("navigateToDashboardsHome", navigate_to_dashboards_home) await syn_webdriver.execute_step("cleanUI", clean_interface) await syn_webdriver.execute_step("navigateToDashboard", navigate_to_dashboard) await syn_webdriver.execute_step("adjustDashboardUI", adjust_dashboard_ui) dashboard = os.environ['DASHBOARD'] dashboard_type = os.environ['DASHBOARD_TYPE'] stack_id = os.environ['STACK_ID'] browser.save_screenshot('screenshot.png', f'{dashboard}-{dashboard_type}-{stack_id}') logger.info("Canary successfully executed")
def adjust_dashboard_ui(): logger.info("### Close the left menu-bar and switch to full screen") try: browser.find_element_by_class_name( "awsui-app-layout__close-button").click() except Exception as e: logger.info("!!! Left menu bar close button not found") logger.info(e) try: # browser.find_element_by_class_name("awsui-flash-action-button").click() browser.find_element_by_xpath( "//*[text()='Execute them all']").click() except Exception as e: logger.info("!!! Allow lambda executions button not found") logger.info(e) try: browser.find_element_by_class_name("fullscreen-button").click() except Exception as e: logger.info("!!! Full screen button not found") logger.info(e) #browser.find_element_by_xpath('//awsui-button[@data-test-id="refresh-dashboard-button"]').click() dashboard_type = os.environ['DASHBOARD_TYPE'] if dashboard_type == 'ModelEvaluation': time.sleep(90) else: time.sleep(30)
def handler(event, context): logger.info("Selenium Python API canary") main()