我正在尝试在Windows 10的Internet Explorer中运行以下代码。
---------------------------- Test ---------------公共类SampleTest {
public static void main(String args[]) throws AWTException, InterruptedException{
System.setProperty("webdriver.ie.driver", "path//IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("url");
HelperMethods.validateSplash();
}
}` -------------------- HelperMethods -----------
` 公共类HelperMethods {
public static void validateSplash() throws AWTException, InterruptedException{
HelperMethods.ctrlV("username");
HelperMethods.pressTab();
Thread.sleep(2000);
HelperMethods.ctrlV("password");
HelperMethods.pressEnter();
}
private static void ctrlV(String stringToPaste) throws AWTException{
Robot robot = new Robot();
StringSelection strToPaste = new StringSelection(stringToPaste);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strToPaste, null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
}
private static void pressTab() throws AWTException{
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
private static void pressEnter() throws AWTException{
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}`
当我尝试在Windows 7(桌面版)中运行上述脚本时,它运行正常。但是,当我尝试在Windows 10(笔记本电脑)中运行相同的软件时,它将无法正常工作。
有人可以帮忙吗?谢谢
答案 0 :(得分:1)
您真正想要做的不是使用Java Robot类之类的hack进行基本身份验证,而是真正使用代理。这是使用browserup proxy的解决方案。
首先将browserup代理依赖项添加到您的maven POM.xml中(这是假设您使用的是maven,但这在Java项目中是非常标准的)。
<dependency>
<groupId>com.browserup</groupId>
<artifactId>browserup-proxy-core</artifactId>
<version>1.0.0</version>
<scope>test</scope>
</dependency>
然后在测试中使用browserup代理。首先,您需要执行以下操作:
import com.browserup.bup.BrowserUpProxy;
import com.browserup.bup.BrowserUpProxyServer;
import com.browserup.bup.client.ClientUtil;
import com.browserup.bup.proxy.auth.AuthType;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
然后您应该能够复制/粘贴并尝试的示例测试是:
// Start up the browserup proxy server
BrowserUpProxy browserUpProxyServer = new BrowserUpProxyServer();
//Specify domain that uses basic auth, then the username and password followed by auth type
browserUpProxyServer.autoAuthorization("the-internet.herokuapp.com", "admin", "admin", AuthType.BASIC);
browserUpProxyServer.start();
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(browserUpProxyServer);
// Configure IEDriver to use the browserup proxy
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.setProxy(seleniumProxy);
WebDriver driver = new InternetExplorerDriver(ieOptions);
//Go to a site with basic auth enabled and check it all works
driver.get("https://the-internet.herokuapp.com/basic_auth");
//Clean up after test has finished
driver.quit();
browserUpProxyServer.stop();
调整autoAuthorization行以使其适用于您的域和关联的基本身份验证凭据,应该是相对简单的工作。
使用代理的优点是: