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')
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.
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:
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()
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With