窗口对话框&用Java或Javascript处理弹出窗口

时间:2011-04-25 06:38:20

标签: java javascript selenium automation

我需要操纵Popups&使用Java或Java下载IE浏览器的对话框 基于Javascript的自动化解决方案。

我尝试使用selenium2,但它没有正常工作,因此对此有任何其他建议。 实际上,selenium2无法正确处理警报/下载对话框 我正在考虑使用其他一些javascript / java解决方案。

使用下载对话框:我需要将下载的文件保存到特定位置。 使用警报对话框:我需要检查显示的消息并单击特定按钮。

任何建议表示赞赏。 感谢。

1 个答案:

答案 0 :(得分:2)

我使用selenium 1,它可以很好地处理我的应用程序中的弹出窗口。

    //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")

要从警告框中获取消息,请使用selenium.getAlert();。这将以警告框的形式返回包含在消息框中的消息。

此外,有时您需要检查是否已在切换之前发出警报。

        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());
        }

如果不需要在IE中运行测试,请在执行代码之前使用firefox(* chrome)并关闭所有其他窗口。

我希望这会对你有所帮助。

*所有提到的代码都是用于处理JavaScript弹出窗口。我不确定这是否适用于Vb脚本。

修改

我认为IE下载弹出窗口是一个windows事件所以不能直接由selenium处理,为此你必须使用Java AWT或AutoIT。

AutoIT脚本应该与

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

并将其另存为IEsave.exe。注意:我没有尝试过这个AutoIT脚本。

现在您已从程序中执行IEsave.exe。我在这里使用java。

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

这将执行文件,该文件又将处理windows的保存按钮事件。

您可以创建类似的exe文件来处理其他窗口的事件。

希望这能解决你的问题。