@@ -33,16 +33,16 @@ namespace OpenQA.Selenium
3333 /// </summary>
3434 public class RelativeBy : By
3535 {
36- private string wrappedAtom ;
37- private object root ;
38- private List < object > filters = new List < object > ( ) ;
36+ private readonly string wrappedAtom ;
37+ private readonly object root ;
38+ private readonly List < object > filters = new List < object > ( ) ;
3939
4040 /// <summary>
4141 /// Prevents a default instance of the <see cref="RelativeBy"/> class.
4242 /// </summary>
4343 protected RelativeBy ( ) : base ( )
4444 {
45- string atom = string . Empty ;
45+ string atom ;
4646 using ( Stream atomStream = ResourceUtilities . GetResourceStream ( "find-elements.js" , "find-elements.js" ) )
4747 {
4848 using ( StreamReader atomReader = new StreamReader ( atomStream ) )
@@ -56,7 +56,7 @@ protected RelativeBy() : base()
5656
5757 private RelativeBy ( object root , List < object > ? filters = null ) : this ( )
5858 {
59- this . root = this . GetSerializableRoot ( root ) ;
59+ this . root = GetSerializableRoot ( root ) ;
6060
6161 if ( filters != null )
6262 {
@@ -105,7 +105,7 @@ public override ReadOnlyCollection<IWebElement> FindElements(ISearchContext cont
105105 IJavaScriptExecutor js = GetExecutor ( context ) ;
106106 Dictionary < string , object > parameters = new Dictionary < string , object > ( ) ;
107107 Dictionary < string , object > filterParameters = new Dictionary < string , object > ( ) ;
108- filterParameters [ "root" ] = this . GetSerializableObject ( this . root ) ;
108+ filterParameters [ "root" ] = GetSerializableObject ( this . root ) ;
109109 filterParameters [ "filters" ] = this . filters ;
110110 parameters [ "relative" ] = filterParameters ;
111111 object rawElements = js . ExecuteScript ( wrappedAtom , parameters ) ;
@@ -279,6 +279,7 @@ public RelativeBy Near(IWebElement element)
279279 /// <param name="atMostDistanceInPixels">The maximum distance from the element to be considered "near."</param>
280280 /// <returns>A <see cref="RelativeBy"/> object for use in finding the elements.</returns>
281281 /// <exception cref="ArgumentNullException">If <paramref name="element"/> is null.</exception>
282+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="atMostDistanceInPixels"/> is not a positive value.</exception>
282283 public RelativeBy Near ( IWebElement element , int atMostDistanceInPixels )
283284 {
284285 return Near ( ( object ) element , atMostDistanceInPixels ) ;
@@ -302,6 +303,7 @@ public RelativeBy Near(By locator)
302303 /// <param name="atMostDistanceInPixels">The maximum distance from the element to be considered "near."</param>
303304 /// <returns>A <see cref="RelativeBy"/> object for use in finding the elements.</returns>
304305 /// <exception cref="ArgumentNullException">If <paramref name="locator"/> is null.</exception>
306+ /// <exception cref="ArgumentOutOfRangeException">If <paramref name="atMostDistanceInPixels"/> is not a positive value.</exception>
305307 public RelativeBy Near ( By locator , int atMostDistanceInPixels )
306308 {
307309 return Near ( ( object ) locator , atMostDistanceInPixels ) ;
@@ -316,7 +318,7 @@ private RelativeBy Near(object locator, int atMostDistanceInPixels)
316318
317319 if ( atMostDistanceInPixels <= 0 )
318320 {
319- throw new ArgumentException ( "Distance must be greater than zero" , nameof ( atMostDistanceInPixels ) ) ;
321+ throw new ArgumentOutOfRangeException ( nameof ( atMostDistanceInPixels ) , "Distance must be greater than zero" ) ;
320322 }
321323
322324 Dictionary < string , object > filter = new Dictionary < string , object > ( ) ;
@@ -347,61 +349,59 @@ private RelativeBy SimpleDirection(string direction, object locator)
347349 return new RelativeBy ( this . root , this . filters ) ;
348350 }
349351
350- private object GetSerializableRoot ( object toSerialize )
352+ private static object GetSerializableRoot ( object root )
351353 {
352- if ( toSerialize == null )
354+ if ( root == null )
353355 {
354- throw new ArgumentNullException ( nameof ( toSerialize ) , "object to serialize must not be null" ) ;
356+ throw new ArgumentNullException ( nameof ( root ) , "object to serialize must not be null" ) ;
355357 }
356358
357- By ? asBy = toSerialize as By ;
358- if ( asBy != null )
359+ if ( root is By asBy )
359360 {
360361 return asBy ;
361362 }
362363
363- if ( toSerialize is IWebElement element )
364+ if ( root is IWebElement element )
364365 {
365366 return element ;
366367 }
367368
368- if ( toSerialize is IWrapsElement wrapper )
369+ if ( root is IWrapsElement wrapper )
369370 {
370371 return wrapper . WrappedElement ;
371372 }
372373
373374 throw new WebDriverException ( "Serializable locator must be a By, an IWebElement, or a wrapped element using IWrapsElement" ) ;
374375 }
375376
376- private object GetSerializableObject ( object toSerialize )
377+ private static object GetSerializableObject ( object root )
377378 {
378- if ( toSerialize == null )
379+ if ( root == null )
379380 {
380- throw new ArgumentNullException ( nameof ( toSerialize ) , "object to serialize must not be null" ) ;
381+ throw new ArgumentNullException ( nameof ( root ) , "object to serialize must not be null" ) ;
381382 }
382383
383- By ? asBy = toSerialize as By ;
384- if ( asBy != null )
384+ if ( root is By asBy )
385385 {
386386 Dictionary < string , object > serializedBy = new Dictionary < string , object > ( ) ;
387387 serializedBy [ asBy . Mechanism ] = asBy . Criteria ;
388388 return serializedBy ;
389389 }
390390
391- if ( toSerialize is IWebElement element )
391+ if ( root is IWebElement element )
392392 {
393393 return element ;
394394 }
395395
396- if ( toSerialize is IWrapsElement wrapper )
396+ if ( root is IWrapsElement wrapper )
397397 {
398398 return wrapper . WrappedElement ;
399399 }
400400
401401 throw new WebDriverException ( "Serializable locator must be a By, an IWebElement, or a wrapped element using IWrapsElement" ) ;
402402 }
403403
404- private IJavaScriptExecutor GetExecutor ( ISearchContext context )
404+ private static IJavaScriptExecutor GetExecutor ( ISearchContext context )
405405 {
406406 IJavaScriptExecutor ? executor = context as IJavaScriptExecutor ;
407407 if ( executor != null )
0 commit comments