Webdriver - HTTP Authentication Dialog Box

I have a very simple selenium-webdriver script. I would like to do HTTP authentication using webdriver.

Script:

WebDriver driver = new FirefoxDriver(); driver.get("http://www.httpwatch.com/httpgallery/authentication/"); driver.findElement(By.id("displayImage")).click(); Thread.sleep(2000); driver.switchTo().alert().sendKeys("httpwatch"); 

Problem:

 driver.switchTo().alert().sendKeys("httpwatch"); 

throws

org.openqa.selenium.NoAlertPresentException: no warning

Question:

  • Does Webdriver only alert as an alert?
  • What are my options to automate this without using AutoIt OR http: // username: password @somesite

EDIT

Alert has a method below and doesn't seem to be implemented yet.

 driver.switchTo().alert().authenticateUsing(new UsernameAndPassword("username","password")) 
+5
source share
3 answers

The problem is that this is not a javascript popup, so you cannot manipulate it through selenium alert() .

If both AutoIt options and sending credentials to a URL (the easiest option - just open the URL and click “Show Image”) and not the options for you, another approach could be to use AutoAuth firefox addon to automatically send earlier saved credentials:

AutoAuth automatically sends HTTP authentication dialogs when you choose to have your browser save your login information. (If you already told the browser that your username and password, and you told him to remember this username and password, why not just send it automatically instead of asking you every time?)

After the response suggested in HTTP Basic Auth via URL in Firefox does not work? thread:

  • Install the AutoAuth Firefox plugin;
  • Visit a site where authentication is required. Enter your username and password and remember to save your credentials;
  • Save the AutoAuth installation file to your hard drive: on the plugin page, right-click "Add to Firefox" and "Save Link As";
  • Create a Firefox web browser as follows:
 FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("default"); File pluginAutoAuth = new File("src/test/resources/autoauth-2.1-fx+fn.xpi"); firefoxProfile.addExtension(pluginAutoAuth); driver = new FirefoxDriver(firefoxProfile); 

In addition, similar to the AutoIt option, you can use the sikuli screen recognition and automation tool to send credentials to a pop-up window.


Also see other ideas and options:

+5
source

Basic / NTLM popup is a browser dialog. WebDriver (Selenium 2.0) cannot interact with such dialog boxes. The reason for this is that WebDriver aims only to simulate user interactions with websites, and browser windows are not currently in this area. JavaScript dialogs are part of the website, so WebDriver can handle them. In Selenium 1.0, you can perform basic authentication.

So how to solve this problem? 1) Authentication via URL http://username: password@website.com 2) Use a browser plugin that will handle Basic / NTLM authentication. 3) Use a local proxy server that will change the request header and pass username / password. 4) Use a robot, for example, AutoIt or some Java library.

Option 1: The simplest and has the least impact on the system / test. Option 2: has a high browser effect as download plugins. Each browser also uses its own plugin, and it is possible that the required plugin for a particular browser is not available. Option 3: works well with HTTP, but HTTPS requires special certificates, so it is not always an option. Not so much affects both the system and the test. Option 4: Mimic keyboard keys, this is a solution, but prone to errors. It only works when the dialog box has focus, and it is possible that this is not always the case.

+3
source

I ran into the same problem and got a specific solution using the robot class. His solution or solution, let's see, but it works.

 public class DialogWindow implements Runnable { @Override public void run() { try { entercredentials(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void entercredentials() InterruptedException { Thread.sleep(5000); try { enterText("username"); enterSpecialChar(KeyEvent.VK_TAB); enterText("password"); enterSpecialChar(KeyEvent.VK_ENTER); } catch (AWTException e) { } } private void enterText(String text) throws AWTException { Robot robot = new Robot(); byte[] bytes = text.getBytes(); for (byte b : bytes) { int bytecode = b; // keycode only handles [AZ] (which is ASCII decimal [65-90]) if (bytecode> 96 && bytecode< 123) bytecode = bytecode - 32; robot.delay(40); robot.keyPress(bytecode); robot.keyRelease(bytecode); } } private void enterSpecialChar(int s) throws AWTException { Robot robot = new Robot(); robot.delay(40); robot.keyPress(s); robot.keyRelease(s); } } 

What to call him

  WebDriver driver = new FirefoxDriver () // or any other driver with capabilities and other required stuff

 (new Thread (new DialogWindow ())). start ();

 driver.get (url);
0
source

Source: https://habr.com/ru/post/1210805/


All Articles