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 test_capture(mocker): driver = MockWebDriver("cat.png") mocker.patch("webtraversallibrary.javascript.JavascriptWrapper.get_full_height", return_value=2048) mocker.patch( "webtraversallibrary.javascript.JavascriptWrapper.find_viewport", return_value=wtl.Rectangle(wtl.Point(0, 0), wtl.Point(256, 256)), ) mocker.patch("webtraversallibrary.javascript.JavascriptWrapper.hide_position_fixed_elements", return_value={}) result = Screenshot.capture("testing", driver, max_page_height=900) reference = Image.open(ORIGINAL_DIR / "page.png") assert result.name == "testing" assert equal_images(result.image, reference)
def test_crop_image(): img = Image.open(ORIGINAL_DIR / "cat.png") rect = wtl.Rectangle(wtl.Point(50, 70), wtl.Point(130, 170)) result = crop_image(img, rect) reference = Image.open(ORIGINAL_DIR / "crop.png") assert equal_images(result, reference) with pytest.raises(ValueError): rect = wtl.Rectangle.empty() crop_image(img, rect) with pytest.raises(ValueError): rect = wtl.Rectangle(wtl.Point(257, 257), wtl.Point(270, 280)) crop_image(img, rect)
def test_javascript_wrapper(): driver = MockWebDriver() js = JavascriptWrapper(driver) result = js.get_full_height() assert result == 1 result = js.find_iframe_name("id") assert result == 2 result = js.get_element_metadata() assert result == 3 result = js.hide_position_fixed_elements() assert result == 4 js.show_position_fixed_elements(None) assert driver.calls == 5 js.disable_animations() assert driver.calls == 6 result = js.is_page_loaded() assert result == 7 result = js.find_active_elements() assert result == 8 js.scroll_to(0, 0) assert driver.calls == 9 js.make_canvas() assert driver.calls == 10 js.annotate(wtl.Point(0, 0), wtl.Color.from_str("FFFFFF"), 0, "") assert driver.calls == 12 js.highlight(wtl.Selector(""), wtl.Color.from_str("FFFFFF")) assert driver.calls == 14 js.clear_highlights() assert driver.calls == 15 js.click_element(wtl.Selector("")) assert driver.calls == 16 js.delete_element(wtl.Selector("")) assert driver.calls == 17 js.fill_text(wtl.Selector(""), "") assert driver.calls == 18 js.select(wtl.Selector(""), "") assert driver.calls == 19 driver.to_return = {"x": 1, "y": 2, "w": 3, "h": 4} result = js.find_viewport() assert result.bounds == (1, 2, 4, 6) assert driver.calls == 20
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 get_element(mouse): # pylint: disable=inconsistent-return-statements """ Look for the element at current coords with smallest bounds """ point = wtl.Point(mouse.x - 5, mouse.y - 5) with data_lock: if not current_view: return smallest_element, smallest_area = None, 999999 for e in current_view.snapshot.elements: if point in e.bounds and e.bounds.area < smallest_area: smallest_area, smallest_element = e.bounds.area, e return smallest_element
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