Create a Selenium WebDriver instance
To launch the website in the desired browser, set the system properties to the path of the driver for the required browser. This example will use ChromeDriver for Login Automation using Selenium Webdriver. The syntax for the same will be:
Webdriver driver = new ChromeDriver(); System.setProperty("webdriver.chrome.driver", "Path of the chrome driver");
Configure the Web browser
Usually, the web page will be in a minimized format when the test case is run. Maximize the browser for a clear picture of the test cases executed. Use the command below to do the same:
driver.manage.window.maximize();
Navigate to the web URL
Open the browser with the desired URL. Use the command below to open the URL in the desired instantiated browser:
Perform Action on the Located Web Element
After locating the element, testers need to perform the desired action. In this case, the action is entering text in the email and password fields and hitting the login button. For this, the script uses sendKeys and click methods, as shown below:
Verify & Validate The Action
To validate the results, use assertion. Assertions are important for comparing the expected results to the actual results. If it matches, the test case passes. If not, then the test case fails. The syntax below will help to assert (validate) the outcome from actions by performing login automation:
Assert.assertEquals(String actual, String expected);
Save the actual URL post-login into a string value, which is:
How to handle authentication popup in chrome with selenium webdriver using java
May be helpful for others to solve this problem in chrome with the help of chrome extension. Thanks to @SubjectiveReality who gave me this idea.
Sending username and password as part of url like https://username:password@www.mytestsite.com may be helpful if same server performs both authentication and hosts the application. However most corporate applications have firmwide authentications and app server may reroute the request to authentication servers. In such cases, passing credentials in URL wont work.
Here is the solution:
#Step1: Create chrome extension#
- Create a folder named ‘extension’
- Create a file named ‘manifest.json’ inside ‘extension’ folder. Copy below code into the file and save it.
{
“name”:”Webrequest API”,
“version”:”1.0″,
“description”:”Extension to handle Authentication window”,
“permissions”:[“<all_urls>”,”webRequest”,”webRequestBlocking”],
“background”: {
“scripts” : [“webrequest.js”]
},
“manifest_version”: 2
}
- Create a file named ‘webrequest.js’ inside ‘extension’ folder and copy paste below code into the file and save it.
chrome.webRequest.onAuthRequired.addListener(
function handler(details){
return {'authCredentials': {username: "yourusername", password: "yourpassword"}};
},
{urls:["<all_urls>"]},
['blocking']);
Open chrome browser, go to chrome://extensions and turn on developer mode
Click ‘Pack Extension’, select root directory as ‘extension’ and pack extension. It should create a file with extension ‘.crx’
#Step2: Add extension into your test automation framework #
- Copy the .crx file into your framework
- Configure your webdriver creation to load the extension like
options.addExtensions(new File("path/to/extension.crx"));
options.addArguments("--no-sandbox");
- Invoke your webdriver and application URL
- You wont see the authentication popup appearing as its handled by above extension
Happy Testing!
References:
http://www.adambarth.com/experimental/crx/docs/webRequest.html#apiReferencehttps://developer.chrome.com/extensions/webRequest#event-onAuthRequiredchrome.webRequest.onAuthRequired Listenerhttps://gist.vhod-v-lichnyj-kabinet.ru/florentbr/25246cd9337cebc07e2bbb0b9bf0de46
How to log in to chrome with selenium?
First login to gmail on regular chrome browser (NOT the one triggered by selenium driver). Once you login, install the EditTheCookie extension. And on the gmail tab, click this extension icon. It will give option to copy the cookies in json format to clipboard. Paste that into Gmail.data which will be used in below programme.
Once you past, place that Gmail.data file in a avilable location for below programme (you can place anywhere and update the path of that file in below code accordingly).
I’ve developed this and is a working solution for me since long.
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.google.gson.Gson;
public class LoginUtils {
private static final String GMAIL_LOGIN_URL =
"https://accounts.google.com/signin/v2/identifier";
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver = LoginUtils.login(driver, GMAIL_LOGIN_URL, "Gmail.data");
}
public static final WebDriver login(WebDriver driver, String url, String pathOfJsonFileName) {
Cookies[] data = readJson(pathOfJsonFileName);
driver.navigate().to(url);
// Set the expire time of each cookie.
Date expiryTime = new Date(System.currentTimeMillis() 1000000000);
for (Cookies cookie : data) {
Cookie ck = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(),
expiryTime, Boolean.parseBoolean(cookie.getSecure()), Boolean.parseBoolean(cookie.getHttpOnly()));
driver.manage().addCookie(ck);
}
return driver;
}
private static final Cookies[] readJson(String jsonFileName) {
String json = null;
try {
byte[] encoded = Files.readAllBytes(Paths.get(jsonFileName));
json = new String(encoded);
} catch (Exception e) {
e.printStackTrace();
}
return new Gson().fromJson(json, Cookies[].class);
}
}
Prerequisites for login automation using selenium webdriver
- Download and Install JDK(Java Development Kit)
- Install Eclipse from the official website
- Download the Selenium Java Client version
- Configure the drivers depending on the browser. The example here will be using a chrome driver for Chrome
Read More: How to configure Selenium in Eclipse
Steps for login automation using selenium webdriver
Before performing automation testing for the login functionality, there are a couple of basic steps that need to be followed for the test case to be written:
- Create a Selenium WebDriver instance
- Configure browser if required
- Navigate to the required web page
- Locate the relevant web element
- Perform action on the web element
- Verify and validate the action
Now let’s walk through each of these steps in detail.