0

I need to verify if a div is expanded or contains style="display: block; using Java.

<div>
    <label class="link">[ Ver ... ]</label>
    <div class="expand" style="display: block;">
        <div>
            <label>Input type text example: </label>
            <input type="text" placeholder="Input type text">
        </div>
    </div>
</div>
JeffC
  • 22,180
  • 5
  • 32
  • 55

3 Answers3

0

you can use getCssValue for example:

element.getCssValue("display")

For assertion you can use like

String actualStatus = element.getCssValue("display");
String expectedStatus = "block"; //set expected value
Assert.assertEquals(actualStatus,expectedStatus);

You can refer WebElement.getCssValue & WebElement.getAttribute Usage

Community
  • 1
  • 1
user861594
  • 5,733
  • 3
  • 29
  • 45
0

You don't need a function for this. The function in your answer is not correct in a couple instances.

  1. .getCssValue() doesn't throw an exception even if it's not found so there's no need for the try/catch.
  2. .getCssValue() doesn't return null if it's not found. It returns empty string.

You can simply do this

assertTrue(!element.getCssValue(attribute).isEmpty());

This will be true if something other than empty string is returned which would indicate that the attribute exists and has a value.

JeffC
  • 22,180
  • 5
  • 32
  • 55
  • I change for this: `element.isDisplayed();` that return a boolean value – Meidelin Cruz Jul 01 '16 at 01:24
  • That doesn't solve the question you asked. You asked how to find if an element is expanded or contains a style. `.isDisplayed()` does neither. – JeffC Jul 01 '16 at 01:27
  • `.isEmpty()` returns a boolean value. I'm confused. – JeffC Jul 01 '16 at 01:28
  • I asked how verify with asserts if that element is displayed. – Meidelin Cruz Jul 01 '16 at 01:37
  • I test with this `Assert.assertTrue(element.isDisplayed());` – Meidelin Cruz Jul 01 '16 at 01:39
  • "I asked how verify with asserts if that element is displayed." You should read your own question.. that's not what you asked. "I need to verify if a div is expanded or contains style="display: block; using Java." That's what you asked. Those two things are not the same. – JeffC Jul 01 '16 at 04:01
-1

I used:

 private boolean isAttributePresent(WebElement element, String attribute) {
    Boolean result = false;
    try {
        if (element.getCssValue(attribute) != null){
            result = true;
        }
    } catch (Exception e) {}

    return result;
}

And then:

 boolean b=isAttribtuePresent(element,"display");
 Assert.assertTrue(b, "true");
  • Relying on specific CSS property values may make your tests fragile. If the developers use a second CSS class on the element, they could put the specific properties in a stylesheet and you'd be able to test for the presence of an additional class. That way they can change the way expansion is achieved without breaking your tests. You should talk to them about it. Also, you're just ignoring a potential exception. You should log it. Otherwise the test can fail without you knowing the reason it did. – toniedzwiedz Jun 26 '16 at 06:44
  • This answer is not correct in a couple instances and is more complicated than it needs to be. Please see my answer for details and a better solution. – JeffC Jun 28 '16 at 20:15