Search code examples
javaseleniumselenium-webdriverassert

How to write a test case for the icon which is not displayed on the page due to permissions


I am trying to write test cases with the different Users with their permissions. I have a situation where there are two users

  1. Admin- with full permissions
  2. Laydown User with limited access.

Now I want to write a testcase when Laydown User is Logged in and hes not able to see his budget Icon.

  1. Budget Icon is not present when he logs in (not hidden as well) so I dont understand how do I uniquely identify it ? I was planning to use IsDisplayed() method but how do I take the path if the icon itself is not present

  2. I though of taking path when Admin logs in but that obviously is not working as the Icon is not present when the Laydown logs in

Is there any way to assert and test this ?

Below is the HTML code when Admin logs in: (You should be able to see Budget here)

<nav id="mainnav-container" ng-if="Engine.hideMainNav != true" class="ng-scope">
    <div class="navbar-header">
        <a href="/app_dev.php/" class="navbar-brand">
        </a>
    </div>
        <div class="btn-group btn-group-vertical">
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine" ui-sref-active="active" navigator-tooltip="Dashboard" type="button" class="btn btn-default inactive tippy_ez0zu8jgw7xqk4ejjan active" dashboard="" style="position:relative;" href="#!/" rel="tooltip">
                <i class="fal fa-fw fa-home" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Dashboard</span>
                             </a>
                     
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine.budget({start_date:'01-01-2020',end_date:'31-12-2020'})" ui-sref-active="active" navigator-tooltip="Budgets" type="button" class="btn btn-default tippy_6jf7no4muityj8c8ngcs" budgets="" style="position:relative;" href="#!/budget/" rel="tooltip">
                <i class="fal fa-fw fa-calculator" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Budgets</span>
                             </a>
                     
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine.activation({start_date:'01-01-2020',end_date:'31-12-2020'})" ui-sref-active="active" navigator-tooltip="Activations" type="button" class="btn btn-default tippy_ieku06frmxanoz20kb5i1" activations="" style="position:relative;" href="#!/activation/?end_date=31-12-2020&amp;start_date=01-01-2020" rel="tooltip">
                <i class="fal fa-fw fa-list" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Activations</span>
                             </a>
                     
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine.laydown({'start_date':'01-05-2022','end_date':'30-09-2022'})" ui-sref-active="active" navigator-tooltip="Activities" type="button" class="btn btn-default tippy_3f19a78v3ungek4l9ejg6" activities="" style="position:relative;" href="#!/activity/?end_date=30-09-2022&amp;start_date=01-05-2022" rel="tooltip">
                <i class="fal fa-fw fa-heartbeat" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Activities</span>
                             </a>
                     
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine.crmLite" ui-sref-active="active" navigator-tooltip="Manage Users" type="button" class="btn btn-default tippy_ffkxpknzd1bt8m1jfoso6" manage="" users="" style="position:relative;" href="#!/manage-users/" rel="tooltip">
                <i class="fal fa-user-cog" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Manage Users</span>
                             </a>
                     
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine.reporting" ui-sref-active="active" ng-init="Engine.sidebarloop6 = true" ng-click="Engine.sidebarloop6 = !Engine.sidebarloop6" navigator-tooltip="Reports" type="button" class="btn btn-default tippy_stuygjcav39xe0mih4g92" reports="" style="position:relative;" href="#!/reporting-suite/" rel="tooltip">
                <i class="fal fa-fw fa-chart-line" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Reports</span>
                             </a>
                     
        
    </div>
    
    </nav>

And this is the HTML when Laydown logs-in : He can see only Dashboard and activities

<nav id="mainnav-container" ng-if="Engine.hideMainNav != true" class="ng-scope">
    <div class="navbar-header">
        <a href="/app_dev.php/" class="navbar-brand">
        </a>
    </div>
        <div class="btn-group btn-group-vertical">
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine" ui-sref-active="active" navigator-tooltip="Dashboard" type="button" class="btn btn-default inactive tippy_oq042rw1d68c75d0f67aho active" dashboard="" style="position:relative;" href="#!/" rel="tooltip">
                <i class="fal fa-fw fa-home" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Dashboard</span>
                             </a>
                     
                                            
                                            
                                                            
            <!-- ui-sref-opts="{reload: true}" -->
            <a ui-sref="base.engine.laydown({'start_date':'01-05-2022','end_date':'30-09-2022'})" ui-sref-active="active" navigator-tooltip="Activities" type="button" class="btn btn-default tippy_3npvbbn0bod30zndtigtho" activities="" style="position:relative;" href="#!/activity/?end_date=30-09-2022&amp;start_date=01-05-2022" rel="tooltip">
                <i class="fal fa-fw fa-heartbeat" aria-hidden="true"></i> 
                <span ng-show="Engine.offsetMainnav" class="ng-hide" style="">&nbsp;Activities</span>
                             </a>
                     
                                            
                                            
        
    </div>
    
    </nav>

Solution

  • You can write a method to search for an element and return true if it is available else return false.

    public static boolean isElementDisplayed() {
        try {
            driver.findElement(By.xpath("//span[contains(text(),'Budgets')]"));
            return true;
        } catch (org.openqa.selenium.NoSuchElementException e) {
            return false;
        }
    }
    

    In your tests,

    @Test
    public void laydownUserCheck() {
        if (isElementDisplayed()) {
            Assert.fail("Displaying the budget icon for laydown user.");
        }
    }
    
    @Test
    public void adminUserCheck() {
        if (!isElementDisplayed()) {
            Assert.fail("Not displaying the budget icon for admin user.");
        }
    }