package nl.hsac.fitnesse.fixture.web;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * Script fixture for testing prisma web application.
 */
public class PrismaBrowserTest extends BrowserTest {
    static final int MAX_WAIT_SECONDS = 30;
    public PrismaBrowserTest() {
        System.setProperty("webdriver.ie.driver", "C:\\dev\\IEDriverServer_Win32_2.33.0\\IEDriverServer.exe");
    }

    @Override
    public String pageTitle() {
        String pageTitle;
        String fullPageTitle = super.pageTitle();
        if (fullPageTitle.contains(" - ")) {
            pageTitle = fullPageTitle.substring(0, fullPageTitle.indexOf(" - "));
        } else {
            pageTitle = fullPageTitle;
        }
        return pageTitle;
    }

    public boolean clickAndWaitForPage(String place, final String pageTitleStart) {
        boolean result = click(place);
        if (result) {
            result = new WebDriverWait(getSeleniumHelper().driver(), MAX_WAIT_SECONDS)
                        .until(new ExpectedCondition<Boolean>() {
                            @Override
                            public Boolean apply(WebDriver f) {
                                return f.getTitle().startsWith(pageTitleStart);
                            }
                        }).booleanValue();
        }
        return result;
    }

    public boolean enterDateFor(String value, String place) {
        boolean result = false;
        String[] values = value.split("-");
        if (values.length == 3) {
            WebElement element = getElement(place);
            if ("span".equalsIgnoreCase(element.getTagName())) {
                String id = element.getAttribute("id");

                String dayId = id + "_d";
                result = enterFor(values[0], dayId);
                String monthId = id + "_m";
                result = enterFor(values[1], monthId);
                String yearId = id + "_y";
                result = enterFor(values[2], yearId);

                String hiddenId = id + "_h";
                result = enterForHidden(value.replace("-", "."), hiddenId);
            }
        }
        return result;
    }

    public boolean waitForToBe(String place, final String value) {
        boolean result = false;
        final WebElement element = getElement(place);
        if (element != null) {
            final String id = element.getAttribute("id");
            String currentText = element.getText();
            result = value.equalsIgnoreCase(currentText);
            if (!result) {
            result = new WebDriverWait(getSeleniumHelper().driver(), MAX_WAIT_SECONDS)
                        .until(new ExpectedCondition<Boolean>() {
                            @Override
                            public Boolean apply(WebDriver f) {
                                WebElement elem = f.findElement(By.id(id));
                                String text = elem.getText();
                                return value.equalsIgnoreCase(text);
                            }
                        }).booleanValue();

            }
        }
        return result;
    }

}

@@ -1079,72 +1079,19 @@
 t = 
-new WebDriverWait(getSeleniumHelper().driver(), MAX_WAIT_SECONDS
+waitDriver(
 )%0A  
@@ -2645,32 +2645,36 @@
 ) %7B%0A            
+    
 result = new Web
@@ -2670,74 +2670,25 @@
 t = 
-new WebDriverWait(getSeleniumHelper().driver(), MAX_WAIT_SECONDS)%0A
+waitDriver()%0A    
     
@@ -2769,32 +2769,36 @@
                 
+    
 @Override%0A      
@@ -2811,32 +2811,36 @@
                 
+   
+ 
 public Boolean a
@@ -2851,32 +2851,36 @@
 (WebDriver f) %7B%0A
+    
                 
@@ -2967,24 +2967,28 @@
             
+  
+  
 String text 
@@ -2997,32 +2997,36 @@
 elem.getText();%0A
+    
                 
@@ -3098,34 +3098,42 @@
                 
+ 
-%7D%0A
+   %7D%0A    
                 
@@ -3214,10 +3214,140 @@
 %0A    %7D%0A%0A
+    private WebDriverWait waitDriver() %7B%0A        return new WebDriverWait(getSeleniumHelper().driver(), MAX_WAIT_SECONDS);%0A    %7D%0A%0A
 %7D%0A

Extract common code