0

I am trying to validate the message which comes on the web page after performing an action (updating/creation of any records in the system.) The problem is the message disappear after 3-4 seconds, and hence I am unable to capture any changes in DOM(Javascript)

Attached is the screenshot - enter image description here I tried BrowserDriver.getBrowserDriver().getPageSource().contains("warning message here") with assertTrue() statement but its not working.

please let me know the approach

7
  • How the DOM looks like when then message pops up? I had to do similar stuff with toaster Commented Apr 5, 2015 at 16:11
  • Similar to this... I am sure since the message disappear in 2-3 seconds, I cant capture the DOM. <script type="text/javascript"> //<![CDATA[ $("div.fadable").delay(2000) .fadeOut("slow", function () { $("div.fadable").remove(); }); //<![CDATA[ </script> Commented Apr 5, 2015 at 16:46
  • This looks like the script. But, the message could be a div or some other tag. can you confirm that? Commented Apr 5, 2015 at 16:55
  • Yes the message is in <div class="message success fadable">..</div> However, I can not see the details as the message disappears soon and so is this div class Commented Apr 5, 2015 at 17:06
  • Are you using Selenium Java bindings?And, can is that a pulic site? Commented Apr 5, 2015 at 17:08

2 Answers 2

1

You can give the following code a try. Note: the selector is written in a way to identify the element based on the failure div text. Please make sure the text inside the selector is as it is appeared in the message(specially whitespace). I always suggest you to find the element depending on the class/id if that is unique if possible

By xpath = By.xpath("//div[contains(.,'Failed To Save')]");

WebElement element = (new WebDriverWait(driver,5)).until(ExpectedConditions.presenceOfElementLocated(xpath));
System.out.println(element.getText());
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you would like to set implicitlyWait property as little as possible:

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

Then it's better to use ExpectedConditions to catch that element:

new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("your-element")));

So you don't need to use assertTrue() or anything like this, WebDriverWait fails if it can't find an element.

Don't forget to change implicitlyWait value after, back to the default one.

2 Comments

How can I define the xpath when the webpage message disappear only in 2 seconds? Also can't we use getPageSource().contains() mehod for this?
@sap1ens It's NOT recommended by SeleniumHQ to mix up implicit and explicit waits. It can cause some unexpected performance issue. And, it's considered as bad practice

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.