0

I am learning Selenium in Java. I want to compare 2 strings in do while loop, I want loop to keep running unless my 2 strings value has been equal, when value is equal then loop should stop. Now I show you my code & tell you what I want to do with this code. it will open https://www.facebook.com , on Facebook homepage, class name of "First Name" field is "inputtext _58mg _5dba _2ph-". So I created a string variable cname1 that will store class value of current focused element in loop, and cname2 variable has value "inputtext _58mg _5dba _2ph-" , then I compare these 2 in do while loop, and loop should stop when cname1 & cname2 are equal. But it is not working, it stops in password field of Facebook instead of running until First Name field. Thanks

public class HelloWorldTest {
private static String cname1;
private static String cname2;

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/"); 
driver.findElement(By.id("email")).click(); 

do {
     driver.switchTo().activeElement().sendKeys(Keys.TAB);
     WebElement element = driver.switchTo().activeElement();
     String cname1 = element.getAttribute("class");
     String cname2 = "inputtext _58mg _5dba _2ph-";
     System.out.println(cname1);
     System.out.println(cname2);
    }
    while ( cname1 != cname2 );
}

private void sendKeys(Keys enter) {}

}
Wyatt Lowery
  • 513
  • 1
  • 7
  • 21
carol
  • 311
  • 2
  • 4
  • 14
  • It's been a while since I touched Java, but I believe Strings are treated like objects without boxing, so you cannot do a comparison literally on the variable name. You will need to use a compare function. See the linked question above by @engineer. ...while(!cname1.equals(cname2))... – Élie Dec 26 '15 at 01:07

1 Answers1

0

try with this :

while ( !cname2.equals(cname1) );
  • Thanks, after changing i see this error Exception in thread "main" java.lang.NullPointerException at learningMaven.HelloWorldTest.main(HelloWorldTest.java:28) – carol Dec 26 '15 at 01:07
  • i am using same code as given above – carol Dec 26 '15 at 01:08
  • What's on line 28 of HelloWorldText.java? It's probably a null variable. I would speculate that cname1 is null. – Élie Dec 26 '15 at 01:09
  • this is line 28 while ( cname1 != cname2 ); – carol Dec 26 '15 at 01:10
  • in eclipse, my output is Exception in thread "main" java.lang.NullPointerException at learningMaven.HelloWorldTest.main(HelloWorldTest.java:28) inputtext inputtext _58mg _5dba _2ph- – carol Dec 26 '15 at 01:11
  • Ah, You have defined cname1 and 2 twice, so they are null. You never actually update them. – Élie Dec 26 '15 at 01:13
  • what should i do now? do they not get any value in do-while loop? cname1 gets value from class name of facebook page fields and cname2 already have "inputtext _58mg _5dba _2ph-" – carol Dec 26 '15 at 01:18
  • 1
    To be a bit more clear, the local variables `cname1` and `cname2` declared inside your loop body are *different variables* than the class variables of the same names. You never initialize the class variables, so they have their default value, `null`. The `while` condition is outside the loop body, so there `cname1` and `cname2` refer to the class variables.One way to fix this would be to avoid declaring new, local variables inside the loop body -- just remove the `String` before their names in the loop body to change the declarations into ordinary assignments. – John Bollinger Dec 26 '15 at 01:19
  • 2
    You have declared cname1 & cname2 twice. Firstly, at the start of your class, and secondly inside your do loop. Remove the 'String' word before cname1 and cname2 inside your loop. This will stop new variables being created :). – Élie Dec 26 '15 at 01:20
  • @John Bollinger, Good answer! – Élie Dec 26 '15 at 01:20
  • Thaaaaaaaaaaaaaaaaaaaanks a LOT – carol Dec 26 '15 at 01:23
  • you really helped me and solved my problem instead of Marking as duplicate ;) Thanks again – carol Dec 26 '15 at 01:24