def debug_on(mode='draw'): """Start debugger with drawing mode and set default timeout down to 2 sec. Examples -------- .. code-block:: robotframework debugon #Start debugger and set timeout to 2sec. debugon debug #Start debugger with SearchMode debug and set timeout to 2sec. Parameters ---------- mode : str debug(default) = Highlight(blink) element without executing kw draw = Highlight element and execute kw """ dbg = DebugLibrary() global cur_mode # pylint: disable=global-statement global cur_timeout # pylint: disable=global-statement cur_mode = CONFIG.get_value('SearchMode') cur_timeout = CONFIG.get_value('DefaultTimeout') CONFIG.set_value('SearchMode', mode) CONFIG.set_value('DefaultTimeout', 2) CONFIG.set_value('Debug_Run', True) dbg.debug()
def get_config(par=None): """Return value of given configuration parameter. If no parameter is given the GetConfig returns all configurations in a python dictionary of current configuration parameter names and their values. Examples -------- .. code-block:: robotframework ${VAL} GetConfig default timeout # Return default timeout value ${VAL} GetConfig # Return all config parameter names and values Parameters ---------- par : str Setting to be fetched """ if par: if not CONFIG.is_value(par): raise ValueError("Parameter {} doesn't exist".format(par)) # Return single configuration value current_config = CONFIG.get_value(par) else: # return whole configuration dictionary current_config = CONFIG.get_all_values() return current_config
def reset_config(par=None): """Reset the value of given parameter to default value. If no parameter is given, reset all parameters configuration parameters to their defaults. Reset also returns the value of the given configuration parameter. If no parameter is given, the ResetConfig returns all configurations in a python dictionary with configuration parameter name and their values. Examples -------- .. code-block:: robotframework ${VAL} ResetConfig default timeout # resets single parameter, and returns value ${VAL} ResetConfig # Resets all parameters, and returns config """ if par: if not CONFIG.is_value(par): raise ValueError("Parameter {} doesn't exist".format(par)) CONFIG.reset_value(par) # Return single configuration value current_config = CONFIG.get_value(par) else: CONFIG.reset_value() # return whole configuration dictionary current_config = CONFIG.get_all_values() return current_config
def reset_config(par=None): r"""Reset the value of given parameter to default value. If no parameter is given, reset all parameters configuration parameters to their defaults. Reset also returns the value of the given configuration parameter. If no parameter is given, the ResetConfig returns all configurations in a python dictionary with configuration parameter name and their values. Examples -------- .. code-block:: robotframework ${VAL} ResetConfig default timeout # resets single parameter, and returns value ${VAL} ResetConfig # Resets all parameters, and returns config Related keywords ---------------- \`GetConfig\`, \`SetConfig\` """ if par: if not CONFIG.is_value(par): raise ValueError("Parameter {} doesn't exist".format(par)) CONFIG.reset_value(par) # if case insensitive was reset, reset xpath if par.lower() == "caseinsensitive": CONFIG.reset_value("ContainingTextMatch") # Return single configuration value current_config = CONFIG.get_value(par) else: CONFIG.reset_value() # return whole configuration dictionary current_config = CONFIG.get_all_values() return current_config
def maximize_window(): """Maximizes current browser window. Note: This keyword will not fail if maximizing is prevented for some reason. This can happen for example if window manager is not installed or setup correctly. Examples -------- .. code-block:: robotframework MaximizeWindow Parameters ---------- None """ driver = browser.get_current_browser() if driver is None: raise QWebDriverError("No browser open. Use OpenBrowser keyword" " to open browser first") if CONFIG.get_value('Headless') is True: logger.debug("Maximizing browser in headless mode") screen_width_js = driver.execute_script("return screen.width") screen_height_js = driver.execute_script("return screen.height") driver.set_window_size(screen_width_js, screen_height_js) else: driver.maximize_window() size = driver.get_window_size() logger.debug("Window size set to {}x{}".format(size["width"], size["height"]))
def click_icon(image, template_res_w=None, browser_res_w=None, timeout=0): # pylint: disable=unused-argument r"""Click the icon on the screen. In case you want to click icons you always have to have reference images. If reference picture are not in default folders (images, files, downloads) then BASE_IMAGE_PATH should be defined in a robot file before using this keyword Examples -------- .. code-block:: robotframework *** Variables *** ${BASE_IMAGE_PATH} ${CURDIR}${/}..${/}resources${/}images BASE_IMAGE_PATH should lead to the folder where all your reference icons are .. code-block:: robotframework ClickIcon plane Related keywords ---------------- \`ClickCell\`, \`ClickCheckbox\`, \`ClickElement\`, \`ClickItem\`, \`ClickList\`, \`ClickText\`, \`ClickUntil\`, \`ClickWhile\`, \`VerifyIcon\` """ if not browser_res_w: browser_res_w = util.get_monitor_width( ) # pyautogui works on whole screen # use current resolution by default if not template_res_w: template_res_w = browser_res_w template_res_w, browser_res_w = int(template_res_w), int(browser_res_w) image_path = icon.get_full_image_path(image) x, y = icon.image_recognition(image_path, template_res_w, browser_res_w, pyautog=True) if x == -1: raise QWebElementNotFoundError( "Couldn't find the icon from the screen") if CONFIG.get_value("RetinaDisplay"): x = x * 0.5 y = y * 0.5 pyautogui.moveTo(x, y) pyautogui.click(x, y)
def _log_matched_image(haystack, needle, scaled_needle, loc, best_scale): """Draw a composite image with the needle image, the haystack image, the scaled needle that matches the best and show where in haystack the best match is. This is best used in debugging, but it could be modified to add the image to the Robot log as well. """ needle = cv2.cvtColor(needle, cv2.COLOR_GRAY2BGR) scaled_needle = cv2.cvtColor(scaled_needle, cv2.COLOR_GRAY2BGR) h1, w1 = needle.shape[:2] hs, ws = scaled_needle.shape[:2] h2, w2 = haystack.shape[:2] max_left_w = max(w1, ws) cv2.rectangle(haystack, (loc[0] - int(w1 / 2 * best_scale), loc[1] - int(h1 / 2 * best_scale)), (loc[0] + int(w1 / 2 * best_scale), loc[1] + int(h1 / 2 * best_scale)), (0, 0, 255), 2) result = np.zeros((max(h2, h1), w2 + max_left_w, 3), np.uint8) result[:h1, :w1, :3] = needle result[h1:h1 + hs, :ws, :3] = scaled_needle result[:h2, max_left_w:max_left_w + w2, :3] = haystack cv2.line(result, (ws, h1), (loc[0] + max_left_w + int(ws / 2), loc[1] - int(hs / 2)), (0, 0, 255), 2) cv2.line(result, (0, h1 + hs), (loc[0] + max_left_w - int(ws / 2), loc[1] + int(hs / 2)), (0, 0, 255), 2) if CONFIG.get_value("LogMatchedIcons"): output = BuiltIn().get_variable_value('${OUTPUT DIR}') filename = "temp_matched_image-{}".format(uuid4()) + ".png" filepath = os.path.join(output, SCREEN_SHOT_DIR_NAME, filename) cv2.imwrite(filepath, result) log_screenshot_file(filepath)
def image_location(self, needle, haystack, tolerance=0.95, draw=1, template_res_w=1440, device_res_w=1080): """Locate an image (needle) within an bigger image (haystack). Tolarance is pixel tolerance, i.e. 1.0 = all pixels are correct, 0.5 = 50% of the pixels are correct. If we know the original resolution, from which the template image is coming, we can supply it as template_res_w. Return value is the central (x,y) of the first image found. Draw function will plot red lines where needle image is found. """ print("*INFO* _image_location Starts") image = cv2.imread(haystack) image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) _hay_h, hay_w = image_gray.shape[:2] needle_path = Path(needle) if not needle_path.exists(): raise Exception("Needle file does not exist. Tried: {}".format(needle_path)) template = cv2.imread(str(needle_path.resolve()), 0) if template is None: raise Exception("Cannot read template image. Tried: {}".format(needle)) height, width = template.shape[:2] scale_ratios = self._get_scale_ratios(template_res_w, device_res_w) print("*DEBUG* Scale ratios to be used in order: {}".format(scale_ratios)) best_highest_max_val = 0.0 best_highest_max_val_loc = (-1, -1) best_scale_ratio = None best_matched_image = None print("*DEBUG* Resampling loop Starts") for scale_ratio in scale_ratios: interpolation_method = cv2.INTER_LINEAR if scale_ratio > 1.0 else cv2.INTER_AREA print(("*DEBUG* resize starts: for scale {}".format(scale_ratio))) if math.isclose(scale_ratio, 1.0, rel_tol=0.03): scaled_img_template = template else: scaled_img_template = cv2.resize(template, None, fx=scale_ratio, fy=scale_ratio, interpolation=interpolation_method) print("*DEBUG* matchTemplate Starts:") res = cv2.matchTemplate(image_gray, scaled_img_template, cv2.TM_CCOEFF_NORMED) ratio = device_res_w / hay_w if CONFIG.get_value("RetinaDisplay"): ratio = ratio * 2 elif ratio < 1.1: ratio = 1.0 print("*DEBUG* _extract_points Starts:") _current_points, highest_max_val, highest_max_val_loc = \ self._extract_points(height * scale_ratio, res, tolerance, width * scale_ratio, ratio) if highest_max_val > best_highest_max_val: best_highest_max_val = highest_max_val best_highest_max_val_loc = highest_max_val_loc best_scale_ratio = scale_ratio best_matched_image = scaled_img_template print("*DEBUG* Current best match location: {},\n" "max_value: {},\n" "scale_ratio: {}".format(best_highest_max_val_loc, best_highest_max_val, best_scale_ratio)) if best_highest_max_val > tolerance: if draw == 1: loc = np.where(res >= tolerance) for pt in zip(*loc[::-1]): cv2.rectangle(image, pt, (pt[0] + width, pt[1] + height), (0, 0, 255), 2) cv2.imwrite('temp_matched_area.png', image) break print("*DEBUG* Ready to return points:") print("*DEBUG* Best match location: {}, best correlation value: {}, " "best scale ratio: {}".format(best_highest_max_val_loc, best_highest_max_val, best_scale_ratio)) self._log_matched_image(image, template, best_matched_image, best_highest_max_val_loc, best_scale_ratio) if best_highest_max_val >= tolerance: return best_highest_max_val_loc return -1, -1