Window Dialogs & Popup handling in Java or Javascript

后端 未结 1 602
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-16 06:54

I need to manipulate Popups & Download Dialogs of IE browser using either Java or Javascript based automated solution.

I tried with selenium2 but its not working

相关标签:
1条回答
  • 2021-01-16 06:56

    I use selenium 1 and it works well to handle popups in my application.

        //Click on browse file button, open a popup
        selenium.click("//input[@value='Browse...']");
    
        //waiting for popup to load
        selenium.waitForPopUp("_Dialog", "30000");
    
        //selecting the popup by passing window name
        selenium.selectWindow("name=_Dialog");
    
        //click a link inside pop up window
        selenium.click("link=something");
    
        //Put other popup operations here
    
        //click cancel button for pop up
        selenium.click("cancel");
    
        //back to main window
        selenium.selectwindow("null")
    

    To get the message from alert boxes, use selenium.getAlert();. This will return the message contained in the alert box as String.

    Also, sometime you will need to check, whether alert has occurred before switching to it.

            int noofWindows = selenium.getAllWindowNames().length;
            if (noofWindows > 1){
            //selects the second window 
            selenium.selectWindow(selenium.getAllWindowIds()[2]);
            //Prints the message in the alert window
            System.out.println(selenium.getAlert());
            }
    

    If it is not a necessity to run test in IE, use firefox(*chrome) and close all other windows before executing the code.

    I hope this helps you.

    *All the mentioned code is for handling JavaScript pop-ups. I'm not sure whether this will work for Vb-script or not.

    EDIT

    I think IE download pop up is a windows event so cannot be handled by selenium directly, for this you'll have to use Java AWT or AutoIT.

    AutoIT script should be something similiar to

    WinWaitActive(windowTitle)
    ControlClick(windowTitle,"",buttonName)
    

    and save it as IEsave.exe. NOTE: I'haven't tried this AutoIT script.

    now you have execute IEsave.exe from your program. I'm using java here.

    java.lang.Runtime.getRuntime().exec("c:/IEsave.exe");
    

    This will execute the file which in-turn will handle the save button event for windows.

    You can create similar exe files for handling other window's events.

    Hope this solves your problem.

    0 讨论(0)
提交回复
热议问题