1 package io.guixer.ext.decorate;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4 import static org.apache.commons.lang3.StringUtils.substringAfter;
5 import static org.apache.commons.lang3.StringUtils.substringBefore;
6
7 import java.io.IOException;
8 import java.util.Locale;
9
10 import javax.annotation.Nullable;
11
12 import org.apache.commons.lang3.NotImplementedException;
13 import org.openqa.selenium.By;
14 import org.openqa.selenium.Rectangle;
15 import org.openqa.selenium.WebElement;
16
17 import io.guixer.ext.GuixerExtension;
18 import io.guixer.tools.Locators;
19 import io.guixer.tools.RunnerContext;
20
21 public class Coordinates implements GuixerExtension {
22
23 public static void executeCommand(
24 final RunnerContext context,
25 final RefersTo refersTo,
26 final String locator
27 ) throws IOException {
28
29
30
31 final By by = Locators.by(locator);
32
33 final WebElement element = context.getDriver().findElement(by);
34
35 final StringBuilder sb = new StringBuilder();
36
37 sb.append("refersTo: \"" + refersTo.name().toLowerCase(Locale.ENGLISH) + "\"");
38
39 sb.append(", tagName: \"" + element.getTagName() + "\"");
40
41 @Nullable
42 final String type = element.getAttribute("type");
43
44 if (type != null) {
45
46 sb.append(", type: \"" + type + "\"");
47 }
48
49 final Rectangle rect = element.getRect();
50
51 sb.append(", rect: {");
52 sb.append(" x: ").append(rect.getX());
53 sb.append(", y: ").append(rect.getY());
54 sb.append(", width: ").append(rect.getWidth());
55 sb.append(", height: ").append(rect.getHeight());
56 sb.append(" }");
57
58 context.ext(Coordinates.class.getName(), sb.toString());
59 }
60
61 @Override
62 public void executeCommand(
63 final RunnerContext context,
64 final String text
65 ) throws IOException {
66
67 checkNotNull(context, "context");
68 checkNotNull(text, "text");
69
70 final String refersToAsString = substringBefore(text, " ");
71
72 final RefersTo refersTo;
73
74 if ("this".equals(refersToAsString)) {
75
76 refersTo = RefersTo.THIS;
77
78 } else if ("previous".equals(refersToAsString)) {
79
80 refersTo = RefersTo.PREVIOUS;
81
82 } else {
83
84 throw new NotImplementedException("refersTo: " + refersToAsString + ", in command: " + text);
85 }
86
87 final String locator = removeQuotes(substringAfter(text, " "));
88
89 executeCommand(context, refersTo, locator);
90 }
91
92 private static String removeQuotes(
93 final String s
94 ) {
95
96 if (s.startsWith("'")) {
97
98 return s.substring(1, s.length() - 1);
99
100 } else if (s.startsWith("\"")) {
101
102 return s.substring(1, s.length() - 1);
103
104 } else {
105
106 return s;
107 }
108 }
109 }