-1

I am writing my automated test scripts using selenium-webdriver, phantomJS and mocha.

My script file is javascript file by nature.

I want to wait till an element() becomes completely visible. After it becomes visible, the element will be clicked.

Let me explain in details:

There are some menus and submenus. The menu is collapsible in nature. When I click on a menu, then its corresponding submenus are displayed.

My following script first iterates(And clicks) through the menu and then iterates and (should be) clicking the submenu.

for(var iMajor = 2; iMajor <= majorLinkLast ; iMajor++)
{   
    (function(iMajor){
        majorMenuXPath = "//ul[contains(@id, 'side-menu')]/li["+iMajor+"]/a";
        if(iMajor != 2)
        {
            driver.findElement(By.xpath(majorMenuXPath)).click().then((function(iMajor){
                for(var iMinor = 1; iMinor <= minorSize[iMajor] ; iMinor++)
                {
                    (function(iMajor, iMinor){
                        minorMenuXPath = "//ul[contains(@id, 'side-menu')]/li["+iMajor+"]/ul/li["+iMinor+"]/a";
                        driver.findElement(By.xpath(minorMenuXPath)).then(function(eleMinor){
                            driver.wait(function(){
                                    return eleMinor.isDisplayed();
                                }, 20000).then(function(){
                                    eleMinor.isDisplayed().then(function(stat){
                                    console.log(stat);
                                });
                            });
                        });
                    })(iMajor,iMinor)
                }
            })(iMajor));
        } 

    })(iMajor)
}

But I am getting message like this:

Track Revenue
Track Revenue
Track Revenue
Track Revenue
Track Revenue
Track Revenue
Campaigns
    1) View page by clicking menu


  2 passing (1m)
  1 failing

  1) TrackRevenue Click Menu Test View page by clicking menu:
     Error: timeout of 50000ms exceeded. Ensure the done() callback is being cal
led in this test.

Why am I getting such output?

The title should have been different.

Please help.

1 Answers1

0

Try to wait for element to be displayed before clicking on it. When you say that you are getting the displayed status as true, selenium waits for few seconds (default timeout interval) before throwing error and meanwhile your element gets displayed, so its printing true in console. In order to click on the required element, first wait for it to get displayed. Here's how to do it -

var ele = driver.findElement(By.xpath(minorMenuXPath));
driver.wait(function() {
    return ele.isDisplayed();
}, 20000)
.then(function(){
    ele.click();
});

Hope this helps.

giri-sh
  • 6,934
  • 2
  • 25
  • 50