Java 远程主机上的Selenium 2.0机器人类

Java 远程主机上的Selenium 2.0机器人类,java,selenium,webdriver,testng,awtrobot,Java,Selenium,Webdriver,Testng,Awtrobot,我只是写了一个测试,应该是通过webApp下载pdf文件(是的,我知道,我不应该在selenium上做,但你知道,订单) 我需要什么 对于不同的场景,我必须下载difrent pdf,将其重命名并放置到自定义目录中。所以,我必须处理系统模态窗口。 一切都很好,所以测试在远程主机上运行,当我点击下载文件时,我处理系统模式窗口(我使用robotil包,它是扩展的robot类,允许我们在远程主机上使用robot类),所以我使用robotil类在系统模式上键入文件路径和文件名,然后单击“输入”确认并保存

我只是写了一个测试,应该是通过webApp下载pdf文件(是的,我知道,我不应该在selenium上做,但你知道,订单)

我需要什么

对于不同的场景,我必须下载difrent pdf,将其重命名并放置到自定义目录中。所以,我必须处理系统模态窗口。 一切都很好,所以测试在远程主机上运行,当我点击下载文件时,我处理系统模式窗口(我使用robotil包,它是扩展的robot类,允许我们在远程主机上使用robot类),所以我使用robotil类在系统模式上键入文件路径和文件名,然后单击“输入”确认并保存文件。这是我所需要的一切,而且很有效,那么问题出在哪里呢?这里:应该有人登录到远程主机,如果Im通过rdp登录并查看屏幕(并在主机上执行我的操作),那么一切都很好,但是对于没有人登录的情况,看起来在测试期间WebBrowser没有焦点,所以每次robotil类执行某些操作时,此操作都不会集中在WebBrowser上(应该如此)

测试等级:

@Test
public void compareDeposits() throws Exception {

    HomePage homePage = new HomePage(driver);
    PageFactory.initElements(driver, homePage);

    PrintDepositsPage printDepositsPage = (PrintDepositsPage) homePage.openViaUrl(Data.baseUrl).openViewViaTopMenu(
            ETopMenuItem.PrintDeposits);

    ((PrintDepositsPage) printDepositsPage).goToPrintedDepositsTab();
    printDepositsPage.getPrintedDepositsDateRangeFromInput().click();
    printDepositsPage.getPrintedDepositsDateRangeFromInput().clear();
    printDepositsPage.getPrintedDepositsGoButton().click();
    printDepositsPage.getFirstRecordOnPrintedDepositsTab().click();
    handler.getRobot().mouseClick(371, 274, InputEvent.BUTTON1_MASK);// get focus
    printDepositsPage.getPrintButtonEnabled().click();

    handler.downloadFile("DepositTest");
处理程序对象声明:

class SystemModalWindowHandler {

private RemoteWebDriver driver;
private Date date = new Date();
private DateFormat dateFormat = new SimpleDateFormat("yyy/mm/dd");
private String extendedTestName = dateFormat.format(date).replace("/", ".") + ".pdf";
private Robotil robotil = new Robotil("xxxxx", 6667);

public Robotil getRobot(){
    return robotil;
}

public void downloadFile(String testFileName) throws AWTException, InterruptedException {
    boolean continueBool = true;
    while (continueBool) {
        String pathToTestFile = new String("C:\\DiffPdfData\\" + testFileName + "\\"
                + extendedTestName);
        Thread.sleep(3000);
        for (int i = 0; i < pathToTestFile.length(); i++) {
            System.out.println(KeyStroke.getKeyStroke(pathToTestFile.charAt(i)) + " = "
                    + (int) pathToTestFile.charAt(i));
            if ((int) pathToTestFile.charAt(i) == 58) {

                robotil.pressKey(KeyEvent.VK_SHIFT);
                robotil.pressAndReleaseKey(KeyEvent.VK_SEMICOLON);
                robotil.releaseKey(KeyEvent.VK_SHIFT);
            }
            else {
                robotil.pressAndReleaseKey(KeyEvent.getExtendedKeyCodeForChar((int) pathToTestFile.charAt(i)));
            }
        }
        robotil.pressAndReleaseKey(KeyEvent.VK_ENTER);
        continueBool = false;
    }
类SystemModalWindowHandler{
专用远程网络驱动程序;
私有日期=新日期();
private DateFormat DateFormat=新的SimpleDateFormat(“yyy/mm/dd”);
私有字符串extendedTestName=dateFormat.format(date).replace(“/”,“)+”.pdf”;
私人机器人机器人=新机器人(“xxxxx”,6667);
公共机器人{
返回机器人;
}
public void downloadFile(字符串testFileName)引发AWTException、interruptedeexception{
布尔continueBool=true;
while(continueBool){
String pathToTestFile=新字符串(“C:\\DiffPdfData\\”+testFileName+“\\”
+扩展测试名);
睡眠(3000);
对于(int i=0;i

当没有人登录时,有什么方法可以集中精力使用webbrowser吗?

我相信使用上述策略,如果没有登录用户,您将无法完成此任务。因此,我建议您使用更简单的解决方案

您可以配置Firefox,直接下载文件-

如果不想硬编码浏览器的设置,则可以仅为测试设置特定的FF配置文件,您可以在其中配置要下载文件的位置

FirefoxProfile firefoxProfile = new FirefoxProfile();

firefoxProfile.setPreference("browser.download.folderList",2);
firefoxProfile.setPreference("browser.download.manager.showWhenStarting",false);
firefoxProfile.setPreference("browser.download.dir","c:\\downloads");
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
WebDriver driver = new FirefoxDriver(firefoxProfile);
Chrome驱动程序:

String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
String downloadFilepath=“/path/to/download”;
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default\u目录”,downloadFilepath);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
WebDriver=新的ChromeDriver(cap);

ah,我已经使用了chrome webdriver:(而且我公司的应用程序只支持chrome,据我所知,没有办法更改chrome webdriver的默认下载路径(chrome的DesiredCapabilities不适用于downloadPath)