Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass through window asking for basic auth credentials that appears when click link redirecting from HTTP to HTTPS?

I have a website where most of pages are normally used via HTTP but some other pages are available only via HTTPS. Site is protected by basic auth (credentials are the same for HTTP and HTTPS pages).

When I open any HTTP page in browser (either FF or Chrome) and click link that leads to HTTPS page, browser shows alert that asks for basic auth credentials.

I have same issue with Webdriver (either FF or Chrome):
When I visit http://username:password@some_domain.com and click link that leads to HTTPS page, browser alert window that asks for basic auth credentials appears. Selenium doesn't "remember" credentials that were entered for HTTP page.

How can I follow this sequence of actions with Webdriver? If it's not possible what can you advice?

like image 497
Andrei Botalov Avatar asked Oct 04 '22 22:10

Andrei Botalov


2 Answers

 FirefoxProfile profile = new FirefoxProfile();
 profile.SetPreference("network.http.phishy-userpass-length", 255);
 profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", hostname);
 Driver = new FirefoxDriver(profile);

hostname is your URL (example.com) then try to

Driver.Navigate().GoToUrl(http://user:[email protected]);
like image 73
Andrian Durlestean Avatar answered Oct 07 '22 19:10

Andrian Durlestean


The best solution I've been able to come up with so far is create a new Thread that handles a timeout. As WebDriver doesn't return control on FF and other certain browsers, I can call the thread handler that then uses Robot to enter in the credentials and press enter (could also use AutoIt here). Then the control is returned back to WebDriver to continue with script.

//put this where it belongs, say calling a new url, or clicking a link 
//assuming necessary imports 

int pageLoadTimeout = 10;
String basicAuthUser = "user";
String basicAuthPass = "pass";
String url = "https://yourdomain.com";

WebDriver driver = new FirefoxDriver();

TimeoutThread timeoutThread = new TimeoutThread(pageLoadTimeout);
timeoutThread.start();

driver.get(url);

//if we return from driver.get() call and timeout actually occured, wait for hanlder to complete
if (timeoutThread.timeoutOccurred){
    while (!timeoutThread.completed) 
        Thread.sleep(200);
}
else {
    //else cancel the timeout thread
    timeoutThread.interrupt();
}


public class TimeoutThread extends Thread {

    int timeout;
    boolean timeoutOccurred;
    boolean completed;

    public TimeoutThread(int seconds) {
        this.timeout = seconds;
        this.completed = false;
        this.timeoutOccurred = false;
    }

    public void run() {
        try {

            Thread.sleep(timeout * 1000);
            this.timeoutOccurred = true;
            this.handleTimeout();
            this.completed = true;

        } 
        catch (InterruptedException e) {
            return;
        }
        catch (Exception e){
            System.out.println("Exception on TimeoutThread.run(): "+e.getMessage());
        }
    }

    public void handleTimeout(){

        System.out.println("Typing in user/pass for basic auth prompt");

        try {
            Robot robot = new Robot();

            //type is defined elsewhere - not illustrating for this example 
            type(basicAuthUser); 
            Thread.sleep(500);

            robot.keyPress(KeyEvent.VK_TAB);
            robot.keyRelease(KeyEvent.VK_TAB);
            Thread.sleep(500);

            type(basicAuthPass);
            Thread.sleep(500);

            robot.keyPress(KeyEvent.VK_ENTER);
            robot.keyRelease(KeyEvent.VK_ENTER);
        }
        catch (AWTException e) {
            System.out.println("Failed to type keys: "+e.getMessage());
        }
    }
}
like image 32
Lukus Avatar answered Oct 07 '22 20:10

Lukus