Read text from Notepad
Here's the program to demonstrate reading the text data from notepad using "java.awt" API.This is done by following 3 steps:
1. Open the file present in the machine using notepad application.
2. Copy text from notepad using keyboard events(i.e. Cntrl+C).
3. Once the text data is copied, close the notepad and print the captured text(i.e. data captured from notepad).
Sample File:
Sample Program:
package pack1;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;
/** Reading data from notepad.
*
* @author http://automation-home.blogspot.in/
*/
public class ReadTextFromNotepad {
static Runtime rs = Runtime.getRuntime();
static Robot robot = null;
static String filePath = null;
public static void main(String[] args) {
try {
robot = new Robot();
//Note: file should present at path "C:\\Content.txt"
filePath = "C:\\Content.txt";
//Opens the file in notepad.
openNotepad();
//Reads the data from notepad.
String[] notepadContent = readFromNotepad();
//Close the notepad.
closeNotepad();
//Print the data(i.e. text captured from notepad).
for (String text : notepadContent) {
System.out.println(text);
}
} catch (Exception e) {
System.out.println(e);
}
}
/** Opens the file in notepad.
*
*/
public static void openNotepad() {
try {
rs.exec("notepad "+filePath);
robot.delay(500);
} catch (IOException e) {
e.printStackTrace();
}
}
/** Read's the data from notepad.
*
* @return notepadContent as String[]
*/
public static String[] readFromNotepad() {
// Copy text from notepad.
String[] notepadContent = null;
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(DataFlavor.stringFlavor);
try {
String strContent = (String) contents
.getTransferData(DataFlavor.stringFlavor);
notepadContent = strContent.split("\n");
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return notepadContent;
}
/** Closes the opened notepad application.
*
*/
public static void closeNotepad() {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_ALT);
}
}
Output:
Note:
1. Before executing above sample program, file should be present in the file path(i.e. specified in the variable "filePath").
2. While the program is executing don't move the mouse or type(i.e. don't take over the keyboad/mouse control).
Verifying the text using browser popup 'Open' file functionality
Most of the time, we verify the text present in the file by downloading the file(i.e. saving the file into the machine) and reading the data present in the file using the 'java.io.*' API.
Below program demonstrates verifying the text present in the file without saving the file i.e. by using browser's 'Open' file functionality.
Manual Steps:
1. Open the URL 'https://code.google.com/p/selenium/downloads/detail?name=downloads_location&can=2&q='
2. Click on download button/link
3. Popup appears(by default 'Open with' radio button is selected).
4. Click on popup 'OK' button (or) Keyboard 'Enter' key
5. File is opened.
6. Verify the text in the opened file and close the file
Sample automation script to demonstrate the above manual steps:
package wd;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.KeyEvent;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class OpenFile {
static WebDriver driver = null;
static Robot robot = null;
static String URL = "https://code.google.com/p/selenium/downloads/detail?name=downloads_location&can=2&q=";
public static void main(String[] args) throws InterruptedException, AWTException {
driver = new FirefoxDriver();
driver.manage().window().maximize();
robot = new Robot();
downloadFile();
String[] notepadContent = readFromNotepad();
//Close the notepad.
closeNotepad();
String expectedResults = "see description of the new location at https://code.google.com/p/selenium/downloads/list";
if(expectedResults.equalsIgnoreCase(notepadContent[0])){
System.out.println("Success text matches");
}else{
System.out.println("Failure text matches");
}
quitDriver();
}
public static void quitDriver() throws InterruptedException{
Thread.sleep(1000);
driver.close();
driver.quit();
}
/** Open the file in web page.
*
* @throws InterruptedException
*/
public static void downloadFile() throws InterruptedException{
driver.get(URL);
Thread.sleep(1000);
WebElement webElement = driver.findElement(By.xpath("//div[@class='box-inner']/a"));
webElement.click();
Thread.sleep(1000);
//Open the file.
robot.keyPress(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1500);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(1500);
}
/** Read's the data from notepad.
*
* @return notepadContent as String[]
*/
public static String[] readFromNotepad() {
// Copy text from notepad.
String[] notepadContent = null;
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_CONTROL);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(DataFlavor.stringFlavor);
try {
String strContent = (String) contents.getTransferData(DataFlavor.stringFlavor);
notepadContent = strContent.split("\n");
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return notepadContent;
}
/** Closes the opened notepad application.
*
*/
public static void closeNotepad() {
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_ALT);
}
}
Output:
Note:
1. Above program is verified only on windows machine.
2. Program/script work's only with text files(i.e. txt files).
3. While the program is executing don't move the mouse or type(i.e. don't take over the keyboad/mouse control).
Praveen nice post but in real time we never take data from notepad, we always prefer excel sheets. We always take from properties files (object repository).Any ways keep posting
ReplyDeleteMain idea is to read the text file with out downloading the file, refer below url for sample program using webdriver.
ReplyDeletehttp://automation-home.blogspot.in/2014/08/ReadTextFromNotepad.html#webdriver
oh ok got it
ReplyDelete