@Test
public void searchResultBoardenCount() {
int normalResultCount;
openSite();
assertTrue(isHomeDisplayed() == true, "Home page is not displayed!");
searchFor(keyword);
assertTrue(isResultDisplayed() == true, "Result page is not displayed!");
assertTrue(isCountDisplayed(), "Result Count not Displayed!");
assertTrue(count() > 0, "There are no results for the search!");
normalResultCount = count();
In this code sample, the normalResultCount variable is created at the beginning of the test method and used 10 lines below.
When using it, if you will want to see what is the variable type, you will have to scroll up for this information.
Scrolling up to get more information is a waste of time.
Instead, you should just create the variable when you use it:
@Test
public void searchResultBoardenCount() {
openSite();
assertTrue(isHomeDisplayed() == true, "Home page is not displayed!");
searchFor(keyword);
assertTrue(isResultDisplayed() == true, "Result page is not displayed!");
assertTrue(isCountDisplayed(), "Result Count not Displayed!");
assertTrue(count() > 0, "There are no results for the search!");
int normalResultCount = count();
Not sure what is better to call same method twice or first call it to assign the variable then do assertTrue(normalResultCount > 0)
show me the full test method and I will tell you :)
I pretty much did similar to yours like:
assertTrue(searchHasResults(), "There are no search results"); int searchResultsCount = getResultsCount();
where searchHasResults() simply checks if getResultsCount() > 0
I was wondering if something like
int searchResultsCount = getResultsCount(); assertTrue(searchResultCount > 0, "There are no search results");
would have been better
For this code
int searchResultsCount = getResultsCount(); assertTrue(searchResultCount > 0, "There are no search results");
if searchResultsCount is used in at least one more place in addition to the assertion, then the code is correct.
The searchResultsCount variable should be retained if it is used more than once.
But if the assertion is the only place where the variable is used, the variable can be removed and the code become:
assertTrue(getResultsCount()> 0, "There are no search results");
That is exactly what I usually do, the only reason I originally did the first way I posted was the idea that I need to check with assertion that there are results before I get the count to be used later for comparison