package com.crawljax.browser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.crawljax.core.CrawljaxException;
import com.crawljax.util.Helper;

import org.junit.Test;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.File;
import java.io.IOException;

public class WebDriverBackedEmbeddedBrowserTest {

	@Test
	public void testGetDocument() {
		File index = new File("src/test/site/iframe/index.html");
		// TODO Stefan; refactor out the direct use of the FirefoxDriver
		WebDriverBackedEmbeddedBrowser driver =
		        WebDriverBackedEmbeddedBrowser.withDriver(new FirefoxDriver(), null, 100, 100);

		Document doc;
		try {
			driver.goToUrl("file://" + index.getAbsolutePath());
			System.out.println("DOM with frames: " + driver.getDom());
			doc = Helper.getDocument(driver.getDom());
			NodeList frameNodes = doc.getElementsByTagName("IFRAME");
			assertEquals(5, frameNodes.getLength());

			doc = Helper.getDocument(driver.getDomWithoutIframeContent());
			frameNodes = doc.getElementsByTagName("IFRAME");
			assertEquals(3, frameNodes.getLength());

		} catch (SAXException e) {
			e.printStackTrace();
			fail(e.getMessage());
		} catch (IOException e) {
			e.printStackTrace();
			fail(e.getMessage());
		}

		driver.close();

	}

	@Test
	public void saveScreenShot() {
		// TODO Stefan; refactor out the direct use of the FirefoxDriver
		WebDriverBackedEmbeddedBrowser browser =
		        WebDriverBackedEmbeddedBrowser.withDriver(new FirefoxDriver(), null, 500, 500);

		File f = new File("webdriverfirefox-test-screenshot.png");
		if (!f.exists()) {
			browser.goToUrl("http://google.com");
			try {
				browser.saveScreenShot(f);
			} catch (CrawljaxException e) {
				fail(e.getMessage());
			}
			assertTrue(f.exists());
			assertTrue(f.delete());
		}

		browser.close();
	}
}

@@ -161,76 +161,48 @@
 ort 
-com.crawljax.core.CrawljaxException;%0Aimport com.crawljax.util.Helper
+java.io.File;%0Aimport java.io.IOException
 ;%0A%0Ai
@@ -376,48 +376,76 @@
 ort 
-java.io.File;%0Aimport java.io.IOException
+com.crawljax.core.CrawljaxException;%0Aimport com.crawljax.util.Helper
 ;%0A%0Ap
@@ -878,70 +878,9 @@
 ));%0A
-%09%09%09System.out.println(%22DOM with frames: %22 + driver.getDom());
 %0A
+
 %09%09%09d
@@ -998,17 +998,17 @@
 tEquals(
-5
+4
 , frameN
@@ -1161,17 +1161,17 @@
 tEquals(
-3
+2
 , frameN
@@ -1350,16 +1350,29 @@
 );%0A%09%09%7D%0A%0A
+%09%09finally %7B%0A%09
 %09%09driver
@@ -1380,16 +1380,20 @@
 close();
+%0A%09%09%7D
 %0A%0A%09%7D%0A%0A%09@

clean up