Encoding and Decoding in Selenium using Base64 Class of Java
What is Base64?
Base64 is an encoded scheme that converts binary data in to text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.
Simple Program:
package encodeAndDecode;
import java.util.Base64;
public class EncodeText {
public static void main(String[] args) {
// TODO Auto-generated method stub
String username = "mercury";
// Encode Username
byte[] encodedByte = Base64.getEncoder().encode(username.getBytes());
String encodedText = new String(encodedByte);
System.out.println("Encoded Username : "+encodedText);
//Decode Username
byte[] decodedByte = Base64.getDecoder().decode(encodedByte);
String decodedText = new String(decodedByte);
System.out.println("Decoded Username : "+decodedText);
}
Output:
Encoded Username : bWVyY3VyeQ==
Decoded Username : mercury
Usage In Selenium:
We can keep the encoded data in an external data source file (Text File, Properties File, Excel File, JSON File etc.) and fetch the data and decode it and use in our test program. I am taking an example of a properties file.
testData.properties
-------------------------
username=bWVyY3VyeQ==
password=bWVyY3VyeQ==
loginSuccessfulTitle=RmluZCBhIEZsaWdodDogTWVyY3VyeSBUb3Vyczo=
EncoderAndDecoder.java
---------------------------------
package encodeAndDecode;
import java.util.Base64;
public class EncoderAndDecoder {
public static String getEncodedText(String value) {
byte[] encodedByte = Base64.getEncoder().encode(value.getBytes());
String encodedString = new String(encodedByte);
return encodedString;
}
public static String getdecodedText(String value) {
byte[] decodedByte = Base64.getDecoder().decode(value.getBytes());
String decodedString = new String(decodedByte);
return decodedString;
}
}
PropertiesReader.java
-----------------------------
package utility;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class PropertiesReader {
Properties p = new Properties();
public Properties getProperties() {
try {
File file = new File("./src/test/resources/testData.properties");
FileReader reader = new FileReader(file);
p.load(reader);
}catch(FileNotFoundException ex) {
System.out.println(ex.getMessage());
}catch(IOException ex) {
System.out.println(ex.getMessage());
}
return p;
}
}
Login.java
--------------
package testcases;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import encodeAndDecode.EncoderAndDecoder;
import io.github.bonigarcia.wdm.WebDriverManager;
import utility.PropertiesReader;
public class Login {
WebDriver driver;
Properties p;
PropertiesReader reader;
@BeforeMethod
public void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
driver.get("http://newtours.demoaut.com");
}
@Test
public void verifyLoginFunctionality() {
try {
reader = new PropertiesReader();
p = reader.getProperties();
String username = EncoderAndDecoder.getdecodedText(p.getProperty("username"));
String password = EncoderAndDecoder.getdecodedText(p.getProperty("password"));
System.out.println(password);
String loginSuccessfulTitle = EncoderAndDecoder.getdecodedText(p.getProperty("loginSuccessfulTitle"));
driver.findElement(By.name("userName")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.name("login")).click();
Thread.sleep(5000);
Assert.assertEquals(driver.getTitle(), loginSuccessfulTitle);
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
@AfterMethod
public void tearDown() {
if(driver!=null) {
driver.quit();
}
}
}
Usage In Selenium :
Base64 is an encoded scheme that converts binary data in to text format so that encoded textual data can be easily transported over network un-corrupted and without any data loss.
Simple Program:
package encodeAndDecode;
import java.util.Base64;
public class EncodeText {
public static void main(String[] args) {
// TODO Auto-generated method stub
String username = "mercury";
// Encode Username
byte[] encodedByte = Base64.getEncoder().encode(username.getBytes());
String encodedText = new String(encodedByte);
System.out.println("Encoded Username : "+encodedText);
//Decode Username
byte[] decodedByte = Base64.getDecoder().decode(encodedByte);
String decodedText = new String(decodedByte);
System.out.println("Decoded Username : "+decodedText);
}
Output:
Encoded Username : bWVyY3VyeQ==
Decoded Username : mercury
Usage In Selenium:
We can keep the encoded data in an external data source file (Text File, Properties File, Excel File, JSON File etc.) and fetch the data and decode it and use in our test program. I am taking an example of a properties file.
testData.properties
-------------------------
username=bWVyY3VyeQ==
password=bWVyY3VyeQ==
loginSuccessfulTitle=RmluZCBhIEZsaWdodDogTWVyY3VyeSBUb3Vyczo=
EncoderAndDecoder.java
---------------------------------
package encodeAndDecode;
import java.util.Base64;
public class EncoderAndDecoder {
public static String getEncodedText(String value) {
byte[] encodedByte = Base64.getEncoder().encode(value.getBytes());
String encodedString = new String(encodedByte);
return encodedString;
}
public static String getdecodedText(String value) {
byte[] decodedByte = Base64.getDecoder().decode(value.getBytes());
String decodedString = new String(decodedByte);
return decodedString;
}
}
PropertiesReader.java
-----------------------------
package utility;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class PropertiesReader {
Properties p = new Properties();
public Properties getProperties() {
try {
File file = new File("./src/test/resources/testData.properties");
FileReader reader = new FileReader(file);
p.load(reader);
}catch(FileNotFoundException ex) {
System.out.println(ex.getMessage());
}catch(IOException ex) {
System.out.println(ex.getMessage());
}
return p;
}
}
Login.java
--------------
package testcases;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import encodeAndDecode.EncoderAndDecoder;
import io.github.bonigarcia.wdm.WebDriverManager;
import utility.PropertiesReader;
public class Login {
WebDriver driver;
Properties p;
PropertiesReader reader;
@BeforeMethod
public void setUp() {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
driver.get("http://newtours.demoaut.com");
}
@Test
public void verifyLoginFunctionality() {
try {
reader = new PropertiesReader();
p = reader.getProperties();
String username = EncoderAndDecoder.getdecodedText(p.getProperty("username"));
String password = EncoderAndDecoder.getdecodedText(p.getProperty("password"));
System.out.println(password);
String loginSuccessfulTitle = EncoderAndDecoder.getdecodedText(p.getProperty("loginSuccessfulTitle"));
driver.findElement(By.name("userName")).sendKeys(username);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.name("login")).click();
Thread.sleep(5000);
Assert.assertEquals(driver.getTitle(), loginSuccessfulTitle);
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}
@AfterMethod
public void tearDown() {
if(driver!=null) {
driver.quit();
}
}
}
Add some more explanation with code.
ReplyDeleteCode is already given. I have given one simple code to understand the concept. I have also given one more code to understand how encoding and decoding can be used in Selenium.
DeleteGreat Post with valuable information. I am glad that I have visited this site. Share more updates
ReplyDeleteSelenium Online Training
Google Analytics Online course
Great post. keep sharing such a worthy information
ReplyDeleteBig data training in chennai
Big Data Course in Chennai
80mk,m
This post is so interactive and informative.keep update more information…
ReplyDeleteSoftware testing training in Anna Nagar
Software testing training in chennai