Saturday, 2 February 2019

Maven Dependecies: Selenium Java, TestNG


<dependencies>
<dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8</version>
  <scope>test</scope>
</dependency>


<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.5</version>
</dependency>
<dependencies>

//Alternatively we can update eclipse with TestNG url

Install via Eclipse Marketplace

Go to the TestNG page on the Eclipse Market Place and drag the icon called "Install" onto your workspace.

Install from update site


Friday, 1 February 2019

Selenium Actions Class:

package udemy;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionsClass {

public static void main(String[]args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver (2).exe");
WebDriver driver=new ChromeDriver();

driver.get("https://www.amazon.com/");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();

Actions a=new Actions(driver);
WebElement searchbox=driver.findElement(By.xpath("//input[@id='twotabsearchtextbox']"));

a.moveToElement(searchbox).click().keyDown(Keys.SHIFT).sendKeys("mouse").build().perform();


Thread.sleep(4000);

driver.quit();


}


}

Java : Reading Properties File

//config.properties file

name=Tom
place=UK
age=22
key=value
browser=chrome
appurl=http://www.rediff.com
environment=UAT




package udemy;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ReadProperties {

public static void main(String[] args) throws IOException {

Properties prop=new Properties();
FileInputStream fis;
fis = new FileInputStream("D:\\TstArea\\udemy\\src\\main\\resources\\config.properties");
    prop.load(fis);

    System.out.println(prop.getProperty("appurl"));
System.out.println(prop.getProperty("browser"));


}

}

Java: Iterator and For Each

package udemy;
import java.util.ArrayList;
import java.util.Iterator;

public class IteratorVSForEach {

public static void main(String[] args) {

ArrayList<String> str=new ArrayList<String>();

str.add("sumit");
str.add("sagar");
str.add("asg");

Iterator<String> itstr=str.iterator();
Iterator<String> itstr1=str.iterator();

// 1.Iterator has ability to add-remove items
// 2.For each only has provision to traverse

while(itstr.hasNext()){

System.out.println(itstr.next().toString());
// sumit
// sagar
// asg

}


while(itstr1.hasNext()){

System.out.println("Removing "+itstr1.next().toString());
// Removing sumit
// Removing sagar
// Removing asg

itstr1.remove();

}

ArrayList<String> data=new ArrayList<String>();

data.add("sumit");
data.add("sagar");
data.add("asg");



for (String e: data)
       System.out.println(e);



}
}

Java : Interfaces in Java

package udemy;

public interface InterfaceForClass {

int min_bal=100;

public abstract void Credit();
public abstract void Debit();

}


package udemy;

public class ClassForInterface implements InterfaceForClass{

public void Credit() {
System.out.println("From Credit Method");
}

public void Debit() {
System.out.println("From Debit Method");
}
public void MethodNotinInterface() {
System.out.println("this method does not belong to interface");
}

public static void main(String []args) {
ClassForInterface c1=new ClassForInterface();
c1.Credit();
c1.Debit();
c1.MethodNotinInterface();
System.out.println("@@@@@@@@@@@@Separator@@@@@@@@@@@@@@@@@");
InterfaceForClass i=new ClassForInterface();
i.Credit();
i.Debit();
System.out.println(min_bal); //=>100
min_bal=min_bal+1; // THIS IS NOT POSSIBLE. VARIABLES IN INTERFACE ARE FINAL
i.MethodNotinInterface() //THIS METHOS WILL NOT POP UP
}
}



//Output:

From Credit Method
From Debit Method
this method does not belong to interface
@@@@@@@@@@@@Separator@@@@@@@@@@@@@@@@@@@@@@@

//Below Accessed from InterFace reference
From Credit Method
From Debit Method
100


Java: Exception Handling

package dummyarti;

public class ExceptionHandling {
//declare Class Variables
int e1=5 ;
int f1=0 ;



public void test() {

 try {
int c = e1 / f1; //creating exception
}
 catch (ArithmeticException e2) {
System.out.println("Inside Catch Block"); //handling exception
System.out.println(e2.getMessage());
}

 catch (Exception e) {
System.out.println(e.getMessage());
}

 finally{

System.out.println("Executing Finally"); // will execute in any case
 }

}

public static void main(String[]args) {

ExceptionHandling eh=new ExceptionHandling();
eh.test();

}

}

Apache Poi: Writing to Column with Particular Header

package dummyarti;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class PoiData {

//declared for finding row position of result column
public static int  intRowwithResult;
public static int  intColwithResult;

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub


FileInputStream fr=new FileInputStream("D:\\TstArea\\data.xlsx");

XSSFWorkbook wb=new XSSFWorkbook(fr);
XSSFSheet ws=wb.getSheet("Sheet1") ;


Iterator<Row> r1= ws.iterator();
Row row1=r1.next();  // Poitns to first row
Iterator<Cell> c1= row1.iterator(); //Will Hold Cells of first row


while(c1.hasNext()){  //traverse till last empty cell is reached in a row
Cell cellvalue=c1.next(); //Hold value of cell of a row


boolean b1=cellvalue.toString().equalsIgnoreCase("result"); //checking if cell has value "result in it


if (b1) {
System.out.println(cellvalue.getRowIndex()); //get row, column location if result is found
System.out.println(cellvalue.getColumnIndex());

    intRowwithResult=cellvalue.getRowIndex();
    intColwithResult=cellvalue.getColumnIndex();

}


//XSSFRow row=ws.getRow(intRowwithResult);
intRowwithResult=intRowwithResult+1;
//
for(int j=intRowwithResult;j<10;j++) {


XSSFRow rowtowrite=ws.createRow(j);
XSSFCell cell = rowtowrite.createCell(intColwithResult);
cell.setCellValue("sumit");


//
//
}
//


FileOutputStream out = new FileOutputStream(new File("D:\\TstArea\\data.xlsx"));
        wb.write(out);
        wb.close();



}

}
}

Rest Assured Chectsheet

REST Assured Cheat Sheet 🚀 REST Assured Cheat Sheet Complete Reference for ...