11

I am trying to verify the text "The information provided is either invalid or incomplete." is displayed using Selenium/WebDriver in Java.

I have the following HTML:

<div id="validationError">
    <ul>
        <li>Required: Server Name</li>
        <li>Required: Receiving Port</li>
    </ul>
</div>

with the following CSS code:

#validationError:before {
    content: 'The information provided is either invalid or incomplete.';
}

Here's my current java code:

WebElement errorBox = browser.findElement(By.id("validationError"));
Assert.assertNotNull("Could not find validation errors", errorBox);

System.out.println("Error Explanation: " + error.getCssValue("content") + "\n");

List<WebElement> errorList = errorBox.findElements(By.tagName("li"));
for (WebElement error : errorList) {
    System.out.println("Error: " + error.getText());
}

System.out.println("\nError Full Text: " + errorBox.getText());

This is my output of the above code:

Error Explanation:

Error: Required: Server Name
Error: Required: Receiving Port

Error Full Text: Required: Server Name
Required: Receiving Port

I can't seem to find a way to verify the text "The information provided is either invalid or incomplete." is displayed. Calling getText() on the web element also does not return the expected string, but will display any other normal text within that div. Any ideas?

  • can you provide the `html`? – Saifur Jan 30 '15 at 21:30
  • Sorry, I had issues with the code formatting, I didn't realize you need a newline before each code snippet. Does it make sense now? –  Jan 30 '15 at 21:34
  • It does . However, do you know `webdriver` finds the element first or not? plus provide the stacktrace with the question for future reference please – Saifur Jan 30 '15 at 21:38
  • 1
    Can you provide more of the HTML, including the text you're trying to access? – Richard Jan 30 '15 at 21:39
  • I have updated my HTML and included more java code to help explain what's going on. The page renders like so: `The information provided is either invalid or incomplete.` `Required: Server Name` `Required: Receiving Port` I'm trying to verify the first line, the "The information provided is either invalid or incomplete.". However it seems that since it comes from CSS I can't access it through normal means. –  Jan 30 '15 at 22:12
  • Oh, I see, it's a pseudo element. I am not sure Selenium can select those. I'll look into it more in a bit. – Richard Jan 30 '15 at 23:51
  • Might be a duplicate. The answer there is more thorough even though chronologically later: [Getting the values of all the CSS properties of a selected element in Selenium](https://stackoverflow.com/questions/32537339/getting-the-values-of-all-the-css-properties-of-a-selected-element-in-selenium) – Carolus Dec 09 '19 at 13:48

2 Answers2

29

I am not 100% sure if WebDriver can retrieve pseudo element content for you. I think you would need to use Javascript. Below works, I tested.

String script = "return window.getComputedStyle(document.querySelector('#validationError'),':before').getPropertyValue('content')";
JavascriptExecutor js = (JavascriptExecutor)driver;
String content = (String) js.executeScript(script);
System.out.println(content);

This should print

The information provided is either invalid or incomplete.
nilesh
  • 14,131
  • 7
  • 65
  • 79
1

Tried the same, works for me.

Created sample html file :

<!DOCTYPE html>
<html>
<head>
<style>
 p::after { 
  content: " - Remember this";
}
</style>
</head>
<body>

<p id='b'>My name is Donald</p>
<p>I live in Ducksburg</p>

</body>
</html>

Reused Nilesh's code

    String script  = "return window.getComputedStyle(document.querySelector('p#b'), ':after').getPropertyValue('content')";
    JavascriptExecutor js = (JavascriptExecutor) UIKeyWords.getUIDriver();
    String contentValue = (String) js.executeScript(script);
    System.out.println(contentValue);
RArora
  • 234
  • 1
  • 7