5

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.

MorkPork
  • 844
  • 3
  • 20
  • 41

6 Answers6

1

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.

Arie Laxed
  • 103
  • 1
  • 4
  • Thanks for the suggestion mate, I did think about doing this but unfortunately I'm already using my environment capability for its intended purpose (irritatingly our systems here are set up so that to access the dev version of the website you must be on a machine in the dev domain, AND go to the URL relevant URL, and is the same for all the envs, test, uat, preprod etc, rubbish!) – MorkPork Dec 16 '13 at 15:51
1

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());
    }
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
1

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.

Brent Thoenen
  • 402
  • 2
  • 11
1

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.

Jin Thakur
  • 2,711
  • 18
  • 15
0

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.

Nathan Merrill
  • 7,648
  • 5
  • 37
  • 56
  • Have updated my initial question with a little more background, apologies I had left it a little lacking. – MorkPork Aug 02 '13 at 14:45
  • I saw your expanded explanation, and I believe your only solution would be to use Java Robots to automate that. I do not believe that there is any way to do such a thing with pure Selenium. – Nathan Merrill Aug 02 '13 at 16:14
  • Java Robots? How do you mean? And also, can I make one to get me coffee?! @:D – MorkPork Aug 02 '13 at 16:20
  • You can also try Sikuli - refer sikuli.org – Akbar Aug 03 '13 at 12:09
0

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.

VolleyJosh
  • 161
  • 1
  • 1
  • 13