Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running IE as a different user with Selenium Webdriver in Java

Does anyone know how I could get selenium to run an IE session utilising the "Run as different user" function using JAVA? I have no idea how I'd even go about setting this and google isn't bringing up anything so maybe I'm searching for the wrong thing somewhere?

To expand a bit on why I need to do this, the website I'm testing is an internal platform and uses a users login credentials/windows account details to determine their role, what links they have, data they can view etc.

Currently the only way I can run a test as a specific user (manager, data administrator etc) is to log into the machine as that user before running the test. This isn't really possible when I have multiple user accounts to test and am running Webdriver with GRID2 (so I have a bank of machines picking up tests from a queue as they come up).

When running tests manually I can simply right click and select the "run as different user" option on the ie shortcut, so figured if I could find a way to replicating this when running it via webdriver this would solve my problem.

Thanks for any suggestions or assistance.

like image 648
MorkPork Avatar asked Oct 03 '22 08:10

MorkPork


2 Answers

Perhaps you can also setup the machines in your GRID with different users and reflect that in the environment Capability. Of course, it does require more machines in your GRID. I haven't tried this myself, though.

like image 130
Arie Laxed Avatar answered Oct 12 '22 12:10

Arie Laxed


The only solution I got is to use robot class to open new session. Actually IE is not providing any shortcut to open new session so we have to do this by robots.

This code will open a new session for you. Best of luck

    System.setProperty("webdriver.ie.driver","./IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    driver.get("http://www.google.com/");

    try{
        Robot robot=new Robot();
        robot.keyPress(KeyEvent.VK_ALT);
        Thread.sleep(3000);
        robot.keyPress(KeyEvent.VK_F);
        robot.keyRelease(KeyEvent.VK_ALT);
        robot.keyRelease(KeyEvent.VK_F);
        robot.keyPress(KeyEvent.VK_I);
        robot.keyRelease(KeyEvent.VK_I);

    }
    catch(Exception ex){
        System.out.println(ex.getMessage());
    }
like image 26
Shubham Jain Avatar answered Oct 12 '22 10:10

Shubham Jain