-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathActionImage.java
More file actions
63 lines (49 loc) · 2.42 KB
/
ActionImage.java
File metadata and controls
63 lines (49 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package solutions.bellatrix.playwright.components;
import com.microsoft.playwright.Mouse;
import com.microsoft.playwright.options.MouseButton;
import lombok.Getter;
import lombok.Setter;
import org.openqa.selenium.InvalidArgumentException;
import solutions.bellatrix.core.plugins.EventListener;
import solutions.bellatrix.playwright.components.common.ComponentActionEventArgs;
import solutions.bellatrix.playwright.findstrategies.ImageBase64FindStrategy;
import solutions.bellatrix.playwright.infrastructure.PlaywrightService;
import solutions.bellatrix.plugins.opencv.OpenCvService;
import java.awt.*;
@Getter
@Setter
public class ActionImage extends WebComponent {
public final static EventListener<ComponentActionEventArgs> CLICKING = new EventListener<>();
public final static EventListener<ComponentActionEventArgs> HOVERING = new EventListener<>();
private final Mouse mouse = PlaywrightService.wrappedBrowser().getCurrentPage().mouse();
@Override
public Point getLocation() {
ImageBase64FindStrategy findStrategy;
try {
findStrategy = (ImageBase64FindStrategy)getFindStrategy();
} catch (ClassCastException e) {
throw new InvalidArgumentException("Invalid image base 64 format");
}
var encodedImage = findStrategy.getEncodedImage();
var location = OpenCvService.getLocation(encodedImage);
return new Point((int)location.x + encodedImage.getXOffset(), (int)location.y + encodedImage.getYOffset());
}
public void click() {
CLICKING.broadcast(new ComponentActionEventArgs(this, null, "Coordinates: %d, %d".formatted(getLocation().x, getLocation().y)));
mouse.click(getLocation().x, getLocation().y);
}
public void rightClick() {
CLICKING.broadcast(new ComponentActionEventArgs(this, null, "Coordinates: %d, %d".formatted(getLocation().x, getLocation().y)));
mouse.click(getLocation().x, getLocation().y, new Mouse.ClickOptions().setButton(MouseButton.RIGHT));
}
public void hover() {
HOVERING.broadcast(new ComponentActionEventArgs(this, null, "Coordinates: %d, %d".formatted(getLocation().x, getLocation().y)));
mouse.move(getLocation().x, getLocation().y);
}
public void dragAndDrop(ActionImage image) {
mouse.move(getLocation().x, getLocation().y);
mouse.down();
mouse.move(image.getLocation().x, image.getLocation().y);
mouse.up();
}
}