5

HTML code of the message Message display on site

I am unable to verify this sucess message. I am trying to use the following code:

1.

String expectedText ="Dear admin, the Institution is deleted successfully!";
Assert.assertEquals("Dear admin, the Institution is deleted      successfully!",expectedText);

2.

WebElement msg=driver.findElement(By.className("pad margin no-print"));
        String text=msg.getText();
String expectedText = "Dear admin, the Institution is deleted successfully!";
Assert.assertEquals(text,expectedText);

3.

driver.findElement(By.xpath("html/body/div[1]/div/div/div")).getText();`
4
  • can you share the HTML of the message, we can use better element locator. Commented Oct 27, 2016 at 17:13
  • we need your HTML
    – Yu Zhang
    Commented Oct 27, 2016 at 19:39
  • Added screen shot with attachment. Commented Oct 28, 2016 at 5:56
  • For this type of message that disappears after some time on screen, I would suggest you check this post. This helped me in my senario: seleniumeasytutorials.blogspot.com/2020/05/… Commented May 31, 2020 at 11:31

8 Answers 8

3

First two points: I cannot see any "Dear admin" in the message and instead of "assertEquals" try "assertTrue" and "contains". Then try:

String expectedMessage = "The Institution is deleted Successfully!";
String message = driver.findElement(By.xpath("//div[contains(@class,'callout callout-success')]")).getText();
Assert.assertTrue("Your error message", message.contains(expectedMessage));

I mentioned try message.contains because I think that the phrase "iMessage" will be also available in message! Check it.

2
  • I am trying to using following code but not working. Have any other solution to verify this success message. i am also adding iMessage with this but not working. /WebElement msg=driver.findElement(By.xpath("//div[contains(@class,'callout-success')]")); // String text=msg.getText(); //Assert.assertEquals(text,"Message:Dear admin, the Operator is added successfully!"); Commented Oct 28, 2016 at 21:30
  • I wrote you already: try to check message.contains instead of equal. And only check for this expected result: "the Operator is added successfully". Have in mind that it is case sensitive and you should be careful of that.
    – Jose
    Commented Oct 31, 2016 at 12:39
1

I look to use css first and for the elements and attributes I try to be as specific as needed without being overly specific and tying myself into page structure. In this case I would consider using:

success_message=driver.findElement(By.css(".content-wrapper .callout-success"))

along with

success_message.contains('the text')

This assumes that you don't have access to change the HTML source. If you do have that access I recommend you add semantic markup. Currently the page markup is mostly about the placement and appearance, e.g. content-wrapper and main-sidebar. These things will change over time (both in their usage and names) so I would look to add attributes about the mean of the content, e.g. a div#institutions tag and a div.feedback_message tag within it. Also tag the 'deleted' message itself so you aren't asserting the specific text which may change or be in another language.

Then you can do a find and assert that looks like:

Assert.assertTrue(driver.findElement(By.css(".institutions .feedback .deleted")

and you can see how readable that is - essentially " institutions feedback 'deleted' " exists, with no need for additional comments and robust against changes in both the future page layout and also the text used.

0

We should never use the compound class name while finding the element using the className method.

Use the below xpath

WebElement msg=driver.findElement(By.xpath("//div[contains(@class,'callout-success')]"));
String text=msg.getText();
Assert.assertEquals(text,expectedText);

NOTE: Please make sure that with above xpath giving how many elements.If it returning only one element then you can continue with that else you need to change according to that.

0

You can verify your success message using assert.

Assert.assertEqual(Actual message, Expected Message);

OR

Assert.assertTrue(Expected Message.isDisplayed);
0
WebElement msg=driver.findElement(By.xpath(".//*[@id='message23']"));
String text=msg.getText();
if (msg.isEnabled() && text.contains("Registration completed successfully"))
{ 
    System.out.println("Successfully completed");
}else{
    System.out.println("Please enter all details");
}
0

This will return true if it find the success message

WebElement msg=driver.findElement(By.className("pad margin no-print"));
Assert.assertTrue(successMessage.contains("deleted successfully"));
0

When it comes to find element by class name where the element has multiple classes, separate the class names using .

By.className(".pad.margin.no-print"))

If you wanna test your locator, just open the console tab from chrome or firefox browser and try this locator using this command:

$('pad.margin.no-print')

This will get you the element you need

0

In IT industry various software testing techniques are used for different types of testing like web application testing, desktop application testing and medical device software testing where QA team verify the message validation after performing any scenario.

Below mentioned code can be used to verify success message using selenium:

WebElement msg = driver.findElement(By.xpath("//div[text()='The Institution is deleted Successfully!']")); Assert.assertTrue(msg.isDisplayed());

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