[英]Unable to handle Windows 10 pop-ups using Robot class
I am trying to run the below code in Internet Explorer, Windows 10.我正在尝试在 Windows 10 的 Internet Explorer 中运行以下代码。
----------------------------Test---------------public class SampleTest { ---------------测试---------------公共类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 class 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);
}
}` }`
When I try to run the above script in Windows 7(Desktop), it is working fine.当我尝试在 Windows 7(桌面)中运行上述脚本时,它工作正常。 But when I try to run the same in Windows 10(laptop), it is not working.
但是当我尝试在 Windows 10(笔记本电脑)中运行它时,它不起作用。
Can someone please help.有人可以帮忙吗。 Thanks
谢谢
Instead of using a hack like Java Robot classes for Basic Auth, what you really want to do is use a proxy.您真正想做的是使用代理,而不是使用像 Java Robot 类这样的 hack 进行基本身份验证。 Here's a solution using the browserup proxy .
这是使用browserup 代理的解决方案。
First of all add the browserup proxy dependency to your maven POM.xml (this is assuming you are using maven, it's pretty standard with Java projects though).首先,将 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>
Then use the browserup proxy in your tests.然后在您的测试中使用 browserup 代理。 First of all the imports you are going to need to run this are as follows:
首先,您需要运行的导入如下:
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;
Then an example test that you should be able to copy/paste and try out is:然后,您应该能够复制/粘贴并尝试的示例测试是:
// 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();
It should be a relatively simple job to tweak the autoAuthorization line to make it work for your domain and your associated basic auth credentials.调整 autoAuthorization 行以使其适用于您的域和相关的基本身份验证凭据应该是一项相对简单的工作。
The advantages to using a proxy are:使用代理的优点是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.