views:

357

answers:

3

I'm trying to save the whole html page with getBodyText into a String and then write it to a file (.txt). However when I check the file, it's empty. Here's my code:

    String store_report = selenium.getBodyText();
    File f = new File("C:/folder/" + "report" + ".txt");
    FileWriter writer = new FileWriter(f);
    writer.append(store_report);
    System.out.println("Report Created is in Location : " + f.getAbsolutePath());
A: 

Well first, I'd strongly encourage you to consider a try/finally block which closes that FilreWriter :)

Can you confirm you see the getBodyText() command actually being sent to the Selenium server? Have you seen it run in the command log that is embedded inside the browser? Do you have a public URL that reproduces the problem?

Patrick Lightbody
I'm actually running this through IDEA/testng right now, is there a way to check that? Kind of a noob here
JLau
I used System.out.println(store_report) and I can see the text in the console, so we can conclude the getBodyText() works right?
JLau
+1  A: 

I think you just forgot to flush the FileWriter:

writer.flush()

This of course happens automatically if you properly close the FileWriter.

Josef
I added writer.close(); is that sufficient enough?
JLau
yes, that should do the trick
Josef
A: 

Thanks everyone for your help. I think what happened was I didn't close the FileWriter correctly, now it's working.

JLau