简体   繁体   中英

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.

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.

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());
    }

If worse comes to worst, you could just setup a file with your different usernames and passwords, read them in, determine the user you need through a factory class, and then select the user per test based on the parameters you need to look at. If you do it this way, when you are running on multi-threads, it will still pull in the correct user to login with.

I don't think this is your primary goal, but a secondary solution at minimum.

Its quite possible i have done it may time. Easy way If you run selenium through eclipse. Just open eclipse IDE by right click open as different user.

The harder way create a Java>Runnable JAR file as mentioned in the article below.

https://www.joecolantonio.com/selenium-executable-java-test/

you should be admin on local machine to do it and should have windows account iusername and password for user:userA user:userA1 and so on. open multiple command prompt

C:\>runas /user:domainname\userA cmd
C:\>runas /user:domainname\userA1 cmd
C:\>runas /user:domainname\userA2 cmd

or

C:\>runas /user:domainname\username cmd

Enter the password for administrator: Attempting to start cmd as user "techblogger-pc\\administrator" ... then type same jar file in command window Like c:\\runnableselenium.jar and bomb you have all jar running with different credential Find a way to see the logged in user name on test page somehow.

I don't know why you would want to do this, and I don't believe it is possible. However, I've made a few guesses at your end goal, with solutions for each.

If you want to start fresh (with no cache or anything), then simply start up a new IEDriver instance.

If you want to start it with different settings, you can pass in your desired capabilities.

If there is something else entirely you are trying to do, feel free to reply, and explain your situation.

Looks like it's possible to do this using Windows Credential Manager . I was able to manually set the username & password for the site, then drive there with Selenium and the alternate credential I specified was used to authenticate.

The next step is to set the credential at runtime, which should be possible with powershell .

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