0

I need to take value from row from page

my code is

String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals(bname1,"HDFC");
if(bname1=="HDFC") {
    System.out.println("Bank name is:"+bname1);
} else {
    System.out.println("Bank name not found");
}
System.out.println(bname1);

Result: Bank name not found HDFC

My bank name is "VIJAYA" But when i compare to "bname1" and "VIJAYA",RESULT will be negative? How can i compaire these strings pls help me...

1
  • You failed to mention that this was using JAVA. Commented Apr 13, 2011 at 4:08

5 Answers 5

10

You cannot compare strings in Java using ==. All that does is test to see if the two objects have the same address in memory/are the same instance. Use .equals() instead, like:

if(bname1.equals("HDFC"))

...or preferably:

if("HDFC".equals(bname1))

Which is better because it won't crash if 'bname1' is null.

Sign up to request clarification or add additional context in comments.

1 Comment

Use PMD to pick up this kind of error! It's free and awesome.
0

Your original code has some other issues as well. When you call assertEquals() that tells the compiler to check if the two values are equal and then stop executing code if the condition is not true. Because of this the print statements found after your assertEquals() statement will not get executed if the assertion fails. Instead you should try to do something like the following.

String bname1 = selenium.getText("//table[@id='bank']/tbody/tr[3]/td[2]");
assertEquals("Bank name - "+bname1+" should be HDFC",bname1,"HDFC");

If you write your statement like this the first parameter of the assertEquals function will become the error message displayed when the assertion fails.

Comments

0

Try this..

        WebDriver driver = new FirefoxDriver();
        driver.navigate().to("Url");

        WebElement strvalue = driver.findElement(By.xpath("  "));
        String expected = "Text to compare";
        String actual = strvalue.getText();
        System.out.println(actual);

    if(expected.equals(actual)){
        System.out.println("Pass");
    }
        else {
            System.out.println("Fail");
        }

Comments

0

I'm sure It will help you

assertEquals(bname1,"HDFC");
if(bname1.equals("HDFC")) {
    System.out.println("Bank name is:"+bname1);
} else {
    System.out.println("Bank name not found");
}
System.out.println(bname1);```

Comments

0

You can simply use assertions like:

Assert.assertEquals(ExpectedString, ActualString);

Here ExpectedString is a string that is expected and ActualString is original String value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.