Showing posts with label automation. Show all posts
Showing posts with label automation. Show all posts

Wednesday, 15 March 2017

How to Download and Install Selenium IDE


How to Download and Install Selenium IDE?
1) Launch Mozilla Firefox Browser.
2) Type URL: https://addons.mozilla.org/en-us/firefox/addon/selenium-ide/ in your browser. Selenium IDE Add-ons page will get open then Click on Add to Firefox button as shown in image bellow.
Download and Install Selenium IDE

3) Firefox will show one popup saying do you want to allow Mozilla Firefox to install Selenium IDE Add-ons or not. Click on Install button as Shown in Image below.
Download_And_Install_2

4) Firefox will automatically install Selenium IDE software. After the installation is completed, a pop up window appears asking to re-start the Firefox. Click on the “Restart Now” button to reflect the Selenium IDE installation. Click on Restart Now button.
Download_And_Install_3

5) On clicking on the Restart Now button, Firefox will restart automatically. In case you missed the pop-up, simply close the Firefox and launch again. Once the Firefox is booted and started again, we can see selenium IDE under the tools menu list. Click on Tools menu list displayed at the top bar. Selenium IDE will be displayed in the list.
Download_And_Install_4

6) Click on Selenium IDE, it will launch Selenium IDE.
Download_And_Install_5

Monday, 26 December 2016

Moving Files From One Folder to Another Folder

1)create maveen project
2)import lib files from commons-io2.0.1.jar for FileUtils,FilenameUtils
by downloading from following link
 https://commons.apache.org/proper/commons-io/download_io.cgi
3) make sure destination and source folder path

package JavaWorld;

import java.io.File;
//commons-io2.0.1.jar for FileUtils,FilenameUtils
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;


public class MoveFilesBasedOnExtension {
public static void main(String a[])throws Exception
{
File srcFolder = new File("C:/Users/Vishnuyardini/Desktop/fotos/11-12-16 soulmates to banshankari ashram");
File processedDir = new File("C:/Users/Vishnuyardini/Desktop/fotos/soulmate siritual trip");
File shdir = new File("C:/Users/Vishnuyardini/Desktop/fotos/soulmate siritual trip/shdir");
File afile[];
int j = (afile = srcFolder.listFiles()).length;
for(int i = 0; i < j; i++)
{
File fileEntry = afile[i];
String fileName = fileEntry.getName();
System.out.println(fileName);
//moving specific files to other destination directory
if("jpg".equalsIgnoreCase(FilenameUtils.getExtension(fileName)))
{

FileUtils.moveFileToDirectory(fileEntry, processedDir, true);
}
if("txt".equalsIgnoreCase(FilenameUtils.getExtension(fileName)))
{

FileUtils.moveFileToDirectory(fileEntry, shdir, true);
}



}
}
}

Output:
before:





after:

List the files in the folder

package JavaWorld;
import java.io.File;

public class ListFileInFolder {

public static void main(String[] args) {

File myFolder = new File("C:/Users/Vishnuyardini/Desktop");
File afile[];
int j = (afile = myFolder.listFiles()).length;
for(int i = 0; i < j; i++)
{
File fileEntry = afile[i];
String fileName = fileEntry.getName();
System.out.println(fileName);
       
       
    }
}
}

output:







Friday, 23 December 2016

Read Content From Excel File and Write In .txt File

step 1: download selenium
step 2:download Apache poi
step 3:create excel file with content
step 4:create txt document





import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class ReadExcel {

    public static void main( String [] args ) {
      Writer writer = null;
        try {
        String filePath= System.getProperty("user.dir")+"\\file\\readExcel.xls";
    //String sheetName="sheet1";
    //File file =    new File(filePath);
    //FileInputStream inputStream = new FileInputStream(filePath);
             InputStream input = new BufferedInputStream(
                        new FileInputStream(filePath));
            POIFSFileSystem fs = new POIFSFileSystem( input );
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0); //sheet of excel
            String filePath2= System.getProperty("user.dir")+"\\file\\write.txt";
   
            File file = new File(filePath2);
            writer = new BufferedWriter(new FileWriter(file));
            Iterator rows = sheet.rowIterator();
            while( rows.hasNext() ) {  
                HSSFRow row = (HSSFRow) rows.next();
                System.out.println("\n");
                Iterator cells = row.cellIterator();
             
                   writer.write("insert into Emp values(");
                while( cells.hasNext() ) {
                   
                    HSSFCell cell = (HSSFCell) cells.next();
                    if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType()) {
                    System.out.print( cell.getNumericCellValue()+"     "+cell.getColumnIndex() );
                    if(cell.getColumnIndex()==3)
                  writer.write(String.valueOf(cell.getNumericCellValue()));
                    else
                     writer.write(String.valueOf(cell.getNumericCellValue()+","));
                    }
                    else
                    if(HSSFCell.CELL_TYPE_STRING==cell.getCellType()) {
                        System.out.print( cell.getStringCellValue()+"     " );
                        writer.write("'"+cell.getStringCellValue()+"',");
                    }
                    else
                        if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType()) {
                        System.out.print( cell.getBooleanCellValue()+"     " );
                       writer.write("'"+String.valueOf(cell.getBooleanCellValue()+"',"));
                        }
                        else
                            if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType())
                                System.out.print( "BLANK     " );
                                else
                            System.out.print("Unknown cell type");
             
                 
                }
               
                writer.write(");"+"\n");
            }
           
           
        } catch ( IOException ex ) {
            ex.printStackTrace();
        } finally {
        try {
        if (writer != null) {
        writer.close();  
        }             }
        catch (IOException e) {
        e.printStackTrace();  
        }  
        }
    }
   
}

