Do you know JAVA? Prove it

Some of the companies that I interview for do not believe me that I know Java.

They interviewed before me testers with "Java and Selenium" experience and skills in the resume that in reality did not know much of either Java or Selenium.

So the company gives me a Java test.

Such as

BINARY GAP
A binary gap within a positive integer N is any maximal sequence of consecutive zeroes that is surrounded by ones at both ends in the binary representation of N.
Examples:
Number 9 has the binary representation 1001 and contains a binary gap of length 2.
The number 529 has binary representation 1000010001 and contains 2 binary gaps: one of length 4 and one of length 3.



I get a pen and paper and 1 hour to do it.

Pretty stressful, isn't it?

After 1 hour, I came up with this solution.

Not too pretty but somewhat functional:

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;

public class BinaryGapTestsInitial {  
  
 @Test
 public void testLongestGap()
 {
  int number = 789;
  
  //convert from int to binary; the list is in reversed order
  List reversedBinaryList = new ArrayList();
  int tempNumber = number;
  
  while (tempNumber != 0)
  {            
   reversedBinaryList.add(tempNumber % 2);   
   tempNumber = tempNumber / 2;
  }
  
  //reverse the list
  List binaryList = new ArrayList();  
  for (int i = reversedBinaryList.size() - 1; i >= 0; i--)
   binaryList.add(reversedBinaryList.get(i));   
  
  //verify that the binary list is correct
  int numberFromBinary = 0;
  for (int i = 0; i < binaryList.size(); i++)
   if (binaryList.get(i) == 1)
    numberFromBinary += Math.pow(2, binaryList.size() - i - 1);
  
  System.out.println(binaryList);
  System.out.println(numberFromBinary);
  
  int longestGap = 0;
  for (int i = 0; i< binaryList.size(); i++)
   if (binaryList.get(i) == 0)
   {    
    int j;
    int gap = 1;
    
    for (j = i + 1; j < binaryList.size(); j++)
     if (binaryList.get(j) == 1)      
     {
      gap += j - i - 1;
      i = j - 1;
      break;
     }
         
    System.out.println(gap);
    
    if (gap > longestGap)
     longestGap = gap;         
   }
  
  
  System.out.println("longest gap = " + longestGap);
  
 }
 
}

This code is probably a good candidate for the right side of the next image:





Is this the best and most amuzing measure of bad code?

Now, assume that the company really wants to know how well you know Java.

So they want not only working code but good code.

Something that looks like this:


import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;

public class BinaryGapTests {  
 
 private List toBinary(int number)
 {
  List list = new ArrayList();
      
  while (!is0(number))
  {            
   list.add(0, number % 2);   
   number = number / 2;
  }
    
  return list;
 }
 
 private int toInt(List list)
 {
  int number = 0;
  
  for (int i = 0, listSize = list.size(); i < listSize; i++)
   if (is1(list.get(i)))
    number += Math.pow(2, listSize - i - 1);
  
  return number;
 }
 
 private int last0Index(List list, int index)
 {
  int lastIndex = 0;  
  
  for (int i = index; i < list.size(); i++)             
   if (is1(list.get(i)))
   {
    lastIndex = i - 1;
    break;
   }
     
  return lastIndex > 0 ? lastIndex : list.size() - 1;
 }
 
 private int first0Index(List list, int index)
 {
  
  int firstIndex = -1;
  
  for (int i = index; i < list.size(); i++) 
   if (is0(list.get(i))) 
   {
    firstIndex = i;
    break;      
   }
  return firstIndex;
 }
 
 private List shrinkList(List list, int index)
 {
  return list.subList(index, list.size());
 }
 
 private Boolean is0(int number)
 {
  return number == 0;
 }
 
 private Boolean is1(int number)
 {
  return number == 1;
 }
 
 private int gapSize(int startIndex, int endIndex)
 {
  return endIndex + 1 - startIndex;
 }
 
 private Boolean includes0(List list)
 {
  return list.lastIndexOf(new Integer(0)) > 0;
 }
 
 private int longestGap(List binaryValue)
 {
  int longest0Gap = 0;
  List list = binaryValue;
  
  if (includes0(list))
   
   while (!list.isEmpty())
   {
      
    int first0Idx = first0Index(list, 0);           
    int last0Idx = last0Index(list, first0Idx);       
    
    longest0Gap = (gapSize(first0Idx, last0Idx) > longest0Gap ? 
                   gapSize(first0Idx, last0Idx) : 
                   longest0Gap);
    
    list = shrinkList(list, last0Idx + 1);
   }
  
  return longest0Gap;    
 } 
 
