Tutorial 10 – Browser based Authentication in Selenide
Launch https://the-internet.herokuapp.com/
Click ‘Basic Auth’ link that you see above.
Notice below that a ‘Sign in’ popup comes up asking user to enter username/password credentials.
This is a typical example of Browser based Authentication. The reason we call it a browser based authentication is because this authentication is generated by the browser
Enter username and password as ‘admin’
Click ‘Sign in’.
Notice below that the browser has authenticated the user
Let us now see application based authentication.
Launch the application https://www.way2automation.com/
You would see ‘Member Login’ link as shown below
Let us click this link. Notice below that the below authentication is generated by the application. It is NOT generated by browser
Let us see how to automate Browser based Authentication using Selenide.
Let us first execute the below statement
Notice below that the browser popup gets launched
Now, to enter username and password, we can use an override method as shown below
This method accepts 4 arguments:
Let us replace the respective arguments with values
Save and execute. Notice this time that the script automatically logged in during runtime
We can use another override method
So we have
Thus we can write
We get the same results
Inspect the text
Let us assert the text on the screen
Execute, the assertion passses
Let us introduce an error
As expected, the assertion fails this time
This is how we can handle browser authentications.
Code snippet
package w2a.selenide;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.Selenide.switchTo;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.testng.Assert;
import org.testng.annotations.Test;
public class FirstSelenideTest {
@Test
public void searchTest() throws InterruptedException, MalformedURLException {
//open("https://the-internet.herokuapp.com/basic_auth");
//open(relativeOrAbsoluteUrl, domain, login, password);
//open("https://the-internet.herokuapp.com/basic_auth", "", "admin", "admin");
open(new URL("https://the-internet.herokuapp.com/basic_auth"), "", "admin", "admin");
String text = $(By.xpath("//p[contains(text(),'Congratulations! You must have the proper credenti')]")).getText();
Assert.assertTrue(text.contains("pr1oper credentials"));
Thread.sleep(5000);
}
}
Thank you for reading!