Wednesday, 14 December 2016

Writing and Reading Links in excel fetched from website and writing status of link in excel

WRITING IN EXCEL: 

package excelExportAndFileIO;
import java.io.IOException;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Write {
public static void main(String...strings )  throws IOException
{
Write object =new Write();
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
  driver.get("https://www.thesun.co.uk/");
System.out.println("opening of webpage");
List<WebElement> links=driver.findElements(By.tagName("a"));
System.out.println("feaching links from webpage");
System.out.println("writting links");
String filePath= System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx";
String sheetName="sheet1";
File file =    new File(filePath);
FileInputStream inputStream = new FileInputStream(filePath);
Workbook writeWorkbook = new XSSFWorkbook(inputStream);
Sheet sheet = writeWorkbook.getSheet(sheetName);
int rowCount=0;
int columnCount=0;
for(WebElement link:links)
{
Row newRow = sheet.createRow(rowCount++);
Cell cell = newRow.createCell(columnCount);
cell.setCellValue(link.getAttribute("href"));
}
System.out.println("URL-----"+links.size());
FileOutputStream outputStream = new FileOutputStream(file);
writeWorkbook.write(outputStream);
driver.close();
}
}
READING LINKS FROM EXCEL AND OPENING IN BROWSER:
package excelExportAndFileIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Read {
public String readExcel(int rowNum,String sheetName) throws IOException{
String filePath = System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx";
FileInputStream inputStream = new FileInputStream(filePath);
Workbook readWorkbook = null;
readWorkbook = new XSSFWorkbook(inputStream);
Sheet readSheet = readWorkbook.getSheet(sheetName);
int rowCount = readSheet.getLastRowNum();
System.out.println("Row Count"+rowCount);

Row row = readSheet.getRow(rowNum);
return getCellData(row);
}
public static String getCellData(Row row){
   
    String value="";
    try{
    if(row.getCell(0).getCellType()!=Cell.CELL_TYPE_BLANK){
    value= row.getCell(0).getStringCellValue();
      
    }
    }catch(NullPointerException e){
    }
return value;
    }
public static void main(String...strings) throws IOException{


Read objExcelFile = new Read();
                  for(int i=0;i<2;i++){

String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
driver.get(objExcelFile.readExcel(i,"Sheet1"));
System.out.println(objExcelFile.readExcel(i,"Sheet1"));

}
}

}

VALIDATION OF LINKS IN EXCEL:
package excelExportAndFileIO;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ValidLink {
public static void main(String...strings )  throws IOException{
ValidLink object =new ValidLink();
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
  driver.get("https://www.thesun.co.uk");

  //driver.get("https://www.google.com");
System.out.println("opening of webpage");
List<WebElement> links=driver.findElements(By.tagName("a"));
System.out.println("feaching links from webpage");
System.out.println("writting links");
String filePath= System.getProperty("user.dir")+"\\excel\\linkValid.xlsx";
String sheetName="sheet1";
File file =    new File(filePath);
FileInputStream inputStream = new FileInputStream(filePath);
Workbook writeWorkbook = new XSSFWorkbook(inputStream);
Sheet sheet = writeWorkbook.getSheet(sheetName);
int rowCount=0;
int columnCount1=1,columnCount2=2;
for(WebElement link:links)
{
Row newRow = sheet.createRow(rowCount++);
Cell cell = newRow.createCell(columnCount1);
cell.setCellValue(link.getAttribute("href"));

try 
       {
String linkUrl=link.getAttribute("href");
URL url = new URL(linkUrl);
HttpURLConnection httpURLConnect=(HttpURLConnection)url.openConnection();
httpURLConnect.setConnectTimeout(3000);
httpURLConnect.connect();
newRow.createCell(columnCount2).setCellValue(httpURLConnect.getResponseCode());
if(httpURLConnect.getResponseCode()==200)
{
newRow.createCell(columnCount2).setCellValue(httpURLConnect.getResponseCode());
rowCount++;
System.out.println(linkUrl+" - "+httpURLConnect.getResponseMessage());
}
     
       }
catch (Exception e)
{
           
       }
break;
  
}
System.out.println("URL-----"+links.size());
FileOutputStream outputStream = new FileOutputStream(file);
writeWorkbook.write(outputStream);
driver.close();
}}




