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.

 

Advertisements

Selenium WebDriver Interview Questions

SELENIUM WEBDRIVER INTERVIEW QUESTIONS

You have been learning test automation with Selenium WebDriver for some time.

You are wondering if you are ready for interviewing for a test automation job.

How can you know if you are ready or you need to learn more?

You can take practical tests (see this link for an example) to assess the level of your knowledge and skills.

You can also go through possible interview questions to find what you dont know yet.

Personally, I dont think that interview questions are useful for taking the interview at all.

But they can help you find what you dont know and should know.

The following list is not complete and it can be improved.

It focuses on testing your practical Selenium WebDriver skills and less on theoretical knowledge.

For some of the questions, I have added links to articles that provide explanations and useful information.

If you have ideas of other Selenium WebDriver interview questions, please leave them in the comments.

 

EXPLICIT WAITS

  • What can an explicit wait be used for?
  • What is the difference between driver.findElement() and wait.until(ExpectedCondition.visibililityOfElementLocated(locator))
  • What are fluent waits?

LOCATORS

  • When should  you use XPATH locators? When should you not use them?
  • What are the best element locator types?
  • How do you find an element that is included in a frame?
  • How do you test an XPATH expression in Chrome?
  • How do you get the previous, next, parent node?

  • What selectors cannot be done with css selectors?

  • How do you test xpath expressions in IE?

SELENIUM WEBDRIVER ARCHITECTURE

  • What are the differences between the Chrome, IE and Firefox drivers? Where can you find these drivers?
  • What is the difference between ChromeDriver and headless browsers?
  • Why do test scripts execute fastest using Html Unit Driver?

  • How is the Html Unit Driver different from Phantom JS? Which one is better to use?

PAGE OBJECT MODEL

  • Explain how the page object model works. See this article.
  • What is a page component?
  • How can you create fluent methods for the page object classes? For example, resultsPage.changeSortOrder().filterResults()?
  • How does a Page Factory work?
  • Why do you need Page Objects?
  • What is the difference between page object and page element?

UNIT TESTING

  • How do you make test scripts dependent in Test NG? See this article.
  • What is the difference between @Before and @BeforeClass in JUNIT?
  • How do you remove the setUp() and tearDown() methods from the test class?
  • How do you run only the high-priority test scripts?
  • How does test driven development work? See this.
  • What are typical things that can be done in the setUp() method?
  • How do assertions work? See this.
  • Why is unit testing important for test automation? See this.
  • How do you execute the failed test cases in JUNIT and TestNG

  • Why do you need junit rules in test automation?

  • How do you use the test class and test scripts names in test scripts?

  • How do you display the name of each executed script in a log file?

FRAMEWORK

  • How do you create the web driver object in the framework?
  • How do you structure the framework?
  • How do you isolate Selenium WebDriver API code from the page object classes?
  • How do you implement navigation for a site?

MAVEN

  • How do you run test scripts in Command Prompt?
  • How do you run test scripts in parallel? Read this.
  • How is Jenkins? How does it work?
  • What happens when you run mvn test?

  • What is the difference between a maven build phase and goal?

JAVA

  • What is the difference between an abstract class and an interface?
  • How does polymorphism work?
  • What is the difference between inheritance and composition?

  • When do you use composition in test automation?

  • How do you read test data from CSV files? Read this.

SELENIUM WEBDRIVER LIBRARY

  • How do you generate reports for your test scripts? See this article.
  • How do you do cross browser testing with Selenium WebDriver? See this article.
  • How do you make Selenium WebDriver scripts faster? Read this article.
  • How do you run test scripts in parallel? Read this.
  • What should you not have in a test script?
  • How do you run scripts on a headless browser?
  • What test cases should you automate with Selenium WebDriver?
  • What test automation good practices do you know?
  • How do you check that an element is no longer visible?
  • How do you check that an element is no longer in the browser dom?
  • How do you interact with sliders?
  • How do you get the rows of a table embedded in another table without using xpath? assume that the web page includes just 2 tables, the second included in the first one
  • How do you select a listbox option? Provide 2 different ways
  • Why do you need the DesiredCapabilities class?
  • When do you use the RemoteWebDriver class?
  • What is the slowest Selenium WebDriver command?
  • How do you make test script to run in Internet Explorer?
  • What does driver.close() d0?
  • Provide 2 ways of asserting that the title of a page is correct
  • How do you ignore 3rd party requests in a Selenium script?

  • When and how do you use the Actions class?

  • How is a page displayed in the browser?

  • How do you take a page screenshot in case of error?

  • How do you check if a text is included in an element?

  • How can you click an element without using findElement() and click()?

  • How do you deal with the StaleElementReferenceException?

  • How do you check that the server response code for a web page is 200?