Friday, 31 July 2015

Input text to text field with and without sendkeys

Input text to text field:

1. sendkeys()
2. javascript


package automation.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class test1 {

 
 public static void main(String[] args) {
  WebDriver drv=new FirefoxDriver();
  drv.navigate().to("http://www.google.com/");
  
  try{
   
   // WITH SENDKEYS
   //drv.findElement(By.id("lst-ib")).sendKeys("big data");
   
   // WITHOUT SENDKEYS 
   JavascriptExecutor jse = (JavascriptExecutor)drv;
       //jse.executeScript("document.getElementById('lst-ib').value='big data';");
    //   OR
       jse.executeScript("document.getElementById('lst-ib').setAttribute('value', 'big data');");
    drv.findElement(By.id("lst-ib")).submit();
  }
  catch(Exception e){
   e.printStackTrace();
  }
  

 }

}

Tuesday, 14 July 2015

Convert PDF to text file (Core JAVA)

If you are using maven then add dependency for itextpdf from below link or add itextpdf jars to your project:
Maven Dependency itextpdf

Download sample pdf from below link and and save it in your d drive as examplePDF.pdf:
Download Sample PDF (examplePDF.pdf)

Save exampleText.txt in your d drive.

If you want in other format like word document, just change the file name for OUTPUT i.e. "exampleText.doc"


package automation.prac;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
//iText imports
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
 
public class pdfToText {
 
  public void partPdf(String pdf, String txt) throws IOException {
         PdfReader reader = new PdfReader(pdf);
         PdfReaderContentParser parser = new PdfReaderContentParser(reader);
         PrintWriter out = new PrintWriter(new FileOutputStream(txt));
         TextExtractionStrategy strategy;
         for (int i = 1; i <= reader.getNumberOfPages(); i++) {
             strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
             out.println(strategy.getResultantText());
         }
         out.flush();
         out.close();
         reader.close();
     }
 
 private static String INPUTFILE = "d:\\examplePDF.pdf";
    private static String OUTPUTFILE = "d:\\exampleText.txt";
 
    public static void main(String[] args) throws DocumentException, IOException {
     
     System.out.println("Program Starts");
     new pdfToText().partPdf(INPUTFILE, OUTPUTFILE);
     System.out.println("Program Ends");
        
    }
 
}

Verify Content in PDF-Page wise (Core JAVA)


If you are using maven then add dependency for itextpdf from below link or add itextpdf jars to your project:
Maven Dependency itextpdf

Download sample pdf from below link and and save it in your d drive as examplePDF.pdf
Download Sample PDF (examplePDF.pdf)

Output is :
Page Number: 1 Fail
Page Number: 2 Pass
Page Number: 3 Fail
Page Number: 4 Fail



package automation.prac;

import java.io.IOException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;

public class getContentPdf {
 
 private static String INPUTFILE = "d:\\examplePDF.pdf";
 
 public void parsePdf(String pdf) throws IOException {
  
        PdfReader reader = new PdfReader(pdf);
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        TextExtractionStrategy strategy;
        
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            if(strategy.getResultantText().contains("Gurdial Singh")){
             System.out.print("Page Number: "+i);
             System.out.println(" Pass");
            }
            else{
             System.out.print("Page Number: "+i);
             System.out.println(" Fail");
            }
        }
      }

 public static void main(String[] args) throws IOException {
  
  new getContentPdf().parsePdf(INPUTFILE);
 }

}

Monday, 13 July 2015

Program to determine Prime Numbers from 1 to 100 (Core JAVA)

Save File as primeNumber.java



package myFirstProject;

public class primeNumber {

 public static void main(String[] args) {
  
   int a1 =0;
   int number =0;
   //Empty String for printing Prime Numbers
   String  primeNumbers = "";

        for (a1 = 1; a1 <= 100; a1++)         
        {        
           int counter=0;    
           for(number =a1; number>=1; number--)
           {
              if(a1%number==0)
              {
               counter = counter + 1;
              }
           }
            if (counter ==2)
            {
         //Appended the Prime number to the String
         primeNumbers = primeNumbers + a1 + " ";
            } 
        } 
         System.out.println("Prime Numbers from 1 to 100 are :");
         System.out.println(primeNumbers);
 }
}

Friday, 10 July 2015

Swap value of two variable without using third varibale (Core JAVA)

Save File as swapVariables.java


package myFirstProject;

public class swapVariables {

 public static void main(String[] args) {
  
   int firstVariable = 10; 
   int secondVariable = 5;
   
   // Code to swap 'firstVariable' and 'secondVariable'
   firstVariable = firstVariable + secondVariable;   // firstVariable now becomes 15
   secondVariable = firstVariable - secondVariable;   // secondVariable becomes 10
   firstVariable = firstVariable - secondVariable;   // firstVariable becomes 5
   
   System.out.println("Value of firstVariable is: "+firstVariable);
   System.out.println("Value of secondVariable is: "+secondVariable);
 }
}

Selenium WebDriver and Project Challenges

Selenium WebDriver Challenges 


  • Dealing with pop-up windows
  • Testing dynamic text or content
  • How to go about testing Flash
  • Capturing screen shots, either to file or in some form of report
  • Iteration of the test case, running it repeatedly with some minor change
  • Data Driven Testing, using suites of pre-cooked data or generating it on the fly
  • Generating useful test status reports
  • Configuring Node
  • Setting up Grid
  • Handling Alerts Popups
  • Switching between windows
  • Working with Nested frames.
  • Field validation
  • How to identify dynamic objects.
  • Xpath and CSS locators for identifying elements.
  • File Upload/Download Using : Java-AutoIT-Selenium
  • Handling Multiple Popup Windows.
  • Switching with multiple Windows

Projects Challenges 

  1. Time pressure and deadlines
  2. Late migration of components and DBs
  3. Requirements getting changed in the middle
  4. Patch migration without intimating
  5. Less experienced resources
  6. Large data in DB which increases the testing time
  7. Third party intervention - when some orders are made, onsite guys have to approve the orders before Proceeding with next application
  8. Defect rejection without even analysing the probs
  9. Some defects which feels hardto recreate
  10. Finding critical test data -misbehaviour of system for a few types of test data.
  11. Bugs closing taking its max time
  12. n/w unavailability