欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Handle Windows Authentication in Selenium Webdriver

程序员文章站 2022-07-15 20:07:41
...
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,请参见

 

另一种方式,使用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

 

Third 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.

 

Fourth 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.


Handle Windows Authentication in Selenium Webdriver
            
    
    博客分类: Test Windows AuthenticationBASIC authAutoItSpecial Character 
 

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.


Handle Windows Authentication in Selenium Webdriver
            
    
    博客分类: Test Windows AuthenticationBASIC authAutoItSpecial Character 
 

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}")

 
Handle Windows Authentication in Selenium Webdriver
            
    
    博客分类: Test Windows AuthenticationBASIC authAutoItSpecial Character 

 

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

  • Handle Windows Authentication in Selenium Webdriver
            
    
    博客分类: Test Windows AuthenticationBASIC authAutoItSpecial Character 
  • 大小: 98.8 KB
  • Handle Windows Authentication in Selenium Webdriver
            
    
    博客分类: Test Windows AuthenticationBASIC authAutoItSpecial Character 
  • 大小: 28.1 KB
  • Handle Windows Authentication in Selenium Webdriver
            
    
    博客分类: Test Windows AuthenticationBASIC authAutoItSpecial Character 
  • 大小: 55.2 KB