0

I am new to Automation Testing. While writing an automation script using Selenium I am facing some issues. I have 7 Test Cases of which 2 Test Cases are repeatedly failing but when I check those cases manually they are giving expected results and passed.

I am confused about these scenarios, as there are minor changes in these test cases, I am posting both pass and fail test cases.

Passed Test Case

System.out.println("Scenario S1= Both Textfields Empty");
driver.findElement(By.id("email_box")).clear();
driver.findElement(By.id("email_box")).sendKeys("");
driver.findElement(By.id("password_box")).clear();
driver.findElement(By.id("password_box")).sendKeys("");
driver.findElement(By.id("login_button")).click();
Thread.sleep(1000*2);
if(driver.findElement(By.id("login_error")).getText().equals("Please enter Valid Data"))
 {      System.out.println("S1:- Pass:Expected Error msg");    }
  else
  {     System.out.println("S1:-Fail:-Error not shown for both fields empty");      }
  System.out.println("");
driver.navigate().refresh();

Failed Test Case

 //Scenario 4 invalid email-id using click special char
System.out.println("Scenario S4= E-mail Id With Special Char & Blank Password");
driver.findElement(By.id("email_box")).clear();
driver.findElement(By.id("email_box")).sendKeys("1#$%^&*()@%");
driver.findElement(By.id("login_button")).click();
Thread.sleep(1000*2);
if(driver.findElement(By.id("login_error")).getText().equals("Please enter Valid Data")&&
driver.findElement(By.id("username_error")).getText().equals("Please enter a valid email"))
{   System.out.println("S4:-Pass:Expected Error");    }
else
{   System.out.println("S4:-Fail");     }
System.out.println("");
driver.navigate().refresh();

2 Answers 2

2

Your test is failing and printing "S4:-Fail"? Double check the error messages on the site against the ones in your if clause. It may be failing because of trailing whitespace or something else not easily noticeable by eye.

However, this is a good chance to start using asserts to avoid this problem altogether. The great thing about using assertEquals is that it will show the expected and actual values when the assertion fails. This will solve the problem you currently face; your test is failing and you can't see why.

import org.junit.Assert

String expectedLoginError = "Please enter Valid Data";
String actualLoginError = driver.findElement(By.id("login_error")).getText()
String expectedUsernameError = "Please enter a valid email";
String actualUsernameError = driver.findElement(By.id("username_error")).getText();

Assert.assertEquals("The login error does not match", expectedLoginError, actualLoginError);
Assert.assertEquals("The username error does not match", expectedUsernameError , actualUsernameError)
2
  • Hi Lucas,if I use Assert.assertEquals("The login error does not match", expectedLoginError, actualLoginError); syntax it stops the program when it fails. Feb 19, 2015 at 9:59
  • @Val Yes, that's how asserts works, it does not continue afterwards. Which is correct your test failed right there. Why would you want to continue? Feb 19, 2015 at 10:19
0

Depending on how many assertions exist in your test case, and if you with the test to continue if a failure is encountered, consider using SoftAssertions in lieu of Assertions: https://jitpack.io/com/github/cbeust/testng/master-6.11-g4a8459e-95/javadoc/org/testng/asserts/SoftAssert.html

Keep in mind at the end of the test you need to call SoftAssert.assertAll(). If an assertion failure was encountered this causes the test to fail. Take a look at this blog for examples: https://rameshbaskar.wordpress.com/2013/09/11/soft-assertions-using-testng/

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.