Find That Thing In The Room

80 % is not good enough.

A lot of readers might wonder about this statement. 80 % is quite good. Unless your boss does not accept any mistakes. But Agile is the new norm, so learn and adapt. Errors are some great and uncomfortable way to learn.

On the other hand a test automation result should not be right in 80 % of the cases. Especially when it was executed several times within 15 minutes. It is difficult for me to interpret these results.

My draft FAQ for test automation

This one is not perfect, but it will be better at the end of this blog post.

Q: What to do, if I always have a No Such Element Exception?
A: There is a good chance, that the web element cannot be found. So my way to locate the web element is bad. There are several ways to find a web element.

A: What to do, if I always have a Stale Element Exception?
A: There is a high chance, that TestNG is used. The element to be used cannot be found any more. In this can use a findElement as late as possible.

Q: What to do, if I have a No Such Element Exception once in a while?
A: My solution was to retry the required action. After an exception I would wait and try again.

Boolean individualOrderNotFound = false;
For (int i = 0; i < 3 && !individualOrderNotFound; i++){
  try {
      findElement(xpath).click();
      individualOrderNotFound = true;
  }
  catch(NoSuchElementException e){
      Thread.sleep(2000);
  }
}

Q: What to do, if I have a Stale Element Exception once in a while?
A: See the answer on the previous question.

Q: Would you like to write a blog post about it?
A: You are reading one of the blog post serie.

Q: Do you have anything to add?
A: Sure. How about from 80 % to 100% reliability?

Q: Why is 80 % not good enough?
A: If you perform a test, then the result cannot be trusted.

Q: I would just execute the test several times. 80 % of the results would point in the right direction.
A: So you take extra test execution for granted. And what if 90% of the test results are the same. Do you still execute tests until you get to 80 %?

Q: This is supposed to be a frequently asked questions.
A: You are right. Now it is a dialogue. Does it matter?

Q: I am the person who should be posing the question.
A: Why?

Pretty sure

The morale of the following story is that statistics should be used carefully to help your customers. So you can go straight to the next chapter with more Java and Selenium stuff.

Content warning: the following story contains a scene about a date with an unfortunate ending.

Imaginary situation in the cinema more than 2 decades ago. I am recognized by one of the employees.

Me: “I want to have a Cola.”
Employee: “Here you are. And the nachos and the hot sauce.”
Me: [puzzled] “How did you know that?”

Employee: “You always have the Nachos, if you order the Cola.”
Me: “Yes, you are right. I also want to order a Lemonade.”
Employee: “With a chance of 80 % …”
Employee: [Writes something on the cup and places the drink with a wink] “Here you are.”

Me: [Surprised] “What did you write??”
Brunette: [Walks to the counter] “Amy! Who is Amy?”
Me [Turning to the lady with a cramped smile.]
Employee: “Hi Amy.”

Me [Turning to the blond lady with a cramped smile.]
Brunette [Turning to the blond lady] “I am 5 minutes away and …”
Brunette: [Picks up my drink and throws it over me. Walks away angrily.]

Me: “And Amy, did you figure out the situation with the pointers?”
Young man [Approaches counter]: “So, that’s why you are interested in programming.”
Young man [Picks up the drink of Amy and pours over me. Walks away angry]

Amy: [Sighs] “I finally found a young man, who wants to talk about programming.”
Amy: [Picks up my hot sauce and pours it over me. Followed by the nachos]
Amy [Walks away angrily]

Employee: “If this get worse, I pay you bill. 100 % chance of no way.”
A TV crew pops up. A woman with a microphone: “People do the strangest things to stand the desert heat of the desert scene of Monsters Unlimited. More at 9. What is the cooling effect of Nachos and the hot sauce?”

Employee: [Shows receipt with a message: the receiver gets a lifetime membership of Cinema Hype VIP.]
[The sound of a paper being shredded.]

[The end]

Say it again Sam

Back to reality.
In my office I had the following quality check of my automation scripts. If a script would pass in 4 out of 5 cases, then it would be good. OK next test.

Now I had my honour to make my test results 100 % reliable.

The biggest advantage of the Cinema Hype VIP application was the two stage order. First an order could be placed for the movie tickets, so the seats were reserved. On the day of the movie drinks and snacks could be added to the order.

Let me try to remember what happened.
On first sight the actions were not that complicated:

  • Select a day.
  • Select a movie.
  • Select a time slot.

What did I see on screen?
There were 7 buttons to select a day. I implemented this step flawlessly.

