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.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.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.webautomation.ScreenCaptureHtmlUnitDriver;

public class ZeppelinIT {
	private WebDriver getWebDriver(){
		WebDriver driver;
		if (System.getProperty("runningFromMaven")==null) {
			// zeppelin.daemon.packaged is defined by zeppelin-server/pom.xml
			// assumes it is not running from maven. but eclipse
			driver = new SafariDriver();
		} else { // assumes running from maven
			ScreenCaptureHtmlUnitDriver htmlUnitDriver = new ScreenCaptureHtmlUnitDriver(); //HtmlUnitDriver();
			htmlUnitDriver.setJavascriptEnabled(true);
			driver = htmlUnitDriver;
		}	
		
		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();
                }
            });
            
        } catch (WebDriverException e){
            File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            System.out.println("Screenshot in: " + scrFile.getAbsolutePath());
            fail("Error occured " + e.getMessage());
        } finally {
            // Close the browser
            driver.quit();
        }
    }
}

@@ -137,16 +137,49 @@
 ium.By;%0A
+import org.openqa.selenium.Keys;%0A
 import o
@@ -2652,32 +2652,1211 @@
 );%0A            %0A
+            // type some query%0A            driver.findElement(By.xpath(%22//div%5B@id='zqlEditor'%5D//textarea%22)).sendKeys(%22create table if not exists test (id STRING);%5Cn%22);%0A            driver.findElement(By.xpath(%22//div%5B@id='zqlEditor'%5D//textarea%22)).sendKeys(%22show tables;%5Cn%22);%0A            %0A            // press run button%0A            driver.findElement(By.linkText(%22Run%22)).click();%0A%0A            // wait for visualization%0A            (new WebDriverWait(driver, 60)).until(new ExpectedCondition%3CBoolean%3E() %7B%0A                public Boolean apply(WebDriver d) %7B%0A                    return d.findElement(By.xpath(%22//div%5B@id='visualizationContainer'%5D//iframe%22)).isDisplayed();%0A                %7D%0A            %7D);%0A            %0A            WebDriver iframe = driver.switchTo().frame(driver.findElement(By.xpath(%22//div%5B@id='visualizationContainer'%5D//iframe%22)));%0A            %0A            // wait for result displayed%0A            (new WebDriverWait(iframe, 20)).until(new ExpectedCondition%3CBoolean%3E() %7B%0A                public Boolean apply(WebDriver d) %7B%0A                    return d.findElement(By.xpath(%22//table//td%5Btext()='test'%5D%22)).isDisplayed();%0A                %7D%0A            %7D);           %0A
         %7D catch 
@@ -4093,24 +4093,25 @@
 tMessage());
+%09
 %0A        %7D f

Implement run query and check result