Below are list of frequently used commands. This would come very handy while working with docker & kubernetes.
Thursday, 23 September 2021
Thursday, 12 August 2021
How to create and execute Jmeter script using Java
Performance Test!. When we say this term, one of the first things that gets into our mind is 'Jmeter'.
Jmeter is the go-to tool for the performance testing needs in open source community. It is built completely using Java, designed to perform load test and measure performance. It can simulate load on a server, group of servers, network to check the threshold limit and analyze performance under different types of loads. Vast list of plug-ins which extends jmeter capabilities and making it handle most of the performance test requirements.
Mostly the Jmeter GUI is used to create the scripts, configure the users, capture other details and execute the scripts. But, when it comes to integrate the performance tests to code driven automation frameworks, one has to switch to Jmeter GUI to create scripts & fallback to framework to execute the jmeter scripts. In order to make the integration seamless, Jmeter scripts can be created & executed during runtime using code driven framework. Below code snippet would lead you to achieve the same.
Below snippet will let you create a jmeter script (jmx) for a webservice by adding minimal elements to test plan. Typical hierarchy of a web request in a jmx would be as below:
Test Plan à Thread Group à Sampler à Assertions à Listeners
Steps:
- Create a maven project through eclipse or any IDE.
- Add the below Jmeter dependencies in your POM file:
- ApacheJMeter_core
- ApacheJMeter_components
- ApacheJMeter_http
- jorphan
- ApacheJMeter_java
- Create a class named "APITest" and copy the below code snippet
- Change the service details & file locations accordingly.
- DONE..! You are all set to create and run the jmeter script from java.
import java.io.File;
import java.io.FileOutputStream;
import org.apache.commons.io.FileUtils;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.report.config.ConfigurationException;
import org.apache.jmeter.report.dashboard.ReportGenerator;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
public class APITest {
public void createAndExecute() {
File jmeterHome = new File("C:/apache-jmeter");
try {
if (jmeterHome.exists()) {
// JMeter Engine
StandardJMeterEngine jmeter = new StandardJMeterEngine();
setconfig(jmeterHome, "./htmlreportsdir");
// JMeter Test Plan, basic all u JOrphan HashTree
HashTree testPlanTree = new HashTree();
// HTTP Sampler
HTTPSamplerProxy httpSampler = new HTTPSamplerProxy();
httpSampler.setName("HTTP Sampler");
httpSampler.setProtocol("https");
httpSampler.setDomain("testjmeter.com");
httpSampler.setPort(8080);
httpSampler.setPath("/getservicepath");
httpSampler.setMethod("GET");
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
httpSampler.setEnabled(true);
httpSampler.addArgument("Arg1","val1");//Arguments
httpSampler.addArgument("Arg1","val1");//Arguments
httpSampler.addNonEncodedArgument("", "serviceBody", "=");//payload
httpSampler.setPostBodyRaw(true);
//Loop Controller
LoopController loopController = new LoopController();
loopController.setLoops(1);
loopController.setFirst(true);
loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
loopController.initialize();
//Thread Group
ThreadGroup threadGroup = new ThreadGroup();
threadGroup.setName("API Thread Group");
threadGroup.setNumThreads(20); //Users
threadGroup.setRampUp(10); //Seconds
threadGroup.setSamplerController(loopController);
threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());
threadGroup.setIsSameUserOnNextIteration(true);
threadGroup.setScheduler(false);
//Test Plan
TestPlan testPlan = new TestPlan("JMeter Script From Java Code");
testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());
//Construct Test Plan from previously initialized elements
testPlanTree.add(testPlan);
HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
threadGroupHashTree.add(httpSampler);
// save generated test plan to JMeter's .jmx file format
String jmxFilePath = "./jmxfiles/TestAPI.jmx";
SaveService.saveTree(testPlanTree, new FileOutputStream(jmxFilePath));
// add Summarizer output to get test progress in stdout like:
String jtlFilePath = ".jtlFiles/TestAPI.jtl";
ReportGenerator reportGenerator = setReportInfo(testPlanTree, jtlFilePath);
//Run Test Plan
jmeter.configure(testPlanTree);
jmeter.run();
// Report Generator
FileUtils.deleteDirectory(new File("./htmlreportsdir"));// delete old report
FileUtils.deleteDirectory(new File("./reportsdir"));// delete old report
reportGenerator.generate();
System.out.println("Test completed. See " + jtlFilePath + " file for results");
System.out.println("JMeter .jmx script is available at " + jmxFilePath);
} else {
System.out.println("Jmeter Home not found..");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public void setconfig(File jmeterHome,String htmlrepDir){
File jmeterProperties = new File(jmeterHome.getPath() +"/bin/jmeter.properties");
//JMeter initialization (properties, log levels, locale, etc)
JMeterUtils.setJMeterHome(jmeterHome.getPath());
JMeterUtils.loadJMeterProperties(jmeterProperties.getPath());
JMeterUtils.initLocale();
//Set directory for HTML report
JMeterUtils.setProperty("jmeter.reportgenerator.exporter.html.property.output_dir",htmlrepDir);
}
public ReportGenerator setReportInfo(HashTree testPlanTree,String jtlFilePath) throws ConfigurationException{
Summariser summer = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
if (summariserName.length() > 0) {
summer = new Summariser(summariserName);
}
// Store execution results into a .jtl file
File logFile = new File(jtlFilePath);
//delete log file if exists
if (logFile.exists()){
boolean delete = logFile.delete();
System.out.println("Jtl deleted: " + delete);
}
//Summary Report
ResultCollector logger = new ResultCollector(summer);
logger.setEnabled(true);
logger.setFilename(logFile.getPath());
//creating ReportGenerator for creating HTML report
ReportGenerator reportGenerator = new ReportGenerator(jtlFilePath, logger);
testPlanTree.add(testPlanTree.getArray()[0], logger);
return reportGenerator;
}
}
Friday, 4 June 2021
How to establish a connection to Remote Host & Copy file
Many times we do get the requirement, to connect a remote server & perform operations like copying a file from local to remote, vice versa and many others. But, have you ever thought how do we do that through java.? Yes, We can. Java has multiple libraries which can perform this task. One such java library which can perform this task is Jcraft's 'JSch (Java Secure Channel)' .
Below example will let you know how to connect a remote host & perform copy operations.
public void copyFiletoRemotehost(String username,String pwd,String[] remotehosts, String port) {
Session jschSession = null;
int SESSION_TIMEOUT = 10000;
final int CHANNEL_TIMEOUT = 5000;
String REMOTE_HOST = "";
int REMOTE_PORT = 22;
String lFile = "C:/localhost/local.txt";
String rFile = "/localtoRemote.txt";
try {
JSch jsch = new JSch();
for(int i=0; i < remotehosts.length; i++) {
REMOTE_HOST = remotehosts[i].toString();
jschSession = jsch.getSession(username, REMOTE_HOST, REMOTE_PORT);
//Remove known_hosts requirement
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
jschSession.setConfig(config);
//authenticate using password
jschSession.setPassword(pwd);
jschSession.connect(SESSION_TIMEOUT);
Channel sftp = jschSession.openChannel("sftp");
sftp.connect(CHANNEL_TIMEOUT);
ChannelSftp channelSftp = (ChannelSftp) sftp;
channelSftp.put(lFile, rFile);
channelSftp.exit();
//Copy from one location to another with remote server
copyFile(jschSession);
jschSession.disconnect();
}
} catch (JSchException | SftpException e) {
e.printStackTrace();
} finally {
if (jschSession != null) {
jschSession.disconnect();
}
}
}
/**
* This method will let you copy from one folder to another within the remote server
*/
public static void copyFile(Session jschSession) throws JSchException {
String cmd1 = "cp C:/username/localtoRemote.txt C:/anotherfolder/localtoRemote.txt";
Channel exec = jschSession.openChannel("exec");
ChannelExec channelExec = (ChannelExec) exec;
channelExec.setCommand(cmd1);
exec.connect(CHANNEL_TIMEOUT);
channelExec.setErrStream(System.err);
}
References:Saturday, 18 July 2020
Hybrid App Automation using Appium
Prerequisites:
- Android SDK is downloaded and installed. (ANDROID_HOME env. variable should be set)
- Either Emulator or Real device (USB debugging enabled) is available.
- Appium desktop app or non GUI package is installed.
Steps:
- Create a java project in eclipse 'AppiumMobileTesting'.
- Create a class 'TestHybridApp.java'.
- Connect your device(USB debugging enabled), to system's USB port & accept if any confirmation pop ups.
- Start Appium Server. Once the device is connected, open the 'Appium Inspector Session' providing the suitable capabilities.
- Open the Hybrid app in the device and you see the same screen on inspector session.
- Click on the required control in inspector session & then the related locator details are shown on the right hand side in appium inspector.
Friday, 6 March 2020
How to query or filter Json using RestAssured JsonPath
REST Assured(RA) is a framework built on Java to test the REST services. It supports any HTTP method but has explicit support for GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH and includes specifying & validating like Headers, Cookies etc.
For validating and querying the Webservice responses, it has couple of libraries 'JsonPath' and 'XMLPath' for parsing Json and XML responses accordingly.
Even-though we know that, we have to use these libraries but most of the times we get into situations where we forget or we are not sure of the actual syntax. Here are list of few scenarios and it's JsonPath syntax (XMLPath syntax are almost similar) for your reference.
All the Jsonpath examples in this post, use the following Json:
{
"store":{
"book":[
{
"category":"reference",
"author":"Nigel Rees",
"title":"Sayings of the Century",
"price":8.95
},
{
"category":"fiction",
"author":"Evelyn Waugh",
"title":"Sword of Honour",
"price":12.99
},
{
"category":"horror",
"author":"Herman Melville",
"title":"Moby Dick",
"isbn":"0-553-21311-3",
"price":8.99
},
{
"category":"fiction",
"author":"J. R. R. Tolkien",
"title":"The Lord of the Rings",
"isbn":"0-395-19395-8",
"price":22.99
}
],
"bicycle":{
"color":"red",
"price":19.95
}
},
"city":"Bangalore"
}
Thursday, 18 April 2019
How to send Cookies from Web(GUI) to Web Service
Cookies: Browser Cookies are also referred as Internet Cookie,Web Cookie or HTTP Cookie. A cookie is a "small piece of data" sent from the website to the user's device while the user is accessing the website.
There are multiple ways these cookies are being used in modern world like authentication, browser state information, storing user personal information like name, address etc., tracking the user activity on web, etc., Different types of cookies satisfy different needs.
In certain cases, one might have to test web services which needs cookies to be passed as part of request. One way to address this, is to combine Web application plus web service test steps.
Below code snippet would open the web application, capture the cookies stored by the application and pass it to web service using Rest Assured.
public class CookiesToWebServices {
static WebDriver driver;
static String driverPath = "C:/Drivers/chromedriver.exe";
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",driverPath);
ArrayList cookieList = new ArrayList();
String url = "www.testurl.com/testpath/id";
HashMap headermap = new HashMap<>();
headermap.put("Content-Type", "application/json");
headermap.put(....
HashMap queryParammap = new HashMap<>();
queryParammap.put("id", "12345");
queryParammap.put(....
Monday, 31 December 2018
How to Connect Zephyr For Jira Programmatically (ZAPI)
Use Case: Whenever the automation is run, it has to update the status of execution in test cycle.
Pre-requisite: Manual Tests are created in Jira with Issue Type - Test & Test cycle is created with tests added into it (Adding tests into test cycle creates execution ids for each test case in test cycle).
High Level Steps:- Login to Jira using proper credentials.
- Provide the Cycle Id OR ProjectId,Version & Cycle Id of the Test cycle to ZAPI api.
- Update the Test case status in test cycle based on the execution id of each test case.
Example to Update Status of Test case execution Using ZAPI API:
This example shows how to consume ZAPI REST APIs from Java using REST Assured libraries.
Friday, 14 September 2018
How to run Chrome & Firefox Webdriver in Headless mode
Headless Browsers are a kind of simulation programs of real browsers, but without GUI. Having said that, these may not be 100% equal to real browsers as they have its own limitations. Here are few popular headless browsers:
- Headless Chrome
- Headless Firefox
- HtmlUnit Driver
- PhantomJS
- SlimmerJS
Headless browsers come with its own Pros & Cons.
- Improves the execution speed,in turn increases the performance.
- Can be executed on Browser-less setups like Servers(without browsers).
- Useful while scraping the web.
- Simulate multiple browser versions on a single machine.
- It doesn't mimic the real user.
- Debugging is quite difficult.
Below snippet would show you, How to make Chrome & Firefox work in Headless mode.
Friday, 7 September 2018
Compare Images in Selenium Test
This type of testing is usually performed manually. However, now you can automate these type of tests to an extent using different image comparison libraries like Thumbnailator, Slenium-Shutterbug, ImageMagick Applitools Eyes etc.
Using these libraries, both Functional Testing & Visual Testing can go hand-in-hand without having to maintain separate set of scripts.
One such example using Image-comparison.jar based on Thumbnailator library is as below:
Steps:
Wednesday, 5 September 2018
Capture Full Screenshot in Selenium using Shutterbug
Selenium with the new upgrade 2.0 to 3.* had changed the way it works in the background. Starting with this upgrade, FirefoxDriver has stopped taking the full screenshot. There are many instances where user needs complete screenshot of the web page.
Many teams have worked on it & provided few open source libraries like AShot, ShutterBug etc.
Below code snippet would enable you to get the full screenshot using ShutterBug java library.
STEPS:
Capture Full Screenshot in Selenium using AShot
Selenium with the new upgrade 2.0 to 3.* had changed the way it works in the background. Starting with this upgrade, FirefoxDriver has stopped taking the full screenshot. There are many instances where user needs complete screenshot of the web page.
Many teams have worked on it & provided few open source libraries like AShot, ShutterBug etc.
Below code snippet would enable you to get the full screenshot using AShot java library.
STEPS:
Wednesday, 25 July 2018
Native App Automation using Appium
Automated testing of these many variation of apps, is very tricky & difficult task. Not all of them are supported by single automation tool. Having said that, Appium which is built over Selenium webdriver supports most of the OS & apps with different configurations.
In this article, you would know a way to automate the Native App using AndroidDriver.
Prerequisites:
- Android SDK is downloaded and installed. (ANDROID_HOME env. variable should be set)
- Either Emulator or Real device (USB debugging enabled) is available.
- Appium desktop app or non GUI package is installed.
Steps:
- Create a java project in eclipse 'AppiumMobileTesting'.
- Create a class 'TestNativeApp' to add the automation code.
- Connect your device(USB debugging enabled), to system's USB port & accept if any confirmation pop ups.
Friday, 22 June 2018
Data Generation using Java
Having said that, it is not so easy to address every requirement with single solution. In the below example, i am trying to address few of the generic use cases which will let us automate the data creation.
NOTE:
- Below sample uses 'Xeger' an automation library to generate text from regular expressions.
- Apache commons RandomStringUtils class to generate the random text.
Below example will let you know the way to generate the Random strings of different types.
public class RandomTextGenerator {
Thursday, 31 May 2018
Web App Automation using Appium
Automated testing of these many variation of apps, is very tricky & difficult task. Not all of them are supported by single automation tool. Having said that, Appium which is built over Selenium webdriver supports most of the OS & apps with different configurations.
In this article, you would know a way to automate the mobile web app using RemoteWebdriver.
Prerequisites:
- Android SDK is downloaded and installed. (ANDROID_HOME env. variable should be set)
- Either Emulator or Real device (USB debugging enabled) is available.
- Appium desktop app or non GUI package is installed.
Steps:
- Create a java project in eclipse 'AppiumMobileTesting'.
- Create a class 'TestWebApp' to add the automation code.
Sunday, 29 April 2018
How to Avoid Certificate Popups to enable smooth Automation
Certificate Popups are common in a secured environment or while accessing the internal applications. It is a basically a way to authenticate the users accessing the application.
Yes, I know..! This is annoying for automation testers.
To overcome this annoying feature and yet without compromising on security, we have to make certain settings on each browser. These settings enable us to have smooth automation.
The fix is very simple, let the browser know which certificate to select as default one. Thus stops asking you for a client certificate. Follow the settings mentioned below based on the browser:
NOTE:
- This approach will help when you have no or only single certificate added to browser.
- This approach is tested on windows OS.
Mozilla Firefox:
Step 1: Import Certificate
- Open Firefox > Options > Certificates > View Certificates > Your Certificates.
- Click Import & follow instructions to import a certificate(*.pfx)
- After Import, Success Message will be popped up.