The next step was to select the right movie in a combo box. I really liked Outside In, which would be shown from Thursday.

 
WebElement moviesComboBox = driver.findElement(By.xpath(contains(@id,
                                     “moviesComboBox”));
filterCombobox.click();
selectMovieOption();
filterField.sendKeys(“Outside In”);
filterField.sendKeys(Keys.ENTER);

The selection in the combo box went wrong: Stale Element Exception.

Maybe a loop might solve this problem.

Boolean individualOrderNotFound = false;
For (int i = 0; i < 3 && !individualOrderNotFound; i++){
  try {
      WebElement moviesComboBox = driver.findElement(By.xpath(contains(@id,
                                     “moviesComboBox”));
      filterCombobox.click();
      selectMovieOption();
      filterField.sendKeys(“Outside In”);
      filterField.sendKeys(Keys.ENTER);
      individualOrderNotFound = true;
  }
  catch(StaleElementException e){
      Thread.sleep(2000);
  }
}

After some tweaking I got No Such Element Exception. I also added this exception, but it did not help.

 
Boolean individualOrderNotFound = false;
For (int i = 0; i < 3 && !individualOrderNotFound; i++){
  try {
      WebElement moviesComboBox = driver.findElement(By.xpath(contains(@id,
                                     “moviesComboBox”));
      filterCombobox.click();
      selectMovieOption();
      filterField.sendKeys(“Outside In”);
      filterField.sendKeys(Keys.ENTER);
      individualOrderNotFound = true;
  }
  catch(StaleElementException e){
      Thread.sleep(2000);
  }
  catch(NoSuchElementException e){
      Thread.sleep(2000);
  }
}

I found my monster in the room. So I asked for help.

A tester from another team replaced the loop for the movie selection by a couple of sleeps.

Thread.sleep(2000);
filterCombobox.click();
Thread.sleep(2000);

selectMovieOption();
filterField.sendKeys(“Outside In”);
filterField.sendKeys(Keys.ENTER);

The code was a bit slower, but more reliable. 5 out of 5 passed. I better got used to this.

Hypothetical causes

Retrospecting my coding was not pleasant. I did not stick to my own FAQ. And this can happen.

Now let me focus on the working code.
My fellow tester gave me the tip to use sleep. This way any delays would be handled.

There were two statements “filterCombobox.click();” and “selectMovieOption();”, which were preceded with sleeps. Therefore there were two places where exceptions could occur. In my loop I assumed that there was only one place for exceptions. The worst part was that I picked the wrong action.

Let me illustrate, what would happen, if I handle exceptions by starting over at the wrong spot.
I have a recipe for baking. I have enough flour and more than enough eggs. First I find the flour. Everything goes right: there is no exception. Then I put 200 gram of flour into a bowl.

For the second step I pick an egg. I break the egg and the smell is bad. That is an exception. The egg goes into the wastebin. So I start all over. I empty the bowl.

First step I have to weigh the flour, but there is no flour any more. Now I have an exception and I cannot finish the recipe.

A much better way to find a good egg without throwing away the flour.

This is something I need to research.

Final Fantast I C Assert

At the end of every regression test there should be one final check. A simple yes or no would indicate, whether the test has passed. For the cinema reservation system this was quite simple. Has the order for the tickets, drinks, and snacks sent to the cinema?

I let Selenium set the search field to Order Id and enter the Order id. I saw a Stale Element Exception. Time for my FAQ. I used findElement at the latest moment. Stale Element Exception.

I let Selenium switch to another menu and back to the right page to continue.
Stale Element Exception.

Which other steps were possible?
I let Selenium log off and log on. A search of the Order Id followed.
The order was not found, so it was sent. Test passed.

Another look at Continuous Delivery

[Update author: my opinion is not the same as the author of the referred characters, but I believe in the goodness of the good characters.]

It was like delivering a message. If one way did not work out, another way was explored. What had a mail delivery person, a milk delivery person, and a mail delivery teacher in common?

They all wanted to deliver a letter to Mr. H. Potter. Persistence paid off. And be nice to the messenger. The delivery story had a nasty tail.

Deliberate Practice

Of course this post should end with some smart code.

While blogging I was going through the chapters of the free course about Selenium WebDriver and Java of Angie Jones on the Test Automation University. In Chapter 9 about Wait Strategies I found FluentWait. One of the cool things about this command, that it could ignore exceptions.

A part of the solution was:

Thread.sleep(2000);

selectMovieOption();

This code was not optimal. If the environment or network is changed, then the sleep time might be adopted each time. A FluentWait would provide a robust and fast solution in this case.

FluentWait wait = new FluentWait(driver)
  .withTimeout(Duration.ofSeconds(8))
  .pollingEvery(Duration.ofSeconds(1))
  .ignoring(NoSuchElementexception.class);
wait.until(ExpectedConditions.visibilityOf(
  driver.findelement(filterCombobox)));
selectMovieOption();

Also Alan Richardson released a paid course about test automation on LinkedIn: Advanced Selenium: 3 Synchronization Strategies. If I need more tricks, I consider to have a close look.

My improved FAQ for test automation

Q: What to do, if I always have a No Such Element Exception?
A: There is a good chance, that the web element cannot be found. So my way for to locate the web element is bad. There are several ways to find a web element.

Q: What to do, if I always have a Stale Element Exception?
A: There is a high chance, that TestNG is used. The element to be used cannot be found any more. In this case I use a findElement as late as possible.  Another option is to add some additional steps like logging out and logging in.

Q: What to do, if I have a No Such Element Exception once in a while?
A: Use a FluentWait.

Q: What to do, if I have a Stale Element Exception once in a while?
A: See the answer on the previous question.

Q: Do you have anything to add?
A: Have a look at Deliberate Practice. I mean the way of learning and not the previous chapter.

Selenium, I think this is the beginning of a beautiful friendship.