简体   繁体   中英

Unable to handle Windows 10 pop-ups using Robot class

I am trying to run the below code in Internet Explorer, Windows 10.

----------------------------Test---------------public class 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-----------

` public class 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. But when I try to run the same in Windows 10(laptop), it is not working.

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. Here's a solution using the browserup proxy .

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).

<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. 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.

The advantages to using a proxy are:

  • This is cross browser compliant
  • This is cross OS compliant
  • This will work with local and remote WebDriver instances (However with remote ones the machine that the browser is running on will need access to the machine that the proxy is running on and you need to pass in a valid IP address for the proxy, not localhost)
  • It's a lot less code than various Robot class hacks to try and and click on varying different OS level dialogue boxes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM