Reputation: 21
I've read through several questions/answers with similar sounding issues but I've not found a solution to my issue and have been racking my brain over this for the last few hours so here goes.
On the web form I'm writing tests for, there are several drop down lists and if the option "Don't know" is selected from any of them, a check box will be displayed and needs to be selected or the form cannot be submitted.
I'm storing the selected values as Strings and then have the clicking of the check box within an IF statement checking whether any of those drop down values is "Dont know" (the actual value in the drop down is recorded as "Dont know"):
String drpdwn_WAN = driver.findElement(By.id("idontknow4")).getAttribute("value");
String drpdwn_SFTPAuthMethod = driver.findElement(By.id("idontknow5")).getAttribute("value");
String drpdwn_SFTPCreds = driver.findElement(By.id("idontknow6")).getAttribute("value");
String drpdwn_Bandwidth = driver.findElement(By.id("idontknow7")).getAttribute("value");
String drpdwn_Data = driver.findElement(By.id("idontknow8")).getAttribute("value");
//click on the Understood check box (only appears when 'Don't know' option is selected)
if(drpdwn_WAN == "Dont know" || drpdwn_SFTPAuthMethod == "Dont know" || drpdwn_SFTPCreds == "Dont know" || drpdwn_Bandwidth == "Dont know" || drpdwn_Data == "Dont know"){
driver.findElement(By.name("understand")).click();
}
I've run through the code in debug mode and have confirmed that the drop down values are being correctly stored but even when at least one of the drop down values is "Dont know" it will not run the code within the IF statement and instead just ignore it.
I've written my test so that the options selected from the drop downs are randomised so need a lot of conditional logic to verify other elements on the page such as the check box. As such the above is only one example of the types of IF/ELSE statement I have in my code but it seems that all code contained within any IF/ELSE statements is not getting executed and is instead being ignored and passed over. Can anyone explain to me what it is that I'm doing wrong here and why the IF statements are seemingly not being executed?
This is the first time I've posted on here so apologies if any of the information is lacking or not clear. Also apologies if the solution to this is rather simple but I'm new to the world of Java development.
Thanks in advance for any help offered!
Upvotes: 1
Views: 659
Reputation: 21
Having read stackoverflow.com/questions/513832/ it appears that I should have been using .equals()
instead of ==
for string comparisons. Code worked as expected once I replaced ==
with .equals()
if(drpdwn_WAN.equals("Dont know") || drpdwn_SFTPAuthMethod.equals("Dont know") || drpdwn_SFTPCreds.equals("Dont know") || drpdwn_Bandwidth.equals("Dont know") || drpdwn_Data.equals("Dont know")){
driver.findElement(By.name("understand")).click();
}
Upvotes: 1