0

I want to pass authentification form for testing with Selenium.
I want to omit passing user credentials in URL.

How can I do it with Selenium?

Example of authorization form is below: auth form example

1 Answer 1

1

For passing this authorisation you have few choices:

It has API for basic auth, with something like:

server.autoBasicAuthorization("", "username", "password");

Example:

import net.lightbody.bmp.proxy.ProxyServer;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class ProxyServerBasicAutorizationTest {

    private ProxyServer server;
    private WebDriver driver;

    @Before
    public void startProxy() throws Exception {
        server = new ProxyServer(4444);
        server.start();
        server.autoBasicAuthorization("", "username", "password");
        Proxy proxy = server.seleniumProxy();

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, proxy);

        driver = new FirefoxDriver(capabilities);
    }

    @Test
    public void testAccessProtectedSite() throws Exception {
        driver.get("https://example.com");
        driver.findElement(By.className("sign-out"));
    }

    @After
    public void stopProxyServer() throws Exception {
        driver.quit();
        server.stop();
    }
}

However, I knew that it worked only with basic authorization.

  • AutoIt The problem here is that different browsers make windows in a different way. For Firefox your script can be like the following:

    Local $classForBasicAuthWindow = "[CLASS:MozillaDialogClass]";

    ;wait auth window appearance 10 sec
    WinWait($classForBasicAuthWindow, "", 10)

    If WinExists($classForBasicAuthWindow) Then
    WinActivate($classForBasicAuthWindow)

    ;user name
    Send($CmdLine[1] & "{TAB}");

    ;user password
    Send($CmdLine[2] & "{ENTER}");
    EndIf

You have to convert .au3 file to .exe file (if you have Windows OS) and use it during the test. You can use Aut2exe for it.

You have to launch .exe file before opening the page by browser:

public class AutoItBasicAutorizationTest {

    private final String username = "username";
    private final String password = "password";

    @Test
    public void testBasicAuthenticationFirefox() throws Exception {
        WebDriver driver = new FirefoxDriver();
        File autoIt = new File("src/test/resources/auth.exe");

        // run exe file with passing user credentials
        Process p = Runtime.getRuntime().exec(
                autoIt.getAbsolutePath() + " "
                                  + username + " " + password);

        driver.get("https://example.com");
        driver.findElement(By.className("sign-out"));
        driver.quit();
    }
}

For writing script for multiple browsers your script should be something like:

$timeoutSec = 10;
$lookingSec = 0;
While $lookingSec < $timeoutSec
    ;Firefox or InternetExplorer or Chrome
    If WinExists("[CLASS:MozillaDialogClass]") _
        Or WinExists("[TITLE:Windows Security; CLASS:#32770]") _
            Or WinExists("[CLASS:Chrome_WidgetWin_1]", _
                                    "Authentication Required") Then
                ;user name
                Send($CmdLine[1] & "{TAB}"); 
                ;password
                Send($CmdLine[2] & "{ENTER}"); 
                Exit;
    EndIf
    sleep(1000)
    $lookingSec += 1;
WEnd

However, it depends on browsers versions and settings. Just check AutoIt Window Info.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.