Handle Windows Authentication in Selenium Webdriver

本文介绍了在Selenium WebDriver中处理Windows身份验证的五种方法:通过URL提供凭证,使用AutoAuth Firefox插件,配置NTLM & SPNEGO集成验证,利用SikuliX自动化,以及使用AutoIt脚本。每种方法都详细说明了实施步骤,包括在遇到特殊字符时如何处理以及在Linux系统上的注意事项。
First approach

You can provide credentials in URL itself it means we will add username and password in URL so while running script it will bypass the same.

Syntax

http://username:password@url

Example :

http://mukeshotwani:password1234@www.xyz.com

If username/password contanis the following special characters, you should use URL encoding(Percent-encoding)

] [ ? / < ~ # ` ! @ $ % ^ & * ( ) + = } | : " ; ' , > { space

Example(username "user@example.com" and password "pass!word"):

http://user%40example.com:pass%21word@www.xyz.com

Example(Java):

String url = "http://" + URLEncoder.encode("user@example.com", "UTF-8") + ":" + URLEncoder.encode("pass!word", "UTF-8") + "@www.xyz.com";

Second approach

使用AutoAuth Firefox插件。需要先保存网站密码(密码保存在Firefox 的profile配置文件中),AutoAuth会自动提交验证对话框。Selenium firefox driver 中默认的配置signon.rememberSignons为false,这个值不能在代码中更改,因此不能通过完全使用编码的方式构建FirefoxProfile,需要先安装好AutoAuth,保存好密码,再以此profile为基础构建。

FirefoxProfile profile = new FirefoxProfile(new File("profile dir"));
profile.setPreference(..., ...);

or

// 推荐使用这种方式
ProfilesIni profilesIni = new ProfilesIni();
FirefoxProfile profile = profilesIni.getProfile("default");

 如何创建指定名称的Profile,请参见

Third approach

使用NTLM & SPNEGO集成验证,可安装Integrated Authentication插件,或在firefox地址栏键入about:config回车,修改以下参数:

network.automatic-ntlm-auth.trusted-uris

network.negotiate-auth.delegation-uris

network.negotiate-auth.trusted-uris

在其中输入网址,多个网址用逗号分隔。这样firefox就像IE一样自动获取域用户信息了,不必再输入密码。

Linux下请参见Configuring Firefox to use Kerberos for SSO

Fourth approach

We can use SikuliX to handle this authentication window. SikuliX is a Java application, that works on Windows XP+, Mac 10.6+ and most Linux/Unix systems.

1) Add dependency

<dependency>
  <groupId>com.sikulix</groupId>
  <artifactId>sikulixapi</artifactId>
  <version>1.1.0</version>
</dependency>

2) Type the following code inside your test class:

final Screen screen = new Screen();
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
    @Override
    public void run() {
        while (true)
            if (App.focus("Authentication Required").isRunning()) {
                screen.type("user@example.com" + Key.TAB + "pass!word" + Key.ENTER);
                return;
            }
    }
});
service.shutdown();

driver.get("https://www.engprod-charter.net");

Setup information for Linux systems:

On Linux/Unix systems one has to provide OpenCV (recommended version 2.4+) and Tesseract (needed version 3.0.2+). Additionally the package wmctrl is needed, which is used to implement the app related features.

Fifth approach

We can use AutoIT to handle this authentication window for this Please check whether you have AutoIT installed or not. If not then please download from here.

1) Open the Url ENGPROD on which the authentication is required and open the AutoIt Window Info tool to get the name of the class and the text of the authentication window.

2) Drag the "Finder Tool" box to the object in which you are interested and it display you the information.

Highlight the title in the AutoIt Window Info Tool and press CTRL-C to copy it to the clipboard - we can then paste the title into our script without fear of misspelling it.

3) Open the Script Editor window, save the blank file with ‘.au3′ extension and then enter the following in the script (use CTRL-V or Edit\Paste to paste our window title from the clipboard).

WinWaitActive("Authentication Required")
Send("user@example.com{TAB}pass!word{ENTER}")

4) Now save the above script and then convert it into .exe format. For that, go to Tools > Compile or Ctrl + F7(or right click on the .au3 file and select “Compile Script“).

5) Once you done with the compiling, it will create the ‘.exe’ file with the same name under the same folder and that ‘.exe’ file will be called in the Selenium Test Script by using the following script:

...
Runtime.getRuntime().exec("C:\\test.exe");
driver.get("https://www.engprod-charter.net");
...

Reference

Handle Windows Authentication in Selenium Webdriver

Use of AutoIt in Selenium Webdriver

Introduction to Sikuli GUI Automation Tool (Automate Anything You See on Screen) – Sikuli Tutorial

Special Characters in Usernames and Passwords

Sikuli Script

SikuliX

Sikuli Slides

AutoIt

Custom Firefox Profile for Selenium

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值