 @Test
 public void testLongestGap()
 {
  ListbinaryValue = toBinary(808);
  
  System.out.println(binaryValue);
  System.out.println(longestGap(binaryValue));
  
  assertTrue(longestGap(binaryValue) > 0);
 }
 
 @Ignore
 public void testBinaryValue()
 {
  int number = 7;    
  assertEquals(number, toInt(toBinary(number)));      
 }

}

Can you write code like this in an hour?

It is probably doable with practice but definitely not easy.

So, if you dont know Java well, think again next time you add Java to your resume.

Because a company may ask you to do a test like the one from above.

Selenium WebDriver Interview Resources





I have gathered together a few resources that may be useful to manual testers that either

  • prepare for test automation interviews or
  • want to interview test automation engineers



BASIC TEST AUTOMATION CONCEPTS


1. WebDriver driver = new FirefoxDriver()


You see this line of code in any test automation project.

Why does it use 2 different types (WebDriver and FirefoxDriver) for creating the driver object?

Why can you create a FirefoxDriver() object and assign it to a WebDriver object?



2. How Does Selenium WebDriver Work?

Selenium WebDriver is a library that allows the test script to drive the website in the browser.





3. Why is unit testing essential for test automation?

Can Selenium WebDriver test automation be done without unit testing?




4. How do JUNIT assertions work?

Each test automation script uses assertions.

assertTrue, assertEquals, assertFalse, etc.


How are they implemented?




TEST AUTOMATION INTERVIEW QUESTIONS

The interview questions are group in categories:
  • explicit waits
  • locators
  • unit testing
  • selenium webdriver architecture
  • framework
  • maven
  • selenium webdriver library
  • java



SELENIUM WEBDRIVER PRACTICAL TEST

If you need to go beyond asking questions about test automation, this is a practical test from a real interview.





NEW

Do you need help to prepare for a Selenium WebDriver interview?

I can help you with it (not free).

Email me at alex@alexsiminiuc.com for more details.

How Do Assertions Work?

@Test
public void testScript()
{
int count = 5;
assertTrue("the count is not greater than 0", count > 0);
assertTrue(count > 0);
assertEquals("the count is not 5", count, 5);
assertEquals(count, 5);
}

One of the things that you will use very frequently in test automation is assertions: 

  1. assertTrue
  2. assertEquals
  3. assertFalse

They allow adding verifications to the unit tests. 

If an assertion passes, the unit test execution continues. 
If an assertion fails, the unit test execution stops and the remaining code lines are ignored. 

 If we use them so much, how about we try to understand how they work? 




So, how do assertions work? 

For this task, we will look at the junit framework code from the GitHub repository: 
https://github.com/junit-team/junit4 

The Assert.java class is the one that implements all JUNIT assertions: 
https://github.com/junit-team/junit4/blob/master/src/main/java/org/junit/Assert.java 

We will look first at methods that implement assertTrue. 




assertTrue 

There are 2 methods that implement assertTrue: 

1. assertTrue method that takes 2 parameters: 

  • condition to be evaluated 
  • message to be displayed in case the assertion fails 


The code is so simple: 


public static void assertTrue(String message, boolean condition)
{
if (!condition)
{
   fail(message);
}
}

If the result of evaluating the condition is false, the method fails because of the fail(message) method. 

The fail(message) method has 2 versions as well: 


public static void fail(String message)
{
  if (message == null)
  {
   throw new AssertionError();
  }
  throw new AssertionError(message);
}

The first fail(message) method checks if the message parameter is null. 

If it is null, it throws an exception. 
If the message is not null, it throws the same exception with the message as a parameter. 


public static void fail()
{
  fail(null);
}

The second fail() method calls the first fail() method with null as parameter. 

2. assertTrue method with 1 parameter (condition to be evaluated)


public static void assertTrue(boolean condition)
{
  assertTrue(null, condition);
}

This method calls the first assertTrue() method with null as message. 

And this is all code needed for implementing assertTrue. 

A few things need to be noticed: 

  • both assertTrue methods are static; using static is not a good programming practice but sometimes it is necessary
  • the methods are very simple and short; one method calls the other 


assertEquals 

Similar with assertTrue, there are 2 methods that implement assertEquals (actually more than 2): 

1. method with 3 parameters 

  • expected - expected value of the variable to be evaluated 
  • actual - actual value of the variable to be evaluated 
  • message - message to be displayed if expected is not equal to actual 



