To perform Basic Authentication for Web pages, we can use any of the following methods:
1. Passing the authentication details via URL(Easiest method)
2. Using Custom Function(Moderate difficulty)
1. Passing the authentication details via URL(Easiest method)
In order to perform Basic Authentication within URL in Testsigma, you can use the following format in the Go to testdata NLP Statement:
Go to [protocol]://[username]:[password]@hostname
For example,
Go to https://admin:admin123@orangehrm.com OR Go to http://admin:admin123@orangehrm.com
2. Using Custom Function(Moderate difficulty)
a. Create a Custom Function with the below code. Please refer for more details - How to create and use Custom Functions in Testsigma?
import org.openqa.selenium.WebDriver; import com.testsigma.customfunc.common.CustomTestStep; import com.testsigma.customfunc.result.TestStepResult; import com.testsigma.customfunc.result.ResultConstants; import org.openqa.selenium.Keys; import org.openqa.selenium.Alert; import org.openqa.selenium.remote.BrowserType; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.Capabilities; import com.testsigma.customfunc.common.TestsigmaCustomFunctions; public class BasicAuthentication extends TestsigmaCustomFunctions{ @CustomTestStep public TestStepResult authenticationURL(String url, String username, String password) throws InterruptedException { TestStepResult result = new TestStepResult(); Capabilities cap = ((RemoteWebDriver) driver).getCapabilities(); String browser = cap.getBrowserName(); if(browser.equals(BrowserType.CHROME) || browser.equals(BrowserType.GOOGLECHROME)) { if(url.startsWith("https://")) { url = url.replace("https://", "https://" + username + ":" + password + "@"); } else if(url.startsWith("http://")) { url = url.replace("http://", "http://" + username + ":" + password + "@"); } driver.get(url); result.setStatus(ResultConstants.SUCCESS); result.setMessage("Successfully Authenticated on Chrome"); } else if (browser.equals(BrowserType.FIREFOX)) { driver.get(url); Alert alert = driver.switchTo().alert(); alert.sendKeys(username + Keys.TAB.toString()+ password); alert.accept(); Thread.sleep(3000); alert.sendKeys(username + Keys.TAB.toString()+ password); alert.accept(); Thread.sleep(3000); result.setStatus(ResultConstants.SUCCESS); result.setMessage("Successfully Authenticated on Firefox"); } else if (browser.equals(BrowserType.IE)) { if(url.startsWith("https://")) { url = url.replace("https://", "https://" + username + ":" + password + "@"); } else if(url.startsWith("http://")) { url = url.replace("http://", "http://" + username + ":" + password + "@"); } driver.get(url); result.setStatus(ResultConstants.SUCCESS); result.setMessage("Successfully Authenticated on Firefox"); } else { driver.get(url); result.setStatus(ResultConstants.FAILURE); result.setMessage("Unsupported Browser"); } result.setStatus(ResultConstants.SUCCESS); result.setMessage("Successfully Executed"); return result; } }
b. Once the Custom Function is compiled and saved successfully, go to Create Test Step page, Select the Custom Function option and then select the Function name from the list.
c. Enter the following value as Custom Function arguments - URL, Username, Password and click on Create.
d. We need to add two Capabilities for hassle-free Execution. In Create Execution Configuration(Normal Execution) or Create Dry Run(Dry Run) page, add the following Desired Capabilities.
- acceptInsecureCerts : true
- acceptSslCerts : true
That's all we need to perform for Basic Authentication in URL.
Happy Automation!