Hi,
I have the following sample if elseif statement. When I execute this, the else if is executed and right after that it entered in to last else statement. I thought the else statement will be executed only when all if conditions are false. Not sure whats wrong I am doing.
This is my actual code
int jobCntNum = 21
int countTotalJobsListed = ‘’
int lastPageNum = Integer.parseInt(pageNum)
for (int i = 1; i <= lastPageNum; i++) {
List<String> jobCardsPerPage =
resultsSection.findElements(By.xpath('//div[@class=\'pagination-
results\']//div[@class=\'career-card-header\']/a'))
countTotalJobsListed = (countTotalJobsListed + jobCardsPerPage.size())
if (jobCntNum == countTotalJobsListed) {
CustomKeywords.'ciautils.ciaUtilities.checkStringDisplayed'('The Total jobs posted ', countTotalJobsListed,
jobCntNum)
} else if (i != lastPageNum) {
driver.findElement(By.xpath(GlobalVariable.jobResultsSection + '//*[name()=\'svg\' and @aria-label= \'Forwards\']')).click()
} else {
println('Total jobs posted doesn\'t match with the count shown as jobs found')
}
}
}
those assignments looks weird. you declare an int but you pass an empty string as value
after that you declare another int, lastPageNum parsing … an unknown pageNum. which is further used in the else if condition
so … how it is supposed we can debug your code, with so many unknown variables?
LE: @Russ_Thomas this variant it is also supported by java, should work same as your example, provided the conditions are properly written:
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
@bionel, Thanks for your responses. I didn’t post the complete code as even if I post that it will not help you as pageNum is getting number from the application. So by looking at the variable names I thought it will be understandable as what type of data I might be sending. Sorry if that didn’t provide you the details.
Anyhow the issue got resolved and now I am able to go through the if conditions and get what I was expecting. All I did was I made sure all the variables used in that code are integers and so it worked fine.