Append a text file in selenium

Here is a little code I wrote for appending a text file in selenium

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Pattern;

public class WriteTextFile extends SeleneseTestCase {
    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://");
        selenium.start();
    }

    @Test
    public void testUntitled() throws Exception {
        selenium.open("www.google.com");
        String filepath = "D:/FindMeHere2.txt" ;
        FileWriter somenewfile = CreateTxtFile(filepath);
       
        AppendTextToFile(filepath, "here is how i would append the text");
       
    }

/*    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }*/
   
        public FileWriter CreateTxtFile(String fpath) throws IOException {
        System.out.println("Inside the Create Text File method :::: ");
        //String fpath = "d:/mynewtextfile.txt";
        FileWriter mynewfile = new FileWriter(fpath, false);
        //FileOutputStream fout = new FileOutputStream(fpath);
        mynewfile.write("This is a new text file , that i wrote  into ");
        mynewfile.close();
        return mynewfile ;
       
    }
       
        public void AppendTextToFile(String filepath, String testtext) throws IOException {
            System.out.println("Inside the Append Text  to File method :::: ");
        //FileOutputStream fout = new FileOutputStream(fpath);
              FileWriter fstream = new FileWriter(filepath,true);
              BufferedWriter out = new BufferedWriter(fstream);
              out.newLine();
              out.write(testtext);
              //Close the output stream
              out.close();
           
        }
}

Hope this helps

www.CodeNirvana.in

Powered by Blogger.

Translate

Total Pageviews

Copyright © T R I A G E D T E S T E R