views:

1411

answers:

2

This is probably a java noob question but here is my scenario:

  1. using selenium, I captured the html source with getBodyText()
  2. using java, I want to save the information from getBodyText() into a html file so I can review it later

I currently have getBodyText() stored as a String, here's the code:

String stored_report = selenium.getBodyText();

File f = new File("C:/folder/" + "report" + ".html");
FileWriter writer = new FileWriter(f);
writer.append(stored_report);
System.out.println("Report Created is in Location : " + f.getAbsolutePath())
writer.close();

Do I have to use FileReader? What do I need to do so the saved html file still shows the html format? (currently since it's stored as a string, the page shows up with everything appear on one line)

Thanks in advance!

+2  A: 

Change to the following:

String stored_report = selenium.getBodyText();

File f = new File("C:/folder/" + "report" + ".html");
FileWriter writer = new FileWriter(f,true);
writer.write(stored_report);
System.out.println("Report Created is in Location : " + f.getAbsolutePath())
writer.close();

Your code looked sound except for appending operations. Using FileWriter(f,true) gives us appending operations on the write.

You only need the reader class if you want to read back the file you just wrote.

Update: Looks like selenium.getHtmlSource() exists and may do what you require. See This Post

Wayne
the code currently save the html source as string so after I save it as html file, all the original html format will be gone. I'd still want to see the original html format
JLau
thanks for the udpate, gethtmlsource() works!!
JLau
Good to see that works! You should mark this as being an accepted solution :)
Wayne
A: 

Can you give some ideas to my question posted in

Thanks

http://stackoverflow.com/questions/3242640/need-to-compare-2-html-documents-using-java-selenium

Getafix