Skip to content

Commit f833e9e

Browse files
committed
More cleanups in By
1 parent fea3c70 commit f833e9e

File tree

1 file changed

+67
-66
lines changed
  • java/client/src/org/openqa/selenium

1 file changed

+67
-66
lines changed

java/client/src/org/openqa/selenium/By.java

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -48,47 +48,47 @@ public abstract class By {
4848
* @param id The value of the "id" attribute to search for.
4949
* @return A By which locates elements by the value of the "id" attribute.
5050
*/
51-
public static By id(final String id) {
51+
public static By id(String id) {
5252
return new ById(id);
5353
}
5454

5555
/**
5656
* @param linkText The exact text to match against.
5757
* @return A By which locates A elements by the exact text they display.
5858
*/
59-
public static By linkText(final String linkText) {
59+
public static By linkText(String linkText) {
6060
return new ByLinkText(linkText);
6161
}
6262

6363
/**
6464
* @param partialLinkText The partial text to match against
6565
* @return a By which locates A elements that contain the given link text
6666
*/
67-
public static By partialLinkText(final String partialLinkText) {
67+
public static By partialLinkText(String partialLinkText) {
6868
return new ByPartialLinkText(partialLinkText);
6969
}
7070

7171
/**
7272
* @param name The value of the "name" attribute to search for.
7373
* @return A By which locates elements by the value of the "name" attribute.
7474
*/
75-
public static By name(final String name) {
75+
public static By name(String name) {
7676
return new ByName(name);
7777
}
7878

7979
/**
8080
* @param tagName The element's tagName
8181
* @return a By which locates elements by their tag name
8282
*/
83-
public static By tagName(final String tagName) {
83+
public static By tagName(String tagName) {
8484
return new ByTagName(tagName);
8585
}
8686

8787
/**
8888
* @param xpathExpression The XPath to use.
8989
* @return A By which locates elements via XPath.
9090
*/
91-
public static By xpath(final String xpathExpression) {
91+
public static By xpath(String xpathExpression) {
9292
return new ByXPath(xpathExpression);
9393
}
9494

@@ -100,7 +100,7 @@ public static By xpath(final String xpathExpression) {
100100
* @param className The value of the "class" attribute to search for.
101101
* @return A By which locates elements by the value of the "class" attribute.
102102
*/
103-
public static By className(final String className) {
103+
public static By className(String className) {
104104
return new ByClassName(className);
105105
}
106106

@@ -112,7 +112,7 @@ public static By className(final String className) {
112112
* @param cssSelector CSS expression.
113113
* @return A By which locates elements by CSS.
114114
*/
115-
public static By cssSelector(final String cssSelector) {
115+
public static By cssSelector(String cssSelector) {
116116
return new ByCssSelector(cssSelector);
117117
}
118118

@@ -124,9 +124,9 @@ public static By cssSelector(final String cssSelector) {
124124
*/
125125
public WebElement findElement(SearchContext context) {
126126
List<WebElement> allElements = findElements(context);
127-
if (allElements == null || allElements.isEmpty())
128-
throw new NoSuchElementException("Cannot locate an element using "
129-
+ toString());
127+
if (allElements == null || allElements.isEmpty()) {
128+
throw new NoSuchElementException("Cannot locate an element using " + toString());
129+
}
130130
return allElements.get(0);
131131
}
132132

@@ -140,14 +140,13 @@ public WebElement findElement(SearchContext context) {
140140

141141
@Override
142142
public boolean equals(Object o) {
143-
if (this == o)
144-
return true;
145-
if (o == null || getClass() != o.getClass())
143+
if (!(o instanceof By)) {
146144
return false;
145+
}
147146

148-
By by = (By) o;
147+
By that = (By) o;
149148

150-
return toString().equals(by.toString());
149+
return this.toString().equals(that.toString());
151150
}
152151

153152
@Override
@@ -168,27 +167,27 @@ public static class ById extends By implements Serializable {
168167
private final String id;
169168

170169
public ById(String id) {
171-
if (id == null)
172-
throw new IllegalArgumentException(
173-
"Cannot find elements with a null id attribute.");
170+
if (id == null) {
171+
throw new IllegalArgumentException("Cannot find elements with a null id attribute.");
172+
}
174173

175174
this.id = id;
176175
}
177176

178177
@Override
179178
public List<WebElement> findElements(SearchContext context) {
180-
if (context instanceof FindsById)
179+
if (context instanceof FindsById) {
181180
return ((FindsById) context).findElementsById(id);
182-
return ((FindsByXPath) context).findElementsByXPath(".//*[@id = '" + id
183-
+ "']");
181+
}
182+
return ((FindsByXPath) context).findElementsByXPath(".//*[@id = '" + id + "']");
184183
}
185184

186185
@Override
187186
public WebElement findElement(SearchContext context) {
188-
if (context instanceof FindsById)
187+
if (context instanceof FindsById) {
189188
return ((FindsById) context).findElementById(id);
190-
return ((FindsByXPath) context).findElementByXPath(".//*[@id = '" + id
191-
+ "']");
189+
}
190+
return ((FindsByXPath) context).findElementByXPath(".//*[@id = '" + id + "']");
192191
}
193192

194193
@Override
@@ -204,9 +203,9 @@ public static class ByLinkText extends By implements Serializable {
204203
private final String linkText;
205204

206205
public ByLinkText(String linkText) {
207-
if (linkText == null)
208-
throw new IllegalArgumentException(
209-
"Cannot find elements when link text is null.");
206+
if (linkText == null) {
207+
throw new IllegalArgumentException("Cannot find elements when link text is null.");
208+
}
210209

211210
this.linkText = linkText;
212211
}
@@ -234,17 +233,16 @@ public static class ByPartialLinkText extends By implements Serializable {
234233
private final String partialLinkText;
235234

236235
public ByPartialLinkText(String partialLinkText) {
237-
if (partialLinkText == null)
238-
throw new IllegalArgumentException(
239-
"Cannot find elements when link text is null.");
236+
if (partialLinkText == null) {
237+
throw new IllegalArgumentException("Cannot find elements when link text is null.");
238+
}
240239

241240
this.partialLinkText = partialLinkText;
242241
}
243242

244243
@Override
245244
public List<WebElement> findElements(SearchContext context) {
246-
return ((FindsByLinkText) context)
247-
.findElementsByPartialLinkText(partialLinkText);
245+
return ((FindsByLinkText) context).findElementsByPartialLinkText(partialLinkText);
248246
}
249247

250248
@Override
@@ -265,27 +263,27 @@ public static class ByName extends By implements Serializable {
265263
private final String name;
266264

267265
public ByName(String name) {
268-
if (name == null)
269-
throw new IllegalArgumentException(
270-
"Cannot find elements when name text is null.");
266+
if (name == null) {
267+
throw new IllegalArgumentException("Cannot find elements when name text is null.");
268+
}
271269

272270
this.name = name;
273271
}
274272

275273
@Override
276274
public List<WebElement> findElements(SearchContext context) {
277-
if (context instanceof FindsByName)
275+
if (context instanceof FindsByName) {
278276
return ((FindsByName) context).findElementsByName(name);
279-
return ((FindsByXPath) context).findElementsByXPath(".//*[@name = '"
280-
+ name + "']");
277+
}
278+
return ((FindsByXPath) context).findElementsByXPath(".//*[@name = '" + name + "']");
281279
}
282280

283281
@Override
284282
public WebElement findElement(SearchContext context) {
285-
if (context instanceof FindsByName)
283+
if (context instanceof FindsByName) {
286284
return ((FindsByName) context).findElementByName(name);
287-
return ((FindsByXPath) context).findElementByXPath(".//*[@name = '"
288-
+ name + "']");
285+
}
286+
return ((FindsByXPath) context).findElementByXPath(".//*[@name = '" + name + "']");
289287
}
290288

291289
@Override
@@ -301,24 +299,26 @@ public static class ByTagName extends By implements Serializable {
301299
private final String tagName;
302300

303301
public ByTagName(String tagName) {
304-
if (tagName == null)
305-
throw new IllegalArgumentException(
306-
"Cannot find elements when name tag name is null.");
302+
if (tagName == null) {
303+
throw new IllegalArgumentException("Cannot find elements when name tag name is null.");
304+
}
307305

308306
this.tagName = tagName;
309307
}
310308

311309
@Override
312310
public List<WebElement> findElements(SearchContext context) {
313-
if (context instanceof FindsByTagName)
311+
if (context instanceof FindsByTagName) {
314312
return ((FindsByTagName) context).findElementsByTagName(tagName);
313+
}
315314
return ((FindsByXPath) context).findElementsByXPath(".//" + tagName);
316315
}
317316

318317
@Override
319318
public WebElement findElement(SearchContext context) {
320-
if (context instanceof FindsByTagName)
319+
if (context instanceof FindsByTagName) {
321320
return ((FindsByTagName) context).findElementByTagName(tagName);
321+
}
322322
return ((FindsByXPath) context).findElementByXPath(".//" + tagName);
323323
}
324324

@@ -335,9 +335,10 @@ public static class ByXPath extends By implements Serializable {
335335
private final String xpathExpression;
336336

337337
public ByXPath(String xpathExpression) {
338-
if (xpathExpression == null)
338+
if (xpathExpression == null) {
339339
throw new IllegalArgumentException(
340-
"Cannot find elements when the XPath expression is null.");
340+
"Cannot find elements when the XPath expression is null.");
341+
}
341342

342343
this.xpathExpression = xpathExpression;
343344
}
@@ -365,27 +366,30 @@ public static class ByClassName extends By implements Serializable {
365366
private final String className;
366367

367368
public ByClassName(String className) {
368-
if (className == null)
369+
if (className == null) {
369370
throw new IllegalArgumentException(
370-
"Cannot find elements when the class name expression is null.");
371+
"Cannot find elements when the class name expression is null.");
372+
}
371373

372374
this.className = className;
373375
}
374376

375377
@Override
376378
public List<WebElement> findElements(SearchContext context) {
377-
if (context instanceof FindsByClassName)
379+
if (context instanceof FindsByClassName) {
378380
return ((FindsByClassName) context).findElementsByClassName(className);
379-
return ((FindsByXPath) context).findElementsByXPath(".//*["
380-
+ containingWord("class", className) + "]");
381+
}
382+
return ((FindsByXPath) context).findElementsByXPath(
383+
".//*[" + containingWord("class", className) + "]");
381384
}
382385

383386
@Override
384387
public WebElement findElement(SearchContext context) {
385-
if (context instanceof FindsByClassName)
388+
if (context instanceof FindsByClassName) {
386389
return ((FindsByClassName) context).findElementByClassName(className);
387-
return ((FindsByXPath) context).findElementByXPath(".//*["
388-
+ containingWord("class", className) + "]");
390+
}
391+
return ((FindsByXPath) context).findElementByXPath(
392+
".//*[" + containingWord("class", className) + "]");
389393
}
390394

391395
/**
@@ -398,8 +402,7 @@ public WebElement findElement(SearchContext context) {
398402
* @return XPath fragment
399403
*/
400404
private String containingWord(String attribute, String word) {
401-
return "contains(concat(' ',normalize-space(@" + attribute + "),' '),' "
402-
+ word + " ')";
405+
return "contains(concat(' ',normalize-space(@" + attribute + "),' '),' " + word + " ')";
403406
}
404407

405408
@Override
@@ -415,18 +418,17 @@ public static class ByCssSelector extends By implements Serializable {
415418
private final String cssSelector;
416419

417420
public ByCssSelector(String cssSelector) {
418-
if (cssSelector == null)
419-
throw new IllegalArgumentException(
420-
"Cannot find elements when the selector is null");
421+
if (cssSelector == null) {
422+
throw new IllegalArgumentException("Cannot find elements when the selector is null");
423+
}
421424

422425
this.cssSelector = cssSelector;
423426
}
424427

425428
@Override
426429
public WebElement findElement(SearchContext context) {
427430
if (context instanceof FindsByCssSelector) {
428-
return ((FindsByCssSelector) context)
429-
.findElementByCssSelector(cssSelector);
431+
return ((FindsByCssSelector) context).findElementByCssSelector(cssSelector);
430432
}
431433

432434
throw new WebDriverException(
@@ -436,8 +438,7 @@ public WebElement findElement(SearchContext context) {
436438
@Override
437439
public List<WebElement> findElements(SearchContext context) {
438440
if (context instanceof FindsByCssSelector) {
439-
return ((FindsByCssSelector) context)
440-
.findElementsByCssSelector(cssSelector);
441+
return ((FindsByCssSelector) context).findElementsByCssSelector(cssSelector);
441442
}
442443

443444
throw new WebDriverException(

0 commit comments

Comments
 (0)