Monday, 29 April 2019

TestWork : Paraller Testing with TestNG and ThreadLocal in Selenium Java

//Base Class

package com.testcases;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BaseClass {
private WebDriver driver;
public Date d = new Date();
public SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public String BROWSER = "chrome";
static ThreadLocal<WebDriver> dr = new ThreadLocal<WebDriver>();

public void setdriver(WebDriver driver) {
dr.set(driver);
}

public WebDriver getdriver() {
return dr.get();
}

public void init() {
if (BROWSER.contains("chrome")) {
System.setProperty("webdriver.chrome.driver", "G:\\chromedriver (2).exe");
driver = new ChromeDriver();
setdriver(driver);
getdriver().manage().deleteAllCookies();
getdriver().manage().window().maximize();

}

}

public void quit() {
getdriver().quit();

}

}



//Test Class 1

package com.testcases;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ClassOne extends BaseClass {
Date d = new Date();

@BeforeMethod
public void start() {
init();
// df.format(date)

}

@AfterMethod
public void end() {
quit();
}

@Test
public void methodone() throws Throwable {
System.out.println("method 1 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.google.com");
getdriver().findElement(By.name("q")).sendKeys("method 1");
}

@Test
public void methodtwo() throws Throwable {
System.out.println("method 2 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.google.com");
getdriver().findElement(By.name("q")).sendKeys("method 1");
Thread.sleep(3000);
}

}


//Class two

package com.testcases;

import java.util.Date;

import org.openqa.selenium.By;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Classtwo extends BaseClass {

@BeforeMethod
public void start() {
init();
}

@AfterMethod
public void end() {
quit();
}

@Test
public void methodthree() throws Throwable {
System.out.println("method 3 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.rediff.com");
// getdriver().findElement(By.name("q")).sendKeys("method 3");
}

@Test
public void methodfour() throws Throwable {
System.out.println("method 4 " + df.format(d) + "Thread " + Thread.currentThread().getName());
getdriver().get("http://www.wwe.com");
// getdriver().findElement(By.name("q")).sendKeys("method 4");
Thread.sleep(3000);
}

}

//testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="classes">
  <test thread-count="5" name="Test" parallel="classes">
    <classes>
      <class name="com.testcases.Classtwo"/>
      <class name="com.testcases.ClassOne"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Tuesday, 16 April 2019

Java SimpleDateFormat

package coreJava;

import java.text.SimpleDateFormat;
import java.util.Date;

public class dateDemo {

public static void main(String[] args) {
// TODO Auto-generated method stub
//current date..current time./
Date d= new Date();

SimpleDateFormat sdf=new SimpleDateFormat("M/d/yyyy");
SimpleDateFormat sd=new SimpleDateFormat("M/d/yyyy hh:mm:ss");
System.out.println(sdf.format(d));
System.out.println(sd.format(d));
System.out.println(d.toString());




}

}

Friday, 12 April 2019

Apache POI: is-this-possible-to-read-and-write-on-same-xls-sheet

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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


public class XLSXReaderWriter {

    public static void main(String[] args) {

        try {
            File excel = new File("D://raju.xlsx");
            FileInputStream fis = new FileInputStream(excel);
            XSSFWorkbook book = new XSSFWorkbook(fis);
            XSSFSheet sheet = book.getSheetAt(0);

            Iterator<Row> itr = sheet.iterator();

            // Iterating over Excel file in Java
            while (itr.hasNext()) {
                Row row = itr.next();

                // Iterating over each column of Excel file
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {

                    Cell cell = cellIterator.next();

                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    default:

                    }
                }
                System.out.println("");
            }

            // writing data into XLSX file
            Map<String, Object[]> newData = new HashMap<String, Object[]>();
            newData.put("1", new Object[] { 1d, "Raju", "75K", "dev",
                    "SGD" });
            newData.put("2", new Object[] { 2d, "Ramesh", "58K", "test",
                    "USD" });
            newData.put("3", new Object[] { 3d, "Ravi", "90K", "PMO",
                    "INR" });

            Set<String> newRows = newData.keySet();
            int rownum = sheet.getLastRowNum();

            for (String key : newRows) {
                Row row = sheet.createRow(rownum++);
                Object[] objArr = newData.get(key);
                int cellnum = 0;
                for (Object obj : objArr) {
                    Cell cell = row.createCell(cellnum++);
                    if (obj instanceof String) {
                        cell.setCellValue((String) obj);
                    } else if (obj instanceof Boolean) {
                        cell.setCellValue((Boolean) obj);
                    } else if (obj instanceof Date) {
                        cell.setCellValue((Date) obj);
                    } else if (obj instanceof Double) {
                        cell.setCellValue((Double) obj);
                    }
                }
            }

            // open an OutputStream to save written data into Excel file
            FileOutputStream os = new FileOutputStream(excel);
            book.write(os);
            System.out.println("Writing on Excel file Finished ...");

            // Close workbook, OutputStream and Excel file to prevent leak
            os.close();
            book.close();
            fis.close();

        } catch (FileNotFoundException fe) {
            fe.printStackTrace();
        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }
}

Spring Boot : Exception Handler 14