Skip to content

Commit fea3c70

Browse files
alb-i986shs96c
authored andcommitted
Refactor Bys, "checkNotNull"
Move the checks to the constructors of the concrete classes (which are in the public API). Also, some minor cleanups. Signed-off-by: Simon Stewart <[email protected]>
1 parent a6e465e commit fea3c70

File tree

1 file changed

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

1 file changed

+65
-66
lines changed

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

Lines changed: 65 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public abstract class By {
4949
* @return A By which locates elements by the value of the "id" attribute.
5050
*/
5151
public static By id(final String id) {
52-
if (id == null)
53-
throw new IllegalArgumentException(
54-
"Cannot find elements when the id is null.");
55-
5652
return new ById(id);
5753
}
5854

@@ -61,58 +57,38 @@ public static By id(final String id) {
6157
* @return A By which locates A elements by the exact text they display.
6258
*/
6359
public static By linkText(final String linkText) {
64-
if (linkText == null)
65-
throw new IllegalArgumentException(
66-
"Cannot find elements when the link text is null.");
67-
6860
return new ByLinkText(linkText);
6961
}
7062

7163
/**
72-
* @param linkText The text to match against.
73-
* @return A By which locates A elements that contain the given text.
64+
* @param partialLinkText The partial text to match against
65+
* @return a By which locates A elements that contain the given link text
7466
*/
75-
public static By partialLinkText(final String linkText) {
76-
if (linkText == null)
77-
throw new IllegalArgumentException(
78-
"Cannot find elements when the link text is null.");
79-
80-
return new ByPartialLinkText(linkText);
67+
public static By partialLinkText(final String partialLinkText) {
68+
return new ByPartialLinkText(partialLinkText);
8169
}
8270

8371
/**
8472
* @param name The value of the "name" attribute to search for.
8573
* @return A By which locates elements by the value of the "name" attribute.
8674
*/
8775
public static By name(final String name) {
88-
if (name == null)
89-
throw new IllegalArgumentException(
90-
"Cannot find elements when the name is null.");
91-
9276
return new ByName(name);
9377
}
9478

9579
/**
96-
* @param name The element's tag name.
97-
* @return A By which locates elements by their tag name.
80+
* @param tagName The element's tagName
81+
* @return a By which locates elements by their tag name
9882
*/
99-
public static By tagName(final String name) {
100-
if (name == null)
101-
throw new IllegalArgumentException(
102-
"Cannot find elements when the tag name is null.");
103-
104-
return new ByTagName(name);
83+
public static By tagName(final String tagName) {
84+
return new ByTagName(tagName);
10585
}
10686

10787
/**
10888
* @param xpathExpression The XPath to use.
10989
* @return A By which locates elements via XPath.
11090
*/
11191
public static By xpath(final String xpathExpression) {
112-
if (xpathExpression == null)
113-
throw new IllegalArgumentException(
114-
"Cannot find elements when the XPath is null.");
115-
11692
return new ByXPath(xpathExpression);
11793
}
11894

@@ -125,10 +101,6 @@ public static By xpath(final String xpathExpression) {
125101
* @return A By which locates elements by the value of the "class" attribute.
126102
*/
127103
public static By className(final String className) {
128-
if (className == null)
129-
throw new IllegalArgumentException(
130-
"Cannot find elements when the class name is null.");
131-
132104
return new ByClassName(className);
133105
}
134106

@@ -137,16 +109,11 @@ public static By className(final String className) {
137109
* implement the Selector API, a best effort is made to emulate the API. In this case, we strive
138110
* for at least CSS2 support, but offer no guarantees.
139111
*
140-
* @param selector CSS expression.
112+
* @param cssSelector CSS expression.
141113
* @return A By which locates elements by CSS.
142114
*/
143-
public static By cssSelector(final String selector) {
144-
if (selector == null)
145-
throw new IllegalArgumentException(
146-
"Cannot find elements when the CSS selector is null.");
147-
148-
return new ByCssSelector(selector);
149-
115+
public static By cssSelector(final String cssSelector) {
116+
return new ByCssSelector(cssSelector);
150117
}
151118

152119
/**
@@ -201,6 +168,10 @@ public static class ById extends By implements Serializable {
201168
private final String id;
202169

203170
public ById(String id) {
171+
if (id == null)
172+
throw new IllegalArgumentException(
173+
"Cannot find elements with a null id attribute.");
174+
204175
this.id = id;
205176
}
206177

@@ -233,6 +204,10 @@ public static class ByLinkText extends By implements Serializable {
233204
private final String linkText;
234205

235206
public ByLinkText(String linkText) {
207+
if (linkText == null)
208+
throw new IllegalArgumentException(
209+
"Cannot find elements when link text is null.");
210+
236211
this.linkText = linkText;
237212
}
238213

@@ -256,26 +231,30 @@ public static class ByPartialLinkText extends By implements Serializable {
256231

257232
private static final long serialVersionUID = 1163955344140679054L;
258233

259-
private final String linkText;
234+
private final String partialLinkText;
260235

261-
public ByPartialLinkText(String linkText) {
262-
this.linkText = linkText;
236+
public ByPartialLinkText(String partialLinkText) {
237+
if (partialLinkText == null)
238+
throw new IllegalArgumentException(
239+
"Cannot find elements when link text is null.");
240+
241+
this.partialLinkText = partialLinkText;
263242
}
264243

265244
@Override
266245
public List<WebElement> findElements(SearchContext context) {
267246
return ((FindsByLinkText) context)
268-
.findElementsByPartialLinkText(linkText);
247+
.findElementsByPartialLinkText(partialLinkText);
269248
}
270249

271250
@Override
272251
public WebElement findElement(SearchContext context) {
273-
return ((FindsByLinkText) context).findElementByPartialLinkText(linkText);
252+
return ((FindsByLinkText) context).findElementByPartialLinkText(partialLinkText);
274253
}
275254

276255
@Override
277256
public String toString() {
278-
return "By.partialLinkText: " + linkText;
257+
return "By.partialLinkText: " + partialLinkText;
279258
}
280259
}
281260

@@ -286,6 +265,10 @@ public static class ByName extends By implements Serializable {
286265
private final String name;
287266

288267
public ByName(String name) {
268+
if (name == null)
269+
throw new IllegalArgumentException(
270+
"Cannot find elements when name text is null.");
271+
289272
this.name = name;
290273
}
291274

@@ -315,29 +298,33 @@ public static class ByTagName extends By implements Serializable {
315298

316299
private static final long serialVersionUID = 4699295846984948351L;
317300

318-
private final String name;
301+
private final String tagName;
319302

320-
public ByTagName(String name) {
321-
this.name = name;
303+
public ByTagName(String tagName) {
304+
if (tagName == null)
305+
throw new IllegalArgumentException(
306+
"Cannot find elements when name tag name is null.");
307+
308+
this.tagName = tagName;
322309
}
323310

324311
@Override
325312
public List<WebElement> findElements(SearchContext context) {
326313
if (context instanceof FindsByTagName)
327-
return ((FindsByTagName) context).findElementsByTagName(name);
328-
return ((FindsByXPath) context).findElementsByXPath(".//" + name);
314+
return ((FindsByTagName) context).findElementsByTagName(tagName);
315+
return ((FindsByXPath) context).findElementsByXPath(".//" + tagName);
329316
}
330317

331318
@Override
332319
public WebElement findElement(SearchContext context) {
333320
if (context instanceof FindsByTagName)
334-
return ((FindsByTagName) context).findElementByTagName(name);
335-
return ((FindsByXPath) context).findElementByXPath(".//" + name);
321+
return ((FindsByTagName) context).findElementByTagName(tagName);
322+
return ((FindsByXPath) context).findElementByXPath(".//" + tagName);
336323
}
337324

338325
@Override
339326
public String toString() {
340-
return "By.tagName: " + name;
327+
return "By.tagName: " + tagName;
341328
}
342329
}
343330

@@ -348,6 +335,10 @@ public static class ByXPath extends By implements Serializable {
348335
private final String xpathExpression;
349336

350337
public ByXPath(String xpathExpression) {
338+
if (xpathExpression == null)
339+
throw new IllegalArgumentException(
340+
"Cannot find elements when the XPath expression is null.");
341+
351342
this.xpathExpression = xpathExpression;
352343
}
353344

@@ -374,6 +365,10 @@ public static class ByClassName extends By implements Serializable {
374365
private final String className;
375366

376367
public ByClassName(String className) {
368+
if (className == null)
369+
throw new IllegalArgumentException(
370+
"Cannot find elements when the class name expression is null.");
371+
377372
this.className = className;
378373
}
379374

@@ -417,37 +412,41 @@ public static class ByCssSelector extends By implements Serializable {
417412

418413
private static final long serialVersionUID = -3910258723099459239L;
419414

420-
private final String selector;
415+
private final String cssSelector;
421416

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

426425
@Override
427426
public WebElement findElement(SearchContext context) {
428427
if (context instanceof FindsByCssSelector) {
429428
return ((FindsByCssSelector) context)
430-
.findElementByCssSelector(selector);
429+
.findElementByCssSelector(cssSelector);
431430
}
432431

433432
throw new WebDriverException(
434-
"Driver does not support finding an element by selector: " + selector);
433+
"Driver does not support finding an element by selector: " + cssSelector);
435434
}
436435

437436
@Override
438437
public List<WebElement> findElements(SearchContext context) {
439438
if (context instanceof FindsByCssSelector) {
440439
return ((FindsByCssSelector) context)
441-
.findElementsByCssSelector(selector);
440+
.findElementsByCssSelector(cssSelector);
442441
}
443442

444443
throw new WebDriverException(
445-
"Driver does not support finding elements by selector: " + selector);
444+
"Driver does not support finding elements by selector: " + cssSelector);
446445
}
447446

448447
@Override
449448
public String toString() {
450-
return "By.cssSelector: " + selector;
449+
return "By.cssSelector: " + cssSelector;
451450
}
452451
}
453452
}

0 commit comments

Comments
 (0)