Do not log to console what the test script does
as this shows that you are not too sure of your code
We discussed in the previous post that comments should be removed from the test scripts because
they do not add any value
they make test scripts long
they create unnecessary work (by keeping them in sync with the code; if the code changes, the comments need to be changed as well)
What about logging in the console everything that the test script does?
Let's have a look at this test, very similar to the one we saw before:
@Test
public void searchWithLoggingTest() throws InterruptedException {
String homePageUrl = "https://www.vpl.ca/";
String resultsPageUrl = "https://vpl.bibliocommons.com";
String keyword = "java";
String currentUrl = "";
String resultsFound = "";
int i = 0;
int j = 0;
int resultsCount = 0;
int broadenResultsCount = 0;
System.out.println("log in");
driver.get(homePageUrl);
System.out.println("check that the home page url is correct");
Assert.assertEquals(driver.getCurrentUrl(), homePageUrl);
System.out.println("type keyword");
By searchBoxId = By.id("edit-search");
WebElement searchBox = driver.findElement(searchBoxId);
searchBox.sendKeys(keyword);
System.out.println("click search button");
By searchButtonId = By.id("edit-submit");
WebElement searchButton = driver.findElement(searchButtonId);
searchButton.click();
currentUrl = driver.getCurrentUrl();
System.out.println("check that the results page url is correct");
Assert.assertTrue(currentUrl.startsWith(resultsPageUrl));
Assert.assertTrue(currentUrl.contains("query=" + keyword));
Assert.assertTrue(currentUrl.contains("searchType=smart"));
System.out.println("get results found value");
By resultsFoundXpath =
By.xpath("(//span[@data-key = 'pagination-text'])[1]");
WebElement resultsFoundLabel = driver.findElement(resultsFoundXpath);
resultsFound = resultsFoundLabel.getText();
System.out.println("check that the results found includes 1 to 10");
Assert.assertTrue(resultsFound.contains("1 to 10"));
System.out.println("get total results count value");
i = resultsFound.indexOf("of") + 3;
j = resultsFound.indexOf(" results");
resultsCount = Integer.parseInt(resultsFound.substring(i, j));
System.out.println("results count = " + resultsCount);
System.out.println("check that results count is greater than 0");
Assert.assertTrue(resultsCount > 0);
System.out.println("broaden the search");
By broadenSearchXpath = By.xpath("//a[@class = 'broaden-search-link']");
WebElement broadenSearchLink = driver.findElement(broadenSearchXpath);
broadenSearchLink.click();
currentUrl = driver.getCurrentUrl();
System.out.println("check that the url of the new results page is correct");
Assert.assertTrue(currentUrl.contains("searchType=bkw"));
Thread.sleep(5000);
System.out.println("get the new results found value");
resultsFoundLabel = driver.findElement(resultsFoundXpath);
resultsFound = resultsFoundLabel.getText();
System.out.println("get the new total results count");
i = resultsFound.indexOf("of") + 3;
j = resultsFound.indexOf(" results");
broadenResultsCount = Integer.parseInt(resultsFound.substring(i, j));
System.out.println("broaden results count = " + broadenResultsCount);
System.out.println("check that results count after broadening search is positive");
Assert.assertTrue(broadenResultsCount > 0);
System.out.println("check that new results count is greater than the previous one");
Assert.assertTrue(broadenResultsCount > resultsCount);
}
The test script logs to the console any important thing that happens during the code execution:
log in
System.out.println("log in");
check that the home page url is correct
System.out.println("check that the home page url is correct");
type the keyword
System.out.println("type keyword");
click the search button
System.out.println("click search button");
check that the results page url is correct
System.out.println("check that the results page url is correct");
get the results found value
System.out.println("get results found value");
check that the results found includes "1 to 10"
System.out.println("check that the results found includes 1 to 10");
get the total results count value
System.out.println("get total results count value");
display the results count
System.out.println("results count = " + resultsCount);
check that results count is greater than 0
System.out.println("check that results count is greater than 0");
broaden the search
System.out.println("broaden the search");
check that the url of the new results page is correct
System.out.println("check that the url of the new results page is correct");
get the new results found value
System.out.println("get the new results found value");
get the new total results count
System.out.println("get the new total results count");
check that the results count after broadening the search is positive
System.out.println("check that the results count after broadening the search is positive");
display the results count after broadening the search
System.out.println("broaden results count = " + broadenResultsCount);
check that the new results count is greater than the previous one
System.out.println("check that the new results count is greater than the previous one");
After executing the test script, we get info displayed in the console:
log in
type keyword
click search button
get results found value
get total results count value
results count = 418
broaden the search
get the new results found value
get the new total results count
broaden results count = 573
This is good, right? We get to see that the test script does what it is supposed to do!
By the contrary, this is not good at all.
Let's see why.
First,
all the lines of code that do the logging to the console are just like comments. They explain what the next few lines of code do.
But instead of doing this with comments (which are not code), now we do it with code.
Second,
the test script displays a lot of information to the console. This is easy to read for a single test but becomes difficult for lots of tests.
Also,
if you need to check in the console that each test did what it is supposed to do, then this is not test automation since you check the console info manually. Test automation means that everything is done automatically instead of manually.
Third,
the information added to the console becomes meaningless when you start executing tests in parallel. Since the tests execute in random order and in the same time, you will see a lot of information in the console that is not displayed sequentially. Some lines may come from the first script, others from a second script, others from a third script, then going back to the first script, etc.
This is an example of what you get in the console if we run 2 tests like the one from above in the same time:
log in
type keyword
log in
type keyword
click search button
get results found value
click search button
get results found value
get total results count value
get total results count value
results count = 418
results count = 418
broaden the search
get the new results found value
broaden the search
get the new results found value
get the new total results count
broaden results count = 573
get the new total results count
broaden results count = 573
Isnit this confusing?
What else is wrong about logging information to the console?
It shows that you are not sure yet that the code does what it is supposed to do.
This is also something that a beginner would do so I suggest not doing it.
In conclusion, do not have comments in your test scripts and also do not do any logging in the console.
Code that logs to the console what the test script does is just clutter.
Use a logging framework like log4j maybe? That would have been a better alternative than not logging at all.
What you need to tell is, do not use sysout as logging