Wednesday, 12 November 2014

Difference between Thread.Sleep() and Selenium.setSpeed()?

selenium.setSpeed
 
1.Syntax: selenium.setSpeed(string time in milliseconds);

 
ex: selenium.setSpeed("2000") - will wait for 2 seconds

 
2. Runs each command in after setSpeed delay by the 


number of milliseconds mentioned in setSpeed.
 
thread.sleep

 
1. Syntax:  thread.sleep(integer time in milliseconds);

 
ex: thread.sleep(2000) - will wait for 2 seconds

 
2. Waits for only once at the command given at sleep.

Screenshots using selenium.

package Flowell.Flowell1;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

@Test
public class iTextPDFTutorial {
    public void iTextPDF() throws Exception{
   
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com/");
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
        FileUtils.copyFile(scrFile, new File("C:\\Users\\Youtility\\Desktop\\screenshot.png"));
       
    }
}

How to generate PDF report using iText java API

package Flowell.Flowell1;

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;


public class iTextPDFTutorial {
    public void iTextPDF() throws Exception{
        String FILE = "C:/Users/Youtility/Desktop/sampleiText.pdf";
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();
        document.add(new Paragraph("Hello iText"));
        document.add(new Paragraph("I will be printed in PDF with the help of iText"));
        document.close();
           
    }
   
    public static void main(String args[]){
        iTextPDFTutorial get = new iTextPDFTutorial();
        try {
            get.iTextPDF();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }