Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a Liferay popup (in new browser window) using Selenium2 with the FirefoxDriver?

I am attempting to automate some test cases on a Liferay portal server using Selenium 2. Many Liferay actions open up new browser window pop up (like user impersonation). Here is an example link (notice the target="_blank"):

<a title="(Opens New Window)" target="_blank" id="_125_xafr" 
    href="/web/guest?doAsUserId=xBRUWI85MvM%3D" class="taglib-icon aui-focus" 
    tabindex="0" role="menuitem"> 
  <img alt="" src="/html/themes/control_panel/images/common/impersonate_user.png" class="icon"> 
  Impersonate User 
  <span class="opens-new-window-accessible">(Opens New Window)</span>
</a>

Switching to the pop up window context is fairly straight forward:

String currentWindowHandle = driver.getWindowHandle();
if ( log.isDebugEnabled() ) log.debug( "currentWindowHandle='" + currentWindowHandle + "'" );
for ( String windowHandle : driver.getWindowHandles() ) {
    if ( ! windowHandle.equals( currentWindowHandle ) ) {
        boolean impersonationSuccess = false;
        if ( log.isDebugEnabled() ) log.debug( "checking '" + windowHandle + "' impersonation alert notice" );
        driver.switchTo().window( windowHandle );
        try {
            // wait for body to ensure page has loaded before checking to see if its impersonation page.
            WebElement body = waitForElement( driver, By.xpath( "//body" ) ); 
            WebElement noticeMessage = body.findElement( 
                    By.xpath( "div[@class='popup-alert-notice' and a='Be yourself again.']/span[@class='notice-message']" ) );
            if ( noticeMessage.getText().indexOf( "You are impersonating " + user.firstName + " " + user.lastName ) >= 0 ) {
                impersonationSuccess = true;
                break;
            }
        }
        catch ( NoSuchElementException e ) {
            if ( log.isDebugEnabled() ) {
                log.debug( "did not find impersonation window '" + windowHandle + "'" );
            }
        }
        finally { 
            if ( ! impersonationSuccess ) {
                driver.switchTo().window( currentWindowHandle );
            }
        }
    }
}
return currentWindowHandle;

However, when I am done with impersonation, I need to close the pop up. According to the api for WebDriver.close(), it will:

Close the current window, quitting the browser if it's the last window currently open.

If I am reading this correctly, then it should just close the pop up window, not the Firefox instance (as long as I have another window open, which I do, because this was just a pop up). However, when I call close from the context of the pop up it always fails with:

org.openqa.selenium.WebDriverException:  
org.apache.http.conn.HttpHostConnectException: 
Connection to http://localhost:7055 refused

I found some references to this being a bug in the FirefoxDriver. Does anyone have suggestions or perhaps a workaround? I guess I could leave the pop up open until the entire test case is complete, but with the number of operations that cause pop ups in my test suite, this could rapidly become infeasible.

------------- EDIT --------------

An extremely simplified test case:

@Test
public void testPopupClose() {
    WebDriver driver = new FirefoxDriver();
    driver.get( "http://lucastheisen.com/test/lucas_test_page.html" );
    driver.findElement( By.id( "popup_link" ) ).click();

    String mainWindowHandle = driver.getWindowHandle();
    System.out.println( "currentWindowHandle='" + mainWindowHandle + "'" );
    boolean foundPopup = false;
    for ( String windowHandle : driver.getWindowHandles() ) {
        if ( !windowHandle.equals( mainWindowHandle ) ) {
            System.out.println( "checking '" + windowHandle + "' for taunt" );
            driver.switchTo().window( windowHandle );
            try {
                driver.findElement( By.id( "taunt" ) );
                foundPopup = true;
                break;
            }
            catch ( NoSuchElementException e ) {
                System.out.println( "'" + windowHandle + "' is not taunt window" );
            }
            finally {
                if ( !foundPopup ) {
                    driver.switchTo().window( mainWindowHandle );
                }
            }
        }
    }

    if ( foundPopup ) {
        System.out.println( "found my popup, now try to close it..." );
        driver.close();
    }

    System.out.println( "now try to continue working in original window" );
    driver.switchTo().window( mainWindowHandle );
    driver.findElement( By.id( "popup_link" ) );
    driver.close();

    assertTrue( true );
}

Seem to indicate that this is not really an issue with the FirefoxDriver. This shows that just creating a popup, switching to it, then closing it, then resuming work in the main window does not cause firefox to crash. This means the cause is much more complicated and I cant think of a way to create a simple test case. My real code is basically a framework for working with Liferay. It is intended to be a base class of all my test cases providing utility methods like impersonateUser( LiferayUser user ). I will post my code here, but highly doubt anyone would want to put the effort into looking into this. For now i have a workaround in that i can close the original window and just continue to work in the popup window from there on out. If I come up with a simplified test case to demonstrate this issue, I will edit this post again.

------------- EDIT 2 --------------

I am now thinking this has something to do with the debugger. I am using eclipse and if I am debugging, and am stepping through the code between the close() and the switchTo(), it fails consistently. You should be able to reproduce by inserting a break point in my simplified example test case above at this line:

System.out.println( "now try to continue working in original window" );

When the code stops for that breakpoint, then you get the error... Not sure what to do about this as far as possibly filing a bug. Any suggestions?

------------- EDIT 3 --------------

For tracking purposes, here is the bug I opened on this issue:

http://code.google.com/p/selenium/issues/detail?id=1378

like image 862
Lucas Avatar asked Mar 08 '11 20:03

Lucas


People also ask

What is browser window popup in selenium?

Overview In this article, we will discuss the concept of browser window popup also the practical implementation to handle the browser window popup with selenium web driver. 2. Browser window Popup Browser window popup is a window popup that suddenly pops up on after clicking on some link or on selecting an option.

When do we need to handle this browser window popup?

When do we need to handle this browser window popup: if the scenario is something like: you click on some link on the main window (parent window) and some other screen pops out just another webpage (child window ), now child window demands to submit some important information and press OK once information is filled. 3.

How to close the pop up window in SharePoint?

Perform some actions in the popup window such as get the title or close the button/tab etc. //close the pop up window now by using driver.close () method. tip: Make sure, you are using close () to close the pop up. If you use quit () it will close parent and child both. This is the basic difference between close () & quit (). 5.

What is a browser popup?

Browser window Popup Browser window popup is a window popup that suddenly pops up on after clicking on some link or on selecting an option. It just likes another web page where maximization and minimization of the web page can be done. This is having its own title and url or it could be an advertisement pop up also.


1 Answers

Hello Liferay popup isnot a seperate popup window of firefix that you will be able to close. It is a div element of HTML that seems like a popup. For that you need to close it via javascript code.

In general scenarios every popup will have a developer made close button, you can find its path through selenium IDE and can can simulate click request on same through webdriver.

Repeating again. LiferayWindow popup are not actual browser based browser, they are just illusion to same.

I don't think this is bug.

edit**

I thought this was due to javascript window popup but this is not the case.

like image 159
2 revs Avatar answered Oct 25 '22 15:10

2 revs