Tuesday, 19 March 2019

Java: Using Super()

package constructorDemo;

 public class Superclass {

public int rollno;
public String name;

Superclass(int rollnolocal, String namelocal) {

rollno = rollnolocal;
name = namelocal;

}

}



package constructorDemo;

class subclass extends Superclass {

public int classnum;
public String subject;

subclass(int rollnolocal, String namelocal,int classnumlocal,String subjectlocal) {
super(rollnolocal,namelocal);
classnum = classnumlocal;
subject= subjectlocal;

System.out.println("Roll num is:"+rollno +" Name is "+name+ "Classnum is "+classnum+" Subject is "+subject);
}

Monday, 18 March 2019

Java: File Reading with Buffered Reader

package FileHandling;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;

import java.io.IOException;

public class ReadFile {

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

File fl = new File("H:\\TstArea\\filetoread.txt");

FileReader fr = new FileReader(fl);

BufferedReader br = new BufferedReader(fr);

System.out.println(br.readLine());

String line = null;

while (line != br.readLine()) {

System.out.println(br.readLine());

}

}

}

Java: File Writing with BufferedWriter

package FileHandling;

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

 public class FileWritertest {

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


File fl=new File("H:\\TstArea\\filewriter.txt");

FileWriter fw=new FileWriter(fl,false);

BufferedWriter bfw=new BufferedWriter(fw);

bfw.write("sumit");
bfw.newLine();
bfw.write("neha");
bfw.close();




}

}

Core Java: This() and this

package StudyPackage;

public class ThisKeyword {

//Default Constructor
ThisKeyword(){
this(5);
System.out.println("Default Constructor");
}

//Constructor with 1 parameter
ThisKeyword(int data){
this(5,6);
System.out.println("Int Constructor");
}

//Constructor with 2 parameters
ThisKeyword(int data,int data1){
System.out.println("Int Constructor with 2 Arguments");

}
  public static void main(String[] args) {

  ThisKeyword th=new ThisKeyword();//Calling Default
  //  
}
}

OutPut:
Int Constructor with 2 Arguments
Int Constructor
Default Constructor

#this() is useful for calling the constructor. Matching constructors gets called based on parameters provided.

#Should be first line in constructor. 2 this() not allowed


Friday, 15 March 2019

Java: Apache POI : Writing to Excel File

package poitest;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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 WritingExcel {

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


XSSFWorkbook wb=new XSSFWorkbook(); //new workbook 
XSSFSheet ws=wb.createSheet("dummy");  //Sheet Object

XSSFRow row0=ws.createRow(0); //Row Creation
XSSFRow row1=ws.createRow(1);
XSSFRow row2=ws.createRow(2);


XSSFCell cell0=row0.createCell(0); //Cell Access
XSSFCell cell1=row1.createCell(0);
XSSFCell cell2=row2.createCell(0);

cell0.setCellValue("0"); //Actual writing
cell1.setCellValue("1");
cell2.setCellValue("2");

FileOutputStream fio=new FileOutputStream("H:\\TstArea\\writewithpoi.xlsx");
               //File Creation for writing output

wb.write(fio); //Writing
wb.close(); //Close Workbook

}

}


//MAVEN APACHE POI DEPENDENCIES
<dependencies>
  <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.1</version>
</dependency>

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.0.1</version>
</dependency>
 </dependencies>


Tuesday, 5 March 2019

Selenium Keyboard Compoud Actions and Javascript executor

package TestCases;

import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
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.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.Assert;

public class mouseKeyboard {


    @Test
    public void mousekbddemo() {
   
    System.setProperty("webdriver.chrome.driver", "G:\\chromedriver (2).exe");
        WebDriver driver= new ChromeDriver();
       
        String baseUrl = "http://www.facebook.com/";
        driver.get(baseUrl);
        driver.get(baseUrl);
        WebElement txtUsername = driver.findElement(By.id("email"));

//Series of Steps
//        Actions builder = new Actions(driver);
//        Action seriesOfActions = builder
//        .moveToElement(txtUsername)
//        .click()
//        .keyDown(txtUsername, Keys.SHIFT)
//        .sendKeys(txtUsername, "sumit")
//        .keyUp(txtUsername, Keys.SHIFT)
//        .doubleClick(txtUsername)
//        .contextClick()
//        .build();
//        seriesOfActions.perform() ;
       
        
        //Writes SSSS in email textbox on Facebook in single step
        Actions a=new Actions(driver);
 a.moveToElement(txtUsername).click().keyDown(Keys.SHIFT).sendKeys("ssss").build().perform();
       
        
        //getText for email textbox returns empty value.
       
   
        //Using Java Script Executor to get the entered value i.e SSSS
        JavascriptExecutor js= (JavascriptExecutor)driver;
        String return_value = (String) js.executeScript("return     document.getElementById('email').value");
        System.out.println(return_value);
 
      Assert.assertEquals(return_value, "SSSS", "Comparsing values post data entry");
     
       
     
    }
   


}

Monday, 4 March 2019

Selenium Javascript and Alert Handling

package TestCases;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class JavaScriptExecutor {



@Test
    public void Login() throws InterruptedException
    { System.setProperty("webdriver.chrome.driver", "G:\\chromedriver (2).exe");
        WebDriver driver= new ChromeDriver();
       
        //Creating the JavascriptExecutor interface object by Type casting
        JavascriptExecutor js = (JavascriptExecutor)driver;
       
        //Launching the Site.
        driver.get("http://moneyboats.com/");
   
        //Maximize window
        driver.manage().window().maximize();
       
        Thread.sleep(5000);
       
        //Vertical scroll down by 600  pixels
        js.executeScript("window.scrollBy(0,600)");
       
        //Adding javascript alert to webpage
        js.executeScript("window.alert(\"sometext\")");
       
       
        System.out.println(driver.switchTo().alert().getText()); //sometext
        //Accepting the Alert
        driver.switchTo().alert().accept();
    }
}

Rest Assured Chectsheet

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