/*
 compares expected and actual using the equalsRegardingNull() method.
 the method ends if the equalsRegardingNull() result is true.
 otherwise, the remaining code is executed.

 the remaining code is executed if expected is different from actual.
 an exception will be thrown.
 if both expected and actual are strings, a ComparisonFailure exception is generated. 
 otherwise, the method calls failNotEquals() method which calls fail().
*/

public static void assertEquals(String message, Object expected, Object actual)
{

 if (equalsRegardingNull(expected, actual))
 { 
   return;
 }

 if (expected instanceof String && actual instanceof String)
 {
   String cleanMessage = message == null ? "" : message;
   throw new ComparisonFailure(cleanMessage, (String) expected, (String) actual);
 }
 else
 {
   failNotEquals(message, expected, actual);
 }

}
/*
  if expected and actual are both null, returns true (expected = actual).
  if expected is null but actual is not, returns false (expected <> actual). 
  if expected is not null, it returns the result of comparing expected with actual.
*/

private static boolean equalsRegardingNull(Object expected, Object actual)
{
  if (expected == null)
  {
    return actual == null;
  }
  return isEquals(expected, actual);
}
/*
   if expected is equal to actual, it returns true.
   if expected is not equals to actual, it returns false.
*/

private static boolean isEquals(Object expected, Object actual)
{
   return expected.equals(actual);
}


private static void failNotEquals(String message, Object expected, Object actual)
{
   fail(format(message, expected, actual));
}



2. method with 2 parameters (expected and actual values of the variable to be evaluated) 

This method calls the assertEquals method with 3 parameters and makes the first parameter null. 


public static void assertEquals(Object expected, Object actual)
{
  assertEquals(null, expected, actual);
}


So, what can we learn from this code? 



  • One of the best sources of learning how something works is the source code
  • If we want to learn how to code better, the source code of popular frameworks provides many good lessons. In this case, the code uses many small methods which are very simple and clear instead of bigger methods.
  • Use custom exceptions for making failures visible

Back To Basics - How does WebDriver driver = new FirefoxDriver(); work?




Have you ever thought how this line works?


WebDriver driver = new FirefoxDriver();


More specifically, why are 2 types needed for creating the driver object?

And why is it possible to create a WebDriver variable but instantiate it with a FirefoxDriver object?

You can find the answer here.

Want Success In Your Testing Career? Follow Iceland's Soccer Strategy

This is not an article about soccer.

It just uses soccer as a pretext.

Because good ideas are everywhere.

The European Soccer Championship is taking place these days.

It is in the quarterfinals phase with just 8 teams left:

  1. Poland - Portugal
  2. Wales - Belgium
  3. Germany - Italy
  4. France - Iceland







Iceland, in quarterfinals?

One of them is a big surprise: Iceland.

To get to quarterfinals, Iceland had 2 draws and 2 wins:

Iceland - Portugal 1 - 1

Iceland - Hungary 1 - 1

Iceland - Austria   2 - 1

Iceland - England 2 - 1

Iceland's opponents have the following populations:

Portugal - 10.4 million

Hungary - 9.8 million

Austria   - 8.4 million

England - 53 million

Iceland is one of the smallest countries in Europe with just above 300.000 people.

Soccer is popular in Iceland and was never too good.

It is a great achievement for the Iceland soccer team to qualify to the European Soccer Championship. 

To reach the quarterfinals of the championship is just incredible.




How did Iceland do it?

Was it a fluke, an accident, a stroke of luck?

Iceland beat England because his players were more motivated, had better execution and more desire.

But there is something else that explains the Iceland's success.

With such a low population, Iceland has a limited pool of players.

It has to do well with the players it has.

And that's what they did.





Iceland invested heavily in soccer

So they invested in training and more training.

Iceland took TV money from UEFA (Union of European Association) and invested it all in infrastructure, coaching and player development.

They build heated indoor fields all over the country (Iceland has very low temperature throughout the year and lots of snow) and helped hundreds of coaches get UEFA licenses.

There are so many UEFA licenses soccer coaches in Iceland that they have one for every 825 people.

Compare this with 1 for every 11.000 people in England.




There was no fluke in Iceland making it to the quarterfinal of the European championship.

The big  investment in soccer paid out big time.

Iceland no longer has a small soccer team in Europe but an important one.



And your point is, Alex?

As a tester or IT professional, if you want to have success in your career, you need to invest first.

Not in a house or in stocks.








Invest in yourself

Invest time, energy and money in your training.

Invest in growing your knowledge, your skills, your communication.

Expand your skills.

If you are a manual tester, learn programming and test automation.

If you know test automation, learn performance testing.

If you know performance testing, learn API testing.

Your knowledge and skills will mature as a result of the investment.

And the success will come shortly.