How to perform Basic Authentication for FirefoxDriver, ChromeDriver and IEdriver in Selenium WebDriver?

前端 未结 7 1108
一个人的身影
一个人的身影 2020-11-27 16:46

I am using the Selenium-Firefox-driver and Selenium-Chrome-Driver version 2.0a5 (Web Driver API), and I am trying to test a web app that h

相关标签:
7条回答
  • 2020-11-27 16:56

    Add this New Firefox Profile on your code

    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile myprofile = profile.getProfile("myProjectProfile"); //replace "myProjectProfile" with your profile"
    WebDriver driver = new FirefoxDriver(myprofile);
    

    Firefox configuration settings

    This works fine without prompting any authentication when you do the following settings..

    • Type "about:config" on your FF url
    • Now type "Proxy" in the search field
    • Make sure "signon.autologin.proxy" is set "true" (By default it is "false")

    Load Default/Custom Chrome Profile to run tests using Selenium WebDriver


    1. Download chromedriver.exe
    2. Extract the chromedriver_win_26.0.1383.0.zip folder and locate .exe file to C:/ folder

    Add this Script on your JAVA code

    DesiredCapabilities capability = DesiredCapabilities.chrome();
    System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
    capability.setCapability("chrome.switches", Arrays.asList("–disable-extensions"));
    capability.setCapability("chrome.binary", "C:/Users/user_name/AppData/Local/Google/Chrome/Application/chrome.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data/Default");
    driver = new ChromeDriver(capability);
    

    Note: IE doesn't need profile setup to run tests because they run on Server user while Firefox and Chrome works with binary.

    0 讨论(0)
  • 2020-11-27 16:57

    There is a solution for performing authentication with Selenium 1.x by manually setting the HTTP headers at http://mogotest.com/blog/2010/06/23/how-to-perform-basic-auth-in-selenium but I don't think this is transferable to Selenium 2, as you don't have access to the headers.

    According to the information here 'Basic Authentication support for Selenium 2' was added in Selenium 2 Beta 2 but looking through the source code I can only see it implemented as a way of securing Remote Selenium Servers against anonymous access.

    So I think the answer is that BASIC HTTP authentication is not currently supported.

    0 讨论(0)
  • 2020-11-27 17:00

    True, BASIC HTTP authentication is not currently supported but I got it working now for FF and for Chrome.

    The code I wrote in the questions works for those drivers. I just tried using FF3.6 as Firefox default browser (installed in Firefox folder) instead of FF4 (not supported yet). For IE, i may try to disable the authentication through Windows Registry.

    This page http://code.google.com/p/selenium/issues/detail?id=34 may help.

    0 讨论(0)
  • 2020-11-27 17:03

    I was not able to use the basic authentication with Selenium 2 and Chrome (Due a bug with Chrome), so I created an extension for Chrome that sends the basic authentication credentials automatically (See https://chrome.google.com/webstore/detail/basic-authentication-auto/dgpgkkfheijbcgjklcbnokoleebmeokn).

    0 讨论(0)
  • 2020-11-27 17:07

    For more portability, this can be handled by stub API and using Alert.

    Example Java code (sample):

    import org.openqa.selenium.Alert;
    import org.openqa.selenium.security.Credentials;
    public void authenticateUsing(Credentials credentials) {
        private final Alert alert;
        alert.authenticateUsing(credentials);
    }
    

    See also: auth_tests.py

    Or by sending keys manually like:

    SendKeys("user");
    SendKeys("{TAB}");
    SendKeys("password");
    SendKeys("~"); // Enter
    

    See also the following feature request: #453 Portable BASIC Auth at GitHub

    Related:

    • How to send Basic Authentication headers in Selenium? at QA SE
    0 讨论(0)
  • 2020-11-27 17:14

    I got it to work with Firefox webdriver by the following:

    profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", "google.com");
    driver = new FirefoxDriver(profile);
    
    driver.Navigate().GoToUrl("http://user:pwd@google.com");
    
    0 讨论(0)
提交回复
热议问题