Friday, 9 December 2016

reading content from excel and opening ad link in browser

file name:ReadGuru99ExcelFile.java

package excelExportAndFileIO;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ReadGuru99ExcelFile {

// public String urlLink;

    public void readExcel(String sheetName) throws IOException{

 
   
   
    //Create a object of File class to open xlsx file
    String filePath = System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx";
   // File file =    new File(filePath+"\\"+fileName);

    //Create an object of FileInputStream class to read excel file

    FileInputStream inputStream = new FileInputStream(filePath);

    Workbook guru99Workbook = null;

    //Find the file extension by spliting file name in substring and getting only extension name

 guru99Workbook = new XSSFWorkbook(inputStream);


    //Read sheet inside the workbook by its name

    Sheet guru99Sheet = guru99Workbook.getSheet(sheetName);

    //Find number of rows in excel file

    int rowCount = guru99Sheet.getLastRowNum();

    //Create a loop over all the rows of excel file to read it

    System.out.println("Row Count"+rowCount);
    for (int i = 0; i < rowCount; i++) {
        Row row = guru99Sheet.getRow(i);
        getCellData(row);}
    }
  public static String getCellData(Row row){
   
    String value="";
    try{
    if(row.getCell(0).getCellType()!=Cell.CELL_TYPE_BLANK){
    value= row.getCell(0).getStringCellValue();
    String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
        System.setProperty("webdriver.chrome.driver",driverPath);
        WebDriver driver = new ChromeDriver();
        driver.get(value);
       String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
       driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
    }
    }catch(NullPointerException e){
    //return "";
   
    }
return value;
    }
    // System.out.println("Current row--"+i);
      /*  String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();
String baseUrl = getCellData(row);
   driver.get(baseUrl);*/
      //  driver.get(getCellData(google.com));
   //     String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
       // System.out.println( getCellData(row));
    //urlLink=  getCellData(row);
   
      //  driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
          //rowCount++;

   
       

       

   

   
//    System.out.println(rowCount);
   

   
 

    //Main function is calling readExcel function to read data from excel file

    public static void main(String...strings) throws IOException{

    //Create a object of ReadGuru99ExcelFile class

    ReadGuru99ExcelFile objExcelFile = new ReadGuru99ExcelFile();

    //Prepare the path of excel file

   

    //Call read file method of the class to read data

    objExcelFile.readExcel("Sheet1");

    }

}

Wednesday, 7 December 2016

fetching link from webpage and writing in excel file Method 2(best case)

steps:
1.create maven project
2.attach selenium and chrome drivers
3.set dependency in pom file for Apache POI
4.attach one excel file in the project

file name :NewLinkWritter.java


package excelExportAndFileIO;

import java.io.IOException;
//import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;



import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NewLinkWritter {



public static void main(String...strings )  throws IOException{
NewLinkWritter obj=new NewLinkWritter();
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();

driver.get("http://vishnuyardinichari.blogspot.in/");
System.out.println("opening of webpage");
List<WebElement> links=driver.findElements(By.tagName("a"));
System.out.println("feaching links from webpage");

System.out.println("writting links");

String filePath= System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx";
String sheetName="sheet1";
File file =    new File(filePath);
FileInputStream inputStream = new FileInputStream(filePath);
Workbook guru99Workbook = new XSSFWorkbook(inputStream);
Sheet sheet = guru99Workbook.getSheet(sheetName);

int rowCount=0;
int columnCount=0;

for(WebElement link:links)
{
Row newRow = sheet.createRow(rowCount++);
Cell cell = newRow.createCell(columnCount);
cell.setCellValue(link.getAttribute("href"));
}
System.out.println("URL-----"+links.size());

FileOutputStream outputStream = new FileOutputStream(file);
guru99Workbook.write(outputStream);
//guru99Workbook.close();
//guru99Workbook.close();
driver.close();
//sheet1.close();
}}


