johnsonambrose
johnsonambrose

Reputation: 125

How to Validate Color Coded Background in a table using Selenium Web Driver / Java

I wanted to validate the Color Coded Background inside a Table for a specific Value. The Color code is background-color:rgb(96,192,96).I have 5 different links on the page. Each link has the same background color. I need to validate weather the background color is displayed as rgb(96,192,96) for each of the links. If the color code is anything other the rgb(96,192,96), then the code should consider that the link/server is down.

The five Values/Link That Display on the page are Ssi-1-a, Ssi-2-a, Ssi-3-a, Ssi-4-a and Ssi-5-a

How can I validate this using Xpath or any other method ? Provided the Code Below

<html>
    <head>
    <body>
        <b>Status as of </b>
        Wed Oct 25 16:57:57 2017
        <br/>
        <br/>
        This page shows the current version and build date of the SSI code loaded into the JVMs that constitute the environment
        you selected.
        <br/>
        <br/>
        <br/>
        <div style="float:left">
            <table style="display:inline-table" width="500" border="1">
                <tbody>
                <tr>
                <tr>
                <tr>
                    <th>Prod</th>
                    <td style="background-color:rgb(96,192,96)" align="center">
                        <a href="https://XXXXXXXXXXXXXX-XXX.net:XXXXXX/ssiadmin/">Ssi-1-a</a>
                    </td>
                </tr>
                <tr>
                <tr>
                <tr>
                <tr>
                </tbody>
            </table>
        </div>
    </body>
</html>

Upvotes: 0

Views: 3384

Answers (2)

Nathan Ripert
Nathan Ripert

Reputation: 890

You can use getAttribute method of WebElement, to analyze the value of style attribute. The following code should give you a rough idea of how to do it:

WebElement linkToCheck = driver.findElement(By.xpath("xpathOfTheLink"));
if(!linkToCheck.getAttribute("style").contains("rgb(96,192,96)")){
    // server is down, --> does some action
}

where xpathOfTheLink is the xpath of the link you want to check. Given you have 5 different links, you will have to create 5 xpath, or use a dynamic xpath allowing you to check every xpath in a loop, using the code above.

Be carefull, because as mentionned here https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute-java.lang.String- :

The "style" attribute is converted as best can be to a text representation with a trailing semi-colon.

Therefore maybe you will have to check if it contains something else than "rgb(96,192,96)". I let you try, or provide an url with the given html, so I can try (I am curious to see the result).

Upvotes: 0

Roberto Pegoraro
Roberto Pegoraro

Reputation: 1487

driver.findElement(By.name("nameOfComponent")).getCssValue("background-color");

Upvotes: 2

Related Questions