Advertise

Sunday, October 25, 2009

Selenium PDF Testing

I came across queries related to Testing PDF in web page . As after clicking on PDF link , PDF file on on same page , But we can not either verify Text or any content on that page.

So I thought of copying that data into text File and comparing data in that Text File with actuall Text File.

As we know that after pressing CTRL+A ,CTRL+C text are stored in clipboard. So we can move that data in Text File .

Example given below is performed on selenium forum site itself.


Below is Java code for that

TextClipBoard Class where actuall data from PDF is moved to text file .

package Selenium;


import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import java.awt.event.KeyEvent;
import java.io.*;

public class TextClipBoard extends SeleneseTestCase {

public void setUp() throws Exception {
setUp("http://seleniumhq.org/", "*chrome");
}

public void testNew() throws Exception {

/*
* Opening Selenium Site
*/

selenium.open("/");

/*
* Clicking on link Forums
*/

selenium.click("link=forums");

/*
* Waiting for Elements presence because , while Testing page is getting timeout after
* default timeout period , So I searched for Communities keyword in that page
*/

for (int second = 0;; second++) {

if (second >= 60) fail("timeout");

try { if (selenium.isElementPresent("css=h4:contains(\"Communities\")")) break; }

catch (Exception e) {}

Thread.sleep(1000);
}

/*
* Clicking on recently posted link in forum site
*/

selenium.click("link=Clover report for Selenium tests");

/*
* Waiting for Default Timeout period
*/

selenium.waitForPageToLoad("30000");


/*
* Clicking on View as pdf link
*/

selenium.click("link=View as PDF");


/*
* Maximizing window and moving focus to that page
*/

selenium.windowMaximize();

selenium.windowFocus();

pause(10000);



selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL)); //Stands for CONTROL

pause(1000);

selenium.keyPressNative("65"); // Stands for A "ascii code for A"

pause(4000);

selenium.keyDownNative(String.valueOf(KeyEvent.VK_CONTROL));//Stands for CONTROL

pause(1000);

selenium.keyPressNative("67"); // Stands for C "ascii code for C"

/*
* Below code is call to Text Transfer class so as to retrieve data from Clip Board
* As we know that after copying data using ctrl+A and ctrl+C data remain in clip board
* We can move that data into Text File
*/

TextTransfer textTransfer = new TextTransfer();

//display what is currently on the clipboard

System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );
pause(4000);

selenium.stop(); // Kill the Browser

/*
* Writing data into Text File so as to compare these data with actual data.
* */

Writer output = null;

String text = textTransfer.getClipboardContents();

File file = new File("D:\\write.txt");

output = new BufferedWriter(new FileWriter(file));

output.write(text);

output.close();

try{

FileInputStream fstream = new FileInputStream("D:\\write.txt");

// Get the object of DataInputStream

DataInputStream in = new DataInputStream(fstream);

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String strLine;

//Read File Line By Line

while ((strLine = br.readLine()) != null) {

// Print the content on the console

System.out.println ("Reading from File line by line : " +strLine);

}

//Close the input stream

in.close();

}
catch (Exception e){//Catch exception if any

System.err.println("Error: " + e.getMessage());

}
}



}


TextTransfer class to retrieve data from Clipboard



package Selenium;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.Toolkit;
import java.io.*;

public final class TextTransfer implements ClipboardOwner {

public static void main (String... aArguments ){
TextTransfer textTransfer = new TextTransfer();

//display what is currently on the clipboard
System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );

//change the contents and then re-display

textTransfer.setClipboardContents("blah, blah, blah");

System.out.println("Clipboard contains:" + textTransfer.getClipboardContents() );

}

/**
* Empty implementation of the ClipboardOwner interface.
*/
public void lostOwnership( Clipboard aClipboard, Transferable aContents) {
//do nothing
}

/**
* Place a String on the clipboard, and make this class the
* owner of the Clipboard's contents.
*/
public void setClipboardContents( String aString ){
StringSelection stringSelection = new StringSelection( aString );

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

clipboard.setContents( stringSelection, this );

}

/**
* Get the String residing on the clipboard.
*
* @return any text found on the Clipboard; if none found, return an
* empty String.
*/
public String getClipboardContents() {
String result = "";

Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

//odd: the Object param of getContents is not currently used

Transferable contents = clipboard.getContents(null);

boolean hasTransferableText =

(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor)
;
if ( hasTransferableText ) {

try {

result = (String)contents.getTransferData(DataFlavor.stringFlavor);

}

catch (UnsupportedFlavorException ex){

//highly unlikely since we are using a standard DataFlavor
System.out.println(ex);

ex.printStackTrace();

}
catch (IOException ex) {

System.out.println(ex);

ex.printStackTrace();

}

}

return result;
}
}

No comments:

Post a Comment