fetching link from webpage and writing in excel file Method 1(worst case)

steps:
1.create maven project
2.attach selenium and chrome drivers
3.set dependency in pom file for Apache POI
4.attach one excel file in the project

METHOD 1:


file 1: SunWebLink.java

package excelExportAndFileIO;

import java.io.IOException;
//import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SunWebLink {


public static void main(String...strings) throws IOException{
String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();

driver.get("http://vishnuyardinichari.blogspot.in/");
System.out.println("opening of webpage");
List<WebElement> links=driver.findElements(By.tagName("a"));
System.out.println("feaching links from webpage");

WriteGuru99ExcelFile objExcelFile = new WriteGuru99ExcelFile();
System.out.println("calling write program");

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

data.add("links");

*/
System.out.println("writting links");
for(WebElement link:links){

// System.out.println(link.getAttribute("href"));
 objExcelFile.writeExcel(System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx","sheet1",link.getAttribute("href"));

 
}
//System.out.println("complete writing links");
System.out.println("URL-----"+links.size());
}


}


file 2:WriteGuru99ExcelFile.java


package excelExportAndFileIO;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;



import org.apache.poi.ss.usermodel.Cell;

import org.apache.poi.ss.usermodel.Row;

import org.apache.poi.ss.usermodel.Sheet;

import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WriteGuru99ExcelFile {

    

public void writeExcel(String filePath,String sheetName,String string) throws IOException{

//Create a object of File class to open xlsx file

File file =    new File(filePath);

//Create an object of FileInputStream class to read excel file

FileInputStream inputStream = new FileInputStream(filePath);

Workbook guru99Workbook = null;

//Find the file extension by spliting file name in substing and getting only extension name
guru99Workbook = new XSSFWorkbook(inputStream);



//Read excel sheet by sheet name    

Sheet sheet = guru99Workbook.getSheet(sheetName);

//Get the current count of rows in excel file

int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();

//Get the first row from the sheet

//Row row = sheet.getRow(0);

//Create a new row and append it at last of sheet

Row newRow = sheet.createRow(rowCount+1);

//Create a loop over the cell of newly created Row

for(int j = 0;j<2300; j++){

//Fill data in row

Cell cell = newRow.createCell(j);

//System.out.println("Data to write-"+string);
cell.setCellValue(string);

}

//Close input stream


//Create an object of FileOutputStream class to create write data in excel file

FileOutputStream outputStream = new FileOutputStream(file);

//write data in the excel file

guru99Workbook.write(outputStream);

//close output stream

outputStream.close();
inputStream.close();


}



public static void main(String...strings) throws IOException{

//Create an array with the data in the same order in which you expect to be filled in excel file

String[] valueToWrite = {"Mr. E","Noida"};

//Create an object of current class

WriteGuru99ExcelFile objExcelFile = new WriteGuru99ExcelFile();

//Write the file using file name , sheet name and the data to be filled

// objExcelFile.writeExcel(System.getProperty("user.dir")+"\\excel\\ExportExcel.xlsx","sheet1",valueToWrite);

}

}


RUN:
run the application at java application






Tuesday, 6 December 2016

read and write file in java

writter:

package file.writer.and.reader;
import java.io.*;

/**
 *
 * @author Vishnuyardini
 */
public class writter {
    public static void main(String ... arg)
    {
        try{
   FileWriter fw=new FileWriter("abc.txt");
   fw.write("hai parwez.. this is my first task.. i did this");
   fw.close();
  }
        catch(Exception e){System.out.println(e);}
  System.out.println("success content was success fully write");
 }
    }


reader:

package file.writer.and.reader;
import java.io.*;

/**
 *
 * @author Vishnuyardini
 */
public class reader {
    public static void main(String... arg)throws Exception{  
        try (FileReader fr = new FileReader("abc.txt")) {
            int i;
            while((i=fr.read())!=-1)
                System.out.print((char)i);
        }  
    
}
}

fetch link from webpage

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;


import org.openqa.selenium.chrome.ChromeDriver;
import java.util.*;
public class link1 {


public link1() {
// TODO Auto-generated constructor stub
}


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

String driverPath=System.getProperty("user.dir")+"/drivers/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",driverPath);
WebDriver driver = new ChromeDriver();

driver.get("https://www.thesun.co.uk/");
List<WebElement> links=driver.findElements(By.tagName("a"));


for(WebElement link:links){

 System.out.println(link.getAttribute("href"));
}

System.out.println("URL-----"+links.size());
}

}