JMETER
apache>bin>jmeter bat
test plan>add>threads>thread group
thread group>add>sampler>http request
http request>add>listner>view results in table
JUNIT
1)create maven project
2)add dependencies in pom.xml(maven repo google,type junit,click junit,latest version,copy)
3)run pom as maven test
4)src/test/java>add>package>click on pkg>new>class
5) add code
package myPackage;
public class Addition {
public int sum(int a,int b) {
return a+b;
}
}
6)click package>new>junit testcase>add class name>finish
package myPackage;
import static org.junit.Assert.*;
import org.junit.Test;
public class sumTest {
@Test
public void test() {
Addition a=new Addition();
int actual=a.sum(20,30);
int expected=50;
assertEquals(actual,expected);
System.out.println(“test passed”);
}
7) right click >Run as Junit
Examples with some more assertions
Array Equal:
public class MyClass {
public static int[] getArray() {
return new int[]{1, 2, 3};
}
public static String getNonNullString() {
return "abc";
}
public static boolean returnTrue() {
return true;
}
}
test file of array equal
@Test
@DisplayName(“Array equals”)
public void ArrayTest()
{
assertArrayEquals(new int[] {1,2,3},new int[] {1,2,3});
System.out.println(“test array equal passed”);
}
@Test
@DisplayName(“AssertNotNull”)
public void NotnullTest()
{
String s1=null;
String s2=”abc”;
assertNotNull(s2);
System.out.println(“not null passed”);
}
}
@Test
@DisplayName(“AssertTrue”)
public void trueAssertTest()
{
Boolean truevar=true;
Boolean falsevar=false;
assertTrue(truevar);
System.out.println(“Assert true passed”);
}
Exception Example:
package myPackage;
public class ExceptionExample {
public int dividebyZero(int a,int b)
{
return a/b;
}
}
package myPackage;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class ExceptionTest {
@Test
void Exceptest() {
try {
ExceptionExample a=new ExceptionExample();
int actual=a.dividebyZero(1,0);
assertEquals(1,actual);
System.out.println("Test passed");
}catch(Exception e) {
System.out.println(e);
}
}
}
AssertArrayEquals and NullpointerException Demo Example:
import java.util.Arrays;
public class MyClass {
public static void sortArray(int[] array) {
if (array != null) {
Arrays.sort(array);
} else {
throw new NullPointerException("Array is null");
}
}
public static int[] getArray() {
return new int[]{1, 2, 3, 4};
}
public static int[] getSortedArray() {
int[] array = getArray().clone();
Arrays.sort(array);
return array;
}
public static String getNonNullString() {
return "abc";
}
public static boolean returnTrue() {
return true;
}
}
test file of assertexception & Null Pointerex:
@Test
public void arrayTest()
{
int actual[]= {1,2,3,4};
int expected[]= {1,4,2,3};
Arrays.sort(expected);
assertArrayEquals(expected,actual);
}
@Test
public void exceptionArrayTest()
{
assertThrows(NullPointerException.class,()->{
int numbers[]= null;
Arrays.sort(numbers);});
}
CUCUMBER
1)Create a maven project Ex MyCucumber
Add dependencies from maven rrepository in pom.xml
(a)cucumber-java (b)selenium-java (c)cucumber-testng (4)maven-surefire-report-plugin
(5)maven compiler plugin 1.8
2)Add from eclipse market in help menu:(cucumber plugin and testNG) restart the eclipse
3)Create a Folder(ex. MCAFeatures) by right clicking src/test/resources/
4)Right click on the MCAFeatures and click on File and give a name like SaucdedemoLogin.feature & Save
5)4)Delete all the contents and copy this:
Feature: Saucedemo login test
Scenario: Check login credentials of saucedemo
Given user is in login page
When user enters username and password
And user clicks on login button
Then user is navigated to new page
6)run as cucumber feature
7)right click on src/test/java create folder/package ( ex StepDefSaucedemo) and
right click on this folder and create a java class-Stepdefinition.java
8)Copy the step definitions functions in the java file and hover mouse on the Gerkin keywords
(Given,when,replace one when with And etc with the import) and delete the exceptions and run the feature file as cucumber feature:
9)Copy the code in stepdefinition file
package StepDefSaucedemo;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class Stepdefinition {
static WebDriver driver;
@Given(“user is in login page”)
public void user_is_in_login_page() {
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
driver.get(“https://www.saucedemo.com/”);
}
@When(“user enters username and password”)
public void user_enters_username_and_password() throws InterruptedException {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.id(“user-name”)).sendKeys(“standard_user”);
driver.findElement(By.id(“password”)).sendKeys(“secret_sauce”);
Thread.sleep(2000);
}
@And(“user clicks on login button”)
public void user_clicks_on_login_button() {
// Write code here that turns the phrase above into concrete actions
driver.findElement(By.id(“login-button”)).click();
}
@Then(“user is navigated to new page”)
public void user_is_navigated_to_new_page() {
// Write code here that turns the phrase above into concrete actions
Assert.assertTrue(driver.findElements(By.xpath(“//div[@class=’app_logo’]”)).size()>0,”navigated”);
}
}
Now right click on feature file content and Run as Cucumber feature
SELENIUM
1)Create maven project
2)Add the dependencies in pom.xml file
from maven repository (i)Selenium-java (ii)testng and Junit (iii)webdrivermanager (iv)maven compiler 1.8
(v) install testNG plugin(in eclipse click on help and go to Eclipse market place and in search bar type testNG afetr installation restart eclipse)
3)code for testing:- src/test/java copy file
package SeleniumDemo1;
import static org.testng.Assert.assertEquals;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SeleniumExample1{
@Test
public void LoginTest() {
WebDriverManager.chromedriver().setup();
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.co.in/”);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
assertEquals(driver.getTitle(),”Google”);
System.out.println(driver.getTitle());
driver.close();
}
@Test
public void OrangeTest() throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(Duration.ofMinutes(1));
driver.get(“https://opensource-demo.orangehrmlive.com/”);
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
driver.findElement(By.name(“username”)).sendKeys(“Admin”);
driver.findElement(By.name(“password”)).sendKeys(“admin123”);
Thread.sleep(5000);
driver.findElement(By.xpath(“//button[@type=’submit’]”)).click();
assertEquals(driver.getTitle(),”OrangeHRM”);
System.out.println(driver.getTitle());
System.out.println(“Login Successful”);
driver.close();
driver.quit();
}
}
save & Run as test ng