0

Why does the following assertEquals() statement:

WebElement submit = driver.findElement(By.name("send"));
Assert.assertEquals("optional", "xxxLogin", submit.getAttribute("value"));

give this java error result:

java.lang.AssertionError:  expected [xxxLogin] but found [optional]

The button label is Login

I was expecting to see something like expected [xxxLogin] but found [Login] (the button label). I read that the first argument is an optional string message but it seems to be used as part of the Equals test?

RalphF
  • 373
  • 3
  • 10
  • 21

3 Answers3

2

The optional string message should be the last argument:

Assert.assertEquals("xxxLogin", submit.getAttribute("value"), "optional");
Joe Attardi
  • 4,381
  • 3
  • 39
  • 41
1

What is the intended purpose of the "optional" string in there?

assertEquals("xxxLogin", submit.getAttribute("value")); should cover your needs from what I'm seeing but you can put the string in there as a third argument if needed.

An alternative could also be:

Assert.assertTrue(
                submit.getAttribute("value").equals("xxxLogin"),
                "Incorrect value message" //you can add the value you found to this string
        );

Essentially the same thing but easier to read in my opinion as you're checking for a TRUE state.

Darwin Allen
  • 190
  • 4
  • 15
0

I've seen some VERY strange behaviour with using assertEquals... It'd be a good idea for you to use assertTrue or assertFalse as your assertion statements. In your case, it'd be:

assertTrue("this test failed!", submit.getAttribute("value").equals("xxxLogin"));

this will no doubt work for you.

ddavison
  • 28,221
  • 15
  • 85
  • 110