Friday 24 May 2024

Windows based authentication using encoded url in selenium cucumber

Use below code to login to an application that uses windows based pop-ups. 
      @Given("Windows based authentication with encoded url")  
      public void loginApp() throws Throwable {  
       String applicationURL = "https://www.example.com/  
       //Ensure password should not contain @  
       String user = URLEncoder.encode(System.getProperty("user"), StandardCharsets.UTF_8.toString());  
       String pwd = URLEncoder.encode(System.getProperty("password"), StandardCharsets.UTF_8.toString());  
       String encodeUrl = "https://" + user + ":" + pwd + "@" + applicationURL.split("https://")[1];  
       System.out.println("Encoded URL " + encodeUrl);  
       driver.get(encodeUrl);  
       driver.navigate().refresh();  
      }  

NOTE : 

1) Ensure you pass the user and password as environment variable in eclipse or from the command prompt. 

For instance: In eclipse, 
-Duser=myuser,-Dpassword=mypassword

2) We used @ in the password and it didn't work for us, so if you have @ in password login may not work.

Implementation Credits: SS

No comments:

Post a Comment