Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate using Windows Authentication in Playwright

I need to automate a test that uses Windows Authentication.

I know that the the prompt that opens up is not part of the HTML page, yet why my code is not working:

login_page.click_iwa()
sleep(5)
self.page.keyboard.type('UserName')
sleep(5)
self.page.keyboard.press('Tab')
self.page.keyboard.type('Password')
like image 455
Tal Angel Avatar asked Sep 07 '25 08:09

Tal Angel


2 Answers

I solved the issue by using a virtual keyboard:

from pyautogui import press, typewrite, hotkey


def press_key(key):
    press(key)


def type_text(text):
    typewrite(text)


def special_keys(special, normal):
    hotkey(special, normal)

Then, after I implemented the virtual keyboard, I did this:

def login_iwa(self, user_name='User321', password='123456', need_to_fail=False):
        pyautogui.FAILSAFE = False
        self.click_iwa()
        sleep(7)
        type_text(user_name)
        sleep(1)
        press_key('Tab')
        sleep(1)
        type_text(password)
        sleep(1)
        press_key('Enter')
        sleep(2)
        if need_to_fail:
            press_key('Tab')
            sleep(1)
            press_key('Tab')
            sleep(1)
            press_key('Tab')
            sleep(1)
            press_key('Enter')
            sleep(1)

I used pyautogui.FAILSAFE = False because sometime the popup was hiding behind the screen or was openning up on the 2nd screen.

like image 136
Tal Angel Avatar answered Sep 08 '25 22:09

Tal Angel


I recommend using more protocol-based approaches. I have tested the following methods to authenticate using the the-internet.herokuapp.com/basic_auth endpoint with the username and password set to admin, and all of them worked successfully. Below, you'll find the complete code for each method to make it easy to test them by simply copy-pasting:

  1. Passing the credentials in the URL:

     from playwright.sync_api import sync_playwright
    
     with sync_playwright() as playwright:
         browser = playwright.chromium.launch(headless=False)
         context = browser.new_context()
         page = context.new_page()
         page.goto(url="http://admin:[email protected]/basic_auth")
         context.close()
         browser.close()
    
  2. Passing the credentials to the http_credentials context parameter:

     from playwright.sync_api import sync_playwright
    
     with sync_playwright() as playwright:
         browser = playwright.chromium.launch(headless=False)
         context = browser.new_context(
             http_credentials={
                 "username": "admin",
                 "password": "admin"
             }
         )
         page = context.new_page()
         page.goto(url="http://the-internet.herokuapp.com/basic_auth")
         context.close()
         browser.close()
    
  3. Using the Authorization HTTP header (however, there are other authentication types):

     import base64
     from playwright.sync_api import sync_playwright
    
     def generate_basic_authentication_header_value(username: str, password: str) -> str:
         token = base64.b64encode("{}:{}".format(username, password).encode("utf-8")).decode("ascii")
         return "Basic {}".format(token)
    
     with sync_playwright() as playwright:
         browser = playwright.chromium.launch(headless=False)
         context = browser.new_context(
             extra_http_headers={
                 "Authorization": generate_basic_authentication_header_value("admin", "admin"),
             }
         )
         page = context.new_page()
         page.goto(url="http://the-internet.herokuapp.com/basic_auth")
         context.close()
         browser.close()
    
like image 39
Alireza Roshanzamir Avatar answered Sep 08 '25 22:09

Alireza Roshanzamir