View Javadoc
1   package io.guixer.logs;
2   
3   import static com.google.common.base.Preconditions.checkNotNull;
4   import static org.apache.commons.lang3.StringUtils.substringAfter;
5   
6   import javax.annotation.Nullable;
7   
8   import org.apache.commons.lang3.NotImplementedException;
9   
10  public abstract class LoggedBy {
11  
12  	public static LoggedBy xpath(
13  		final String xpathExpression
14  	) {
15  
16  		return new LoggedByXPath(xpathExpression);
17  	}
18  
19  	public static LoggedBy cssSelector(
20  		final String cssSelector
21  	) {
22  
23  		return new LoggedByCssSelector(cssSelector);
24  	}
25  
26  	public static LoggedBy id(
27  		final String id
28  	) {
29  
30  		return new LoggedById(id);
31  	}
32  
33  	@Override
34  	public boolean equals(
35  		@Nullable final Object o
36  	) {
37  
38  		if (o == null || !(o instanceof LoggedBy)) {
39  			return false;
40  		}
41  
42  		return toString().equals(o.toString());
43  	}
44  
45  	public int hashCode() {
46  
47  		return toString().hashCode();
48  	}
49  
50  	public abstract String serialize();
51  
52  	public static final class LoggedByXPath extends LoggedBy {
53  
54  		public final String xpathExpression;
55  
56  		public LoggedByXPath(
57  			final String xpathExpression
58  		) {
59  
60  			this.xpathExpression = checkNotNull(xpathExpression, "xpathExpression");
61  		}
62  
63  		@Override
64  		public String toString() {
65  
66  			return "By.xpath: " + xpathExpression;
67  		}
68  
69  		@Override
70  		public String serialize() {
71  			return "xpath:" + xpathExpression;
72  		}
73  	}
74  
75  	public static final class LoggedByCssSelector extends LoggedBy {
76  
77  		public final String cssSelector;
78  
79  		public LoggedByCssSelector(
80  			final String cssSelector
81  		) {
82  
83  			this.cssSelector = checkNotNull(cssSelector, "cssSelector");
84  		}
85  
86  		@Override
87  		public String toString() {
88  
89  			return "By.cssSelector: " + cssSelector;
90  		}
91  
92  		@Override
93  		public String serialize() {
94  			return "css:" + cssSelector;
95  		}
96  	}
97  
98  	public static final class LoggedById extends LoggedBy {
99  
100 		public final String id;
101 
102 		public LoggedById(
103 			final String id
104 		) {
105 
106 			this.id = checkNotNull(id, "id");
107 		}
108 
109 		@Override
110 		public String toString() {
111 
112 			return "By.id: " + id;
113 		}
114 
115 		@Override
116 		public String serialize() {
117 			return "#" + id;
118 		}
119 	}
120 
121 	public static LoggedBy parse(
122 		final String s
123 	) {
124 
125 		checkNotNull(s, "s");
126 
127 		if (s.startsWith("By.xpath: ")) {
128 
129 			return xpath(substringAfter(s, ": "));
130 
131 		} else if (s.startsWith("By.cssSelector: ")) {
132 
133 			return cssSelector(substringAfter(s, ": "));
134 
135 		} else if (s.startsWith("By.id: ")) {
136 
137 			return id(substringAfter(s, ": "));
138 		}
139 
140 		throw new NotImplementedException("s: " + s);
141 	}
142 }