def test_draw_text(): img = Image.open(ORIGINAL_DIR / "cat.png") draw_text(img, wtl.Point(10, 10), wtl.Color(50, 150, 250), 20, "This is a cat") draw_text(img, wtl.Point(50, 200), wtl.Color(0, 0, 0), 50, "Not a dog") reference = Image.open(ORIGINAL_DIR / "text.png") assert equal_images(img, reference)
def test_draw_rect(): img = Image.open(ORIGINAL_DIR / "cat.png") rect_1 = wtl.Rectangle(wtl.Point(40, 50), wtl.Point(70, 80)) rect_2 = wtl.Rectangle(wtl.Point(200, 210), wtl.Point(220, 230)) draw_rect(img, rect_1, wtl.Color(50, 150, 250), 5) draw_rect(img, rect_2, wtl.Color(250, 30, 30), 5) reference = Image.open(ORIGINAL_DIR / "rect.png") assert equal_images(img, reference)
def policy(_, view: wtl.View) -> List[wtl.Action]: # Do any random click return [ Clear(), Annotate( location=wtl.Point(30, 30), color=wtl.Color(0, 0, 0), size=20, text="Still dataguy", background=wtl.Color(128, 50, 128), ), choice(view.actions.by_type(Click)), ]
def policy(workflow: wtl.Workflow, view: wtl.View) -> wtl.Action: if "previous" not in view.metadata: view.metadata["previous"] = [] else: workflow.js.annotate(wtl.Point(100, 100), wtl.Color(0, 0, 0), 30, "This is an annotation", wtl.Color(128, 128, 128, 128)) if workflow.config.debug.screenshots: # Create screenshot of previous actions with an emphasis on the latest scr = view.snapshot.new_screenshot("history", of="full") for prev in view.metadata["previous"]: scr.highlight(prev.bounds, color=wtl.Color(255, 0, 0, 100)) scr.highlight(view.metadata["previous_action"][0].target.bounds, text="Latest action", color=wtl.Color(0, 0, 255, 100)) scr.save(workflow.output_path) # Save screenshot of the current live view workflow.scraper.capture_screenshot("live").save( workflow.output_path) # Get all elements tagged as "menu" menu_elements = view.snapshot.elements.by_score("menu") # Filter out those we have already clicked on menu_elements = [ e for e in menu_elements if e.metadata["text"] not in [e.metadata["text"] for e in view.metadata["previous"]] ] if menu_elements: # If there are any left, click that and remember its text element = choice(menu_elements) action = Click(element) view.metadata["previous"].append(element) else: # Otherwise, stop everything action = Abort() # Return print("Here are the buttons I've clicked: ", [e.metadata["text"] for e in view.metadata["previous"]]) print("Last time I did", view.metadata["previous_action"][0]) return action
def url_length_classifier_func(elements, _): # Score all elements with an href attribute with a score of the length of the href attribute href_elements = [ element for element in elements if element.metadata["href"] ] return [(element, len(element.metadata["href"])) for element in href_elements] if __name__ == "__main__": cli_args = parse_cli_args() workflow = wtl.Workflow(config=wtl.Config(cli_args.config), policy=policy, url=cli_args.url, output=cli_args.output) workflow.classifiers.add( wtl.ElementClassifier( name="url_length", highlight=True, mode=wtl.ScalingMode.LINEAR, highlight_color=wtl.Color(0, 0, 255), callback=url_length_classifier_func, )) workflow.run() workflow.quit()