How do you recognize a fake Selenium tester? By his code

recognize trees by their fruits

You can recognize a fake Selenium tester by looking at his code.
Look at

  1. how much code he writes in a day
  2. more importantly, look at how good the code is

 

The following code is written by a fake Selenium tester.

 

I have added some comments to explain what the code does.

 

public void sendKeys(String keyword){ 
  //create a web driver wait with a 4 seconds timeout 
  wait = new WebDriverWait(driver, 4);
  try { 
       //wait until the element is visible 
       wait.until(
            ExpectedConditions.visibilityOfElementLocated
                (elementLocator)); 

       //find the element and type the keyword in it 
       driver.findElement(elementLocator).sendKeys(keyword); 
      
       //display information to console about the typed keyword
       System.out.println("Entered keyword: "+ keyword); 
       
       //wait 1 second 
       waitFor(1000); 
  } 
  //if there is an exception while typing the keyword
  catch(RuntimeException e) { 
    //display info to console about trying again
    System.out.println("\ntry again to type keyword:" + keyword); 

    //wait 2 seconds; see below the wait() method 
    wait(); 

    //find the element again and type the keyword
    driver.findElement(elementLocator).sendKeys(keyword); 

    //find the element one more time and type enter 
    sendKeyEnter(); 
  } 
}

public void sendKeyEnter(){ 
  wait.until(
         ExpectedConditions.visibilityOfElementLocated
              (elementLocator)); 
  driver.findElement(elementLocator).sendKeys(Keys.RETURN);
} 

public void wait(){ 
  try { Thread.sleep(2000); 
  } 
  catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
  }
} 

public void waitFor(int time){ 
  try { 
    Thread.sleep(time); 
  } 
  catch (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
  }
}

 

Not too bad, you would say.

The code is probably created with the intent of

  • trying to type the text in an element that is displayed slowly
  • if the typing fails because the element is not visible yet, wait a few seconds and try again

 

Lets have a second look:

public void sendKeys(String keyword){
   /* 
      why use such a low timeout?  
      if the page is very slow, 4 seconds will not be sufficient; 
      why is the wait object not declared in the sendKeys() method?  
   */ 
   wait = new WebDriverWait(driver, 4); 
   try { 
       /* 
          the result of the next line is the web element; 
          the web element should be saved in a variable and 
          used in the remaining of the method:

           WebElement element = wait.until(.....); 

          also

          is visibilityOfElementLocated the best expected condition 
          if the intent is to type in the element? 
          how about using elementToBeClickable instead? 
       */ 
       wait.until(
           ExpectedConditions.visibilityOfElementLocated
                              (elementLocator)); 

       /*  
          finding the element again is useless; 
          there is no need to find the element again;  
          it was already found by the previous line; 
          just type into it 
       */ 
       driver.findElement(elementLocator).sendKeys(keyword); 

       /* 
           why display info in the console?  
           first, the console can display limited amount of text; 
           second, how is this info from the console useful 
           if you have 250 test methods? 
       */ 
       System.out.println("Entered keyword: " + keyword); 

       /*  
            why wait for 1 second?  
            why wait at all? 
            why not wait for 2, 3, 4 seconds? 
       */ 
       waitFor(1000); 
   } 
   //if there is an exception 
   catch(RuntimeException e) { 
        //same as above about logging info to console 
        System.out.println("\ntry again to type keyword:" + keyword); 

        /* 
           why wait only for 2 seconds?  
           why not 5?  or 10? 
        */ 
        wait(); 

        /*  find the element again and type the keyword 
        */ 
        driver.findElement(elementLocator).sendKeys(keyword); 

        /* 
            no idea why enter has to be pressed; 
            is the web element part of a form so we have to submit it? 
        */ 
        sendKeyEnter(); 
   } 
}

//on what element is this method typing enter?
public void sendKeyEnter(){ 
   wait.until(
       ExpectedConditions.visibilityOfElementLocated
            (elementLocator)); 
   driver.findElement(elementLocator).sendKeys(Keys.RETURN);
} 

/*  for how long does this method wait? 
    this method is duplicated with the next one; 
*/
public void wait(){ 
    try { 
       Thread.sleep(2000); 
    } 
    catch (InterruptedException e) { 
       // the autogenerated code is not removed
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
    }
} 

public void waitFor(int time){ 
    try { 
       Thread.sleep(time); 
    } 
    catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
    }
} 

 

What you just saw is a case of “re-inventing the wheel”.

The code tries to find an web element that is loaded slowly in the page.

 

Selenium WebDriver has already classes that do this such as WebDriverWait and ExpectedConditions.

The same thing can be done with just 3 lines of code:

 

WebElement element = new WebDriverWait(driver, 30).until (
                           elementToBeClickable(elementLocator)); 
element.sendKeys(keyword);

 

How does this work?

The wait object will retry every 500 ms finding the element.
If the element is found and clickable, it is returned so it can be saved in a variable.
Otherwise, the wait object with try again.

Finding the element will be retried until

  • the element is found and is clickabled  or
  • the 30 seconds timeout is up

 

But lets say that we are concerned about stale exceptions or we want more control over the wait times.

Then, we can use FluentWaits:

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                        .withTimeout(30, SECONDS)
                        .pollingEvery(2, SECONDS)
                        .ignoring(NoSuchElementException.class)
                        .ignoring(StaleElementReferenceException.class) ;

WebElement element = wait.until (elementToBeClickable(elementLocator)); 

element.sendKeys(keyword);

 

So, do you want to be sure that a Selenium tester is real or fake?

Ask him to show you some of his code.

 

2 Comments

  1. I’m sorry, there’s a basic flaw in this post – its title (the rest is rather reasonable). No one who identifies as a “selenium tester” can ever be worth their salt.
    Simple reason – if they were, they would know that selenium is only one library that should (can?) be used when writing test-code, and would find better ways to self identify.
    The rest is a clear matter of decent coding and doing some investigation on the libraries that one uses when coding.

    Like

    Reply

Leave a comment