简体   繁体   中英

Handling multiple tables using selenium webdriver

I am checking the folder hierarchy on a webpage, depending on the type of user. User1 has a set of permissions which enable him to see the folder structure like this :

Main Folder
    - First Child
        -First Grandchild
        -Second Grandchild
    - Second Child
    - Third Child

Each branch of the tree is a table consisting of 1 row. But the number of columns varies depending on the generation.

The "Main Folder" parent has only 1 column. The cell content is the string "Main Folder".

The children branches have 2 columns, the first cell containing blank space, and the next cell containing the name of the branch ("First Child", "Second Child").

The grandchildren branches have 3 columns, the first and second cell containing blank space, and the the third cell containing the name of the branch (" First Grandchild", "Second Grandchild").

HTML code :

<div id = 0>
    <div id = 1>
    <table id = 1>
    <tbody>
        <tr>
            <td id="content1" 
                <a id="label1" 
                <span id="treeNode1"  
                Main Folder
                </span>
                </a>
           </td>
       </tr>
    </tbody>
    </table>

            <div id = 2>
            <table id = 2>
            <tbody>
                 <tr>
                    <td>    
                    <td id="content2" 
                        <a id="label2" 
                        <span id="treeNode2"  
                            First Child 
                        </span>
                        </a>
                    </td>
                    </td>
                 </tr>
           </tbody>
           </table>

                    <div id = 5>
                    <table id = 5>
                    <tbody>
                         <tr>
                            <td>
                            <td>
                            <td id="content5" 
                                <a id="label5" 
                                <span id="treeNode5"  
                                    First GrandChild 
                                </span>
                                </a>
                            </td>
                            </td>
                            </td>
                         </tr>
                  </tbody>
                  </table>
                  </div>

                    <div id = 6>
                    <table id = 6>
                    <tbody>
                         <tr>
                            <td>
                            <td>
                            <td id="content6" 
                                <a id="label6" 
                                <span id="treeNode6"  
                                    Second GrandChild 
                                </span>
                                </a>
                            </td>
                            </td>
                            </td>
                       </tr>
                </tbody>
                </table>
                </div>
            </div> /* End of division 2 */


            <div id = 3>
            <table id = 3>
            <tbody>
                 <tr>
                        <td>
                        <td id="content3" 
                            <a id="label3" 
                            <span id="treeNode3"  
                                Second Child 
                            </span>
                            </a>
                       </td>
                       </td>
                </tr>
            </tbody>
            </table>
            </div>


            <div id = 4>
            <table id = 4>
            <tbody>
            <tr>
                        <td>
                        <td id="content4" 
                            <a id="label4" 
                            <span id="treeNode4"  
                                Third Child 
                            </span>
                            </a>
                        </td>
                        </td>
            </tr>
          </tbody>
          </table>
          </div>

    </div> /*End of division 1 */
</div> /* End of division 0 */

User2 has a different set of permissions, which enable him to see the folder structure like this :

Main Folder
    - First Child
        -First Grandchild
    - Second Child
    - Third Child

The corresponding table is absent in the html code for this user.

My test case is to check User2 doesn't have access to the second grandchild. This means I need to ensure that particular table doesn't exist on the webpage.

How can I check this in selenium ? I am using JUnit for my test cases. I want to do an "assert" to ensure the second grandchild is not present.

You'll want to check to see if the element is not present or not visible. Calling isElementVisible() inside an assert false should do the trick. Just get the By locator of the elements you want to check.

private boolean isElementVisible(By by)
{
    try
    {
        return driver.findElement(by).isDisplayed();
    }
    catch(NoSuchElementException e)
    {
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM