package org.talend.component;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import static org.openqa.selenium.support.ui.ExpectedConditions.elementToBeClickable;

public class StorybookTest {

    private static final String ACTION_LOGGER_CONSOLE_SELECTOR = ".horizontal.Pane2 > div > div > div:last-child";

    private static final String STORY_CATEGORY_SELECTOR = "div[data-name='%s']";

    private static final String STORY_MENU_SELECTOR = "a[data-name='%s']";

    protected static WebDriver driver;

    @BeforeClass
    public static void before() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://localhost:6006/");
    }

    @AfterClass
    public static void after() {
        driver.close();
    }

    protected void goToStory(final String categoryName, final String storyName) {
        this.goToMainElement();

        final WebElement categoryMenu = driver.findElement(By.cssSelector(String.format(STORY_CATEGORY_SELECTOR, categoryName)));
        final WebElement menusContainer = categoryMenu.findElement(By.xpath("following-sibling::*"));
        if (menusContainer.findElements(By.tagName("ul")).size() == 0) {
            categoryMenu.click();
        }

        WebDriverWait wait = new WebDriverWait(driver, 15);
        wait.until(elementToBeClickable(By.cssSelector(String.format(STORY_MENU_SELECTOR, storyName)))).click();

        this.goToStoryFrame();
    }

    protected void goToStoryFrame() {
        driver.switchTo().frame(driver.findElement(By.id("storybook-preview-iframe")));
    }

    protected void goToMainElement() {
        driver.switchTo().defaultContent();
    }

    protected String getActionLog() {
        this.goToMainElement();
        final String log = driver.findElement(By.cssSelector(ACTION_LOGGER_CONSOLE_SELECTOR)).getText();
        this.goToStoryFrame();
        return log;
    }
}

@@ -20,24 +20,72 @@
 component;%0A%0A
+import org.apache.http.client.utils.URIBuilder;%0A
 import org.j
@@ -337,24 +337,74 @@
 riverWait;%0A%0A
+import java.net.URL;%0Aimport java.net.URLEncoder;%0A%0A
 import stati
@@ -778,24 +778,205 @@
 me='%25s'%5D%22;%0A%0A
+    private static final String DEFAULT_STORY_NAME = %22default%22;%0A%0A    private static final String STORYBOOK_HOST = %22localhost%22;%0A%0A    private static final int STORYBOOK_PORT = 6006;%0A%0A
     protecte
@@ -1140,54 +1140,8 @@
 ();%0A
-        driver.get(%22http://localhost:6006/%22);%0A
     
@@ -1281,416 +1281,551 @@
 Name
-, final String storyName) %7B%0A        this.goToMainElement();%0A%0A        final WebElement categoryMenu = driver.findElement(By.cssSelector(String.format(STORY_CATEGORY_SELECTOR, categoryName)));%0A        final WebElement menusContainer = categoryMenu.findElement(By.xpath(%22following-sibling::*%22));%0A        if (menusContainer.findElements(By.tagName(%22ul%22)).size() == 0) %7B%0A            categoryMenu.click(
+) %7B%0A        goToStory(categoryName, DEFAULT_STORY_NAME);%0A    %7D%0A%0A    protected void goToStory(final String categoryName, final String storyName) %7B%0A        this.goToMainElement();%0A%0A        try %7B%0A            URIBuilder builder = new URIBuilder();%0A%0A            builder.setScheme(%22http%22);%0A            builder.setHost(STORYBOOK_HOST);%0A            builder.setPort(STORYBOOK_PORT);%0A            builder.setPath(%22/%22);%0A            builder.addParameter(%22selectedKind%22, categoryName);%0A            builder.addParameter(%22selectedStory%22, storyName
 );%0A
+%0A
         
 %7D%0A%0A 
@@ -1824,183 +1824,134 @@
     
-%7D%0A%0A        WebDriverWait wait = new WebDriverWait(driver, 15);%0A        wait.until(elementToBeClickable(By.cssSelector(String.format(STORY_MENU_SELECTOR, storyName)))).click();
+    driver.get(builder.build().toURL().toString());%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A%0A  

improve e2e gotostory (#1139)