if else condition fails even when the condition is true

if else condition fails even when the condition is true

Here is my code

package practice;

import org.testng.annotations.Test;
import static generaluserSignup.signUP.driver;

public class forpractice {

@Test
public void urltest() {

driver.get(“https://www.google.com/”);
String url = driver.getCurrentUrl();
if (url == “https://www.google.com/”) {
System.out.println(“success”);
} else {
System.out.println(“failure”);
}

}

}

RVS Train Asked on December 22, 2015 in Selenium WebDriver.
Add Comment
2 Answer(s)

got the issue fixed by changing the code to
package practice;

import org.testng.annotations.Test;
import static generaluserSignup.signUP.driver;

public class forpractice {

@Test
public void urltest() {

driver.get(“https://www.youtube.com/”);
String url = driver.getCurrentUrl();
String loclurl = “https://www.youtube.com/”;
boolean exact = url.equals(loclurl);
if (exact == true) {
System.out.println(“success”);
} else {
System.out.println(“failure”);
}

}

}

RVS Train Answered on December 22, 2015.
Add Comment

What you have done is fundamentally wrong in Java. You should use String.compare(<String to compare>) method to test out string equality.

Virender Singh Professor Answered on December 22, 2015.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.