When I open the application,it prompts for a username and password. How can I handle this scenario using Selenium java in INTERNET EXPLORER?
-
What is special about Internet Explorer in this scenario ? What about firefox say is that ok. chrome and safari?– Michael DurrantCommented Feb 5, 2016 at 14:38
-
See also sqa.stackexchange.com/q/2755/8992– Michael DurrantCommented Feb 5, 2016 at 14:40
-
yeah i need to run the application in IE only, because application work much faster in IE only– SuryaCommented Feb 5, 2016 at 15:19
-
i'm automating the sanity check– SuryaCommented Feb 5, 2016 at 15:20
-
2I realize this isn't SO, but showing what you've tried, what's working/not working, etc, are all still important for asking a good question . . .– ernieCommented Feb 6, 2016 at 23:16
3 Answers
Selenium doesn't offers much help in handling browser level authentications. You can use java robot class to handle browser level authentication as shown below:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.IOException;
public static void browserLevelAuthentication() throws AWTException{
// initialize the web driver and open the web page
WebDriver _webDriver = new ChromeDriver();
webDriver.navigate().to("www.abc.com");
// create robot for keyboard operations
Robot rb = new Robot();
// Enter user name in username field
StringSelection username = new StringSelection("testUser1");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(username, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
// press tab to move to password field
rb.keyPress(KeyEvent.VK_TAB);
rb.keyRelease(KeyEvent.VK_TAB);
Thread.sleep(2000);
//Enter password in password field
StringSelection pwd = new StringSelection("testPassword");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(pwd, null);
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
//press enter
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
}
Other than java robot class, you can also use Autoit tool to handle authentication.
Or you have to pass the username password in URL like
driver.get("http://<USERNAME>:<PASSWORD>@<URL>")
Here <USERNAME>
is username for that web application
, <PASSWORD>
is password for above mentioned user
and <URL>
is url of the web application
-
Thanks for your answer.. it works for me.. thanks a lot @ sarabjit– SuryaCommented Feb 21, 2016 at 9:01
You need to specify how the browser level authentication box is coming up for starters. Is it a JS pop up or is it a modal window. Depending on this, you can use corresponding methods from Selenium - driver.switch_to.alert
or driver.switch_to.window()
methods- these are in python, but you have corresponding methods in Java also.
Also, you can refer to this question, where it has been discussed in detail- though it is not IE specific.
Though you can use ROBOT class for this, but it highly recommended to avoid ROBOT class uses as much as you can.
If I understood correctly, your website has authentication, it prompts the user name and password. you do not require to do anything with selenium.
You have to add username and password in url for example, your url is
driver.get("http://example.com")
you have to write it in this way
driver.get("http://username:[email protected]")
This will bypass the authentication window.
INFO: This kind of authentication to websites are done, to avoid google ranking, no one will like to rank their testing or dev sites in google