package com.nflabs.zeppelin;

import static org.junit.Assert.fail;

import java.io.File;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ZeppelinIT {
	private WebDriver getWebDriver(){
		WebDriver driver = null;

		if (driver==null){
			try {
				FirefoxBinary ffox = new FirefoxBinary();
				if ("true".equals(System.getenv("TRAVIS"))) {
					ffox.setEnvironmentProperty("DISPLAY", ":99"); // xvfb is supposed to run with DISPLAY 99
				}
				FirefoxProfile profile = new FirefoxProfile();
				driver = new FirefoxDriver(ffox, profile);
			} catch (Exception e){				
			}
		}
					
		if (driver==null){
			try {
				driver = new ChromeDriver();
			} catch (Exception e){				
			}
		}
		
		if (driver==null){
			try {
				driver = new SafariDriver();
			} catch (Exception e){				
			}
		}
			
		String url;
		if (System.getProperty("url")!=null) {
			url = System.getProperty("url");
		} else {
			url = "http://localhost:8080";
		}
		driver.get(url);
		
        // wait for page load
        (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.findElement(By.partialLinkText("Start")).isDisplayed();
            }
        });

		return driver;
	}
	
    @Test
    public void testRunSimpleQueryInNewSession() {
        // Notice that the remainder of the code relies on the interface, 
        // not the implementation.
        WebDriver driver = getWebDriver();
        
        try {
            // click start
            WebElement start = driver.findElement(By.partialLinkText("Start"));
            start.click();

            // Wait for the page to load, timeout after 10 seconds
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return d.findElement(By.linkText("New")).isDisplayed();
                }
            });
            
            // click new
            driver.findElement(By.linkText("New")).click();
            
            // wait for run button appears
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return d.findElement(By.linkText("Run")).isDisplayed();
                }
            });
            
            // type some query
            driver.findElement(By.xpath("//div[@id='zqlEditor']//textarea")).sendKeys("create table if not exists test "+Keys.chord(Keys.SHIFT, "9")+"id STRING);\n");
            driver.findElement(By.xpath("//div[@id='zqlEditor']//textarea")).sendKeys("\nshow tables;");
            
            // press run button
            driver.findElement(By.linkText("Run")).click();

            // wait for button becomes Running ...
            (new WebDriverWait(driver, 5)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return d.findElement(By.xpath("//div//a[text()='Running ...']")).isDisplayed();
                }
            });  

            // wait for button becomes Run
            (new WebDriverWait(driver, 60)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return d.findElement(By.xpath("//div//a[text()='Run']")).isDisplayed();
                }
            });
            
            WebElement msg = driver.findElement(By.id("msgBox"));
            if (msg!=null) {
            	System.out.println("msgBox="+msg.getText());
            }
            
            // wait for visualization
            (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return d.findElement(By.xpath("//div[@id='visualizationContainer']//iframe")).isDisplayed();
                }
            });
            
            WebDriver iframe = driver.switchTo().frame(driver.findElement(By.xpath("//div[@id='visualizationContainer']//iframe")));
            
            // wait for result displayed
            (new WebDriverWait(iframe, 20)).until(new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return d.findElement(By.xpath("//table//td[text()='test']")).isDisplayed();
                }
            });           
        } catch (WebDriverException e){
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            System.out.println("Screenshot in: " + scrFile.getAbsolutePath());
            throw e;
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

@@ -5310,10 +5310,754 @@
 %7D%0A    %7D%0A
+    %0A    %0A    @Test%0A%09public void testZAN() %7B%0A%09%09WebDriver driver = getWebDriver();%0A%09%09%0A%09%09try %7B%0A%09%09%09// goto ZAN menu%0A%09%09%09driver.findElement(By.xpath(%22//ul//a%5Btext()='ZAN'%5D%22)).click();%0A%09%09%09%0A%09%09%09// wait for ZAN page loaded%0A%09%09%09(new WebDriverWait(driver, 20)).until(new ExpectedCondition%3CBoolean%3E() %7B%0A                public Boolean apply(WebDriver d) %7B%0A                    return d.findElement(By.xpath(%22//div//a%5Btext()='Update Catalog'%5D%22)).isDisplayed();%0A                %7D%0A            %7D);%0A%09%09%7D catch (WebDriverException e) %7B%0A%09%09%09File scrFile = ((TakesScreenshot) driver)%0A%09%09%09%09%09.getScreenshotAs(OutputType.FILE);%0A%09%09%09System.out.println(%22Screenshot in: %22 + scrFile.getAbsolutePath());%0A%09%09%09throw e;%0A%09%09%7D finally %7B%0A%09%09%09// Close the browser%0A%09%09%09driver.quit();%0A%09%09%7D%0A%09%7D%0A
 %7D%0A

Implement very basic test for ZAN gui