使用 Selenium 和 AutoIt 通过远程桌面实现自动化 [英] Automate through a remote desktop using Selenium and AutoIt

查看:96
本文介绍了使用 Selenium 和 AutoIt 通过远程桌面实现自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动化某些需要通过远程桌面连接的任务.

I want to automate certain tasks which needs it to go through Remote Desktop Connection.

我会分享我写到现在的代码.

I will share the code which I wrote till now.

public class MainClass
{
  static WebDriverWait  wait;
  static WebDriver driver;
  public static void main(String args[])
  {
    driver = new HtmlUnitDriver(true);
    driver.get("https://mysite");
    WebElement submit_element=driver.findElement(By.id("Log_On"));
    driver.findElement(By.id("Enter user name")).sendKeys("my_username");
    driver.findElement(By.name("passwd")).sendKeys("my_password");
    submit_element.click();
    driver.findElement(By.id( "folderLink_0")).click();
    driver.findElement(By.id( "folderLink_2")).click();
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
    System.out.println(driver.getPageSource());
    driver.findElement(By.id("idCitrix.M")).click();
    System.out.println(driver.getPageSource());
  }
}

代码行

`driver.findElement(By.id("idCitrix.M")).click();`

在新窗口中打开远程桌面.

opens the remote desktop in a new window.

线

`System.out.println(driver.getPageSource());`
is retrieving the same code in both places.

我相信这不能仅由 Selenium 完成.通过浏览互联网,我了解到使用 AutoIt 可以做到这一点.

I believe this cannot be done solely by Selenium. By browsing through the Internet I learnt it is possible to do this using AutoIt.

我该怎么做?

推荐答案

Selenium 可用于自动化 Web 浏览器的部分,而 AutoIT 应用于自动化 Windows 应用程序(在您的情况下,它可能登录远程机).

Selenium can be used for the parts that are automating your web browser whereas AutoIT should be used for automating Windows applications (in your case, its probably logging into the remote machine).

此链接提供了有关如何将 AutoIT 与 Selenium 一起使用的好信息:http://www.toolsqa.com/selenium-webdriver/autoit-selenium-webdriver/

This link provides good information on how to use AutoIT alongwith Selenium: http://www.toolsqa.com/selenium-webdriver/autoit-selenium-webdriver/

这是你必须做的:

下载/安装 AutoIT
您将能够使用 AutoIT SciTe Editor 创建 .au3 脚本编译 .au3 脚本会给你一个 .exe 文件然后你可以使用

Download/install AutoIT
You will be able to create .au3 scripts using AutoIT SciTe Editor Compiling the .au3 script will give you a .exe file Then you can invoke the .exe file from your Selenium script using

Runtime.getRuntime().exec("D:AutoItAutoItTest.exe");

您可以使用 AutoIT 窗口信息 (x86) 或 (x64) 获取窗口的属性.例如,窗口的标题/状态栏.

You can get the properties of a window using the AutoIT Window Info (x86) or (x64). Example, title / status bar of a window.

AutoIT 还具有 Au3 Recorder,以便您可以记录与远程桌面相关的操作.

AutoIT also has Au3 Recorder so that you can record your actions that are related to the remote desktop.

以下是自动进行 Http 身份验证的示例脚本:

Below is a sample script that automates Http authentication:

WinWaitActive("Web page title","","10")
If WinExists("Web page title") Then
Send("userid{TAB}")
Send("password{Enter}")
EndIf

下面的脚本获取出现在记事本状态栏中的文本:

Below script gets the text present in the status bar of Notepad:

WinWaitActive("Untitled - Notepad", "", 30) 
Local $hWnd = WinGetHandle("Untitled - Notepad")
Local $sText = StatusbarGetText("Untitled - Notepad","",2)
ConsoleWrite($sText) 

希望这些信息有帮助!

更新:进一步搜索后,发现这个库 AutoITx4Java - https://code.google.com/p/autoitx4java/

Update: Upon further searching, found this library AutoITx4Java - https://code.google.com/p/autoitx4java/

  1. 下载雅各布,AutoIT(请参阅上面的链接)
  2. 将 jacob.jar 和 autoitx4java.jar 添加到您的库路径中.
  3. 将 jacob-1.15-M4-x64.dll 文件放在您的库路径中.

示例代码

File file = new File("lib", "jacob-1.15-M4-x64.dll"); //path to the jacob dll
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());

AutoItX x = new AutoItX();
String notepad = "Untitled - Notepad";
String testString = "this is a test.";
x.run("notepad.exe");
x.winActivate(notepad);
x.winWaitActive(notepad);
x.send(testString);
Assert.assertTrue(x.winExists(notepad, testString));
x.winClose(notepad, testString);
x.winWaitActive("Notepad");
x.send("{ALT}n");
Assert.assertFalse(x.winExists(notepad, testString));

这篇关于使用 Selenium 和 AutoIt 通过远程桌面实现自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