Issue
I am currently working on a BDD test automation framework built on Selenium using Cucumber and Java. My framework is using Junit and since Junit does not support soft assertions, I am trying to use AssertJ assertions for my tests. However, it seems that the assertions are not working. Let me try to explain this with the help of the following code
//Method to check form submission
public void ValidateForm(String name, String company, String email, String city, String state, String country, String question)
throws InterruptedException {
ContactName.sendKeys(name);
Company.sendKeys(company);
Email.sendKeys(email);
City.sendKeys(city);
browserAction.selectFromDD(State, state);
browserAction.selectFromDD(Country, country);
Questions.sendKeys(question);
SubmitButton.click();
Thread.sleep(4000);
}
// Method to check form landing page
public void CheckFormSubmission() {
assertThat(driver.getTitle().contains("Thank you"));
}
}
As you can figure out, the ValidateForm() method is submitting the values in the form and CheckFormSubmission() method is checking the title of the landing page after the form is submitted. Now, here I am applying the assertion on the landing page title that if it contains "Thank you", the test should pass and if it contains any other text, the test should fail and an exception should be thrown which should ultimately mark my test as failed in the extent report.
However, it seems that this assertion never works as my test continues and even when I don't get the matching landing page title, the test is marked as passed and no exception is thrown. Can you please help me understand what I might be doing wrong here?
Solution
I think instead of this :
assertThat(driver.getTitle().contains("Thank you"));
you should use this :
String expectedMsg = "Thank you";
assertThat(expectedMsg).contains(driver.getTitle());
Answered By - cruisepandey
Answer Checked By - Pedro (JavaFixing Volunteer)