package nl.glasoperator.fitnesse.vodafone;

import nl.hsac.fitnesse.fixture.web.BrowserTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.util.List;

/**
 * Test fixture to run Selenium tests against Vodafone sites.
 */
public class VodafoneBrowserTest extends BrowserTest {
    @Override
    public boolean clickImpl(String place) {
        boolean result = clickFirstButton(place);
        if (!result) {
            result = super.clickImpl(place);
        }
        return result;
    }

    private boolean clickFirstButton(String place) {
        boolean result = false;
        By byButtonText = getSeleniumHelper().byXpath("//button[text() = '%s']", place);
        List<WebElement> elements = getSeleniumHelper().driver().findElements(byButtonText);
        if (elements != null) {
            for (WebElement element : elements) {
                if (clickElement(element)) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    public String errorOn(String label) {
        String result = null;
        By errorXPath = getSeleniumHelper()
                            .byXpath("//label[text() = '%s']/following-sibling::div/p[@class='help-block']",
                                    label);
        WebElement element = getSeleniumHelper().findElement(errorXPath);
        if (element != null) {
            result = element.getText();
        }
        return result;
    }
}

@@ -157,16 +157,69 @@
 ement;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Arrays;%0A
 import j
@@ -1556,11 +1556,2119 @@
 t;%0A    %7D
+%0A%0A    public boolean errorStyleOn(String label) %7B%0A        boolean result = false;%0A        By controlGroupXPath = getSeleniumHelper().byXpath(%22//div%5Blabel%5Btext() = '%25s'%5D%5D%22, label);%0A        WebElement element = getSeleniumHelper().findElement(controlGroupXPath);%0A        if (element != null) %7B%0A            String classAttr = element.getAttribute(%22class%22);%0A            if (classAttr != null) %7B%0A                String%5B%5D classes = classAttr.split(%22 %22);%0A                result = Arrays.asList(classes).contains(%22error%22);%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    public String errorsOnOthersThan(String label) %7B%0A        String result = %22%22;%0A        By otherErrorXPath = getSeleniumHelper()%0A                .byXpath(%22//label%5Btext() != '%25s'%5D/following-sibling::div/p%5B@class='help-block' and normalize-space(text()) != ''%5D%22,%0A                        label);%0A        List%3CWebElement%3E elements = getSeleniumHelper().driver().findElements(otherErrorXPath);%0A        if (elements != null) %7B%0A            List%3CString%3E errors = new ArrayList%3CString%3E(elements.size());%0A            for (WebElement element : elements) %7B%0A                String errorText = element.getText();%0A                errors.add(errorText);%0A            %7D%0A            if (!errors.isEmpty()) %7B%0A                result = errors.toString();%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    public String errorStyleOnOthersThan(String label) %7B%0A        String result = %22%22;%0A        By otherErrorXPath = getSeleniumHelper()%0A                .byXpath(%22//div%5Bcontains(@class, 'error') and label%5Btext() != '%25s'%5D%5D/label%22,%0A                        label);%0A        List%3CWebElement%3E elements = getSeleniumHelper().driver().findElements(otherErrorXPath);%0A        if (elements != null) %7B%0A            List%3CString%3E labels = new ArrayList%3CString%3E(elements.size());%0A            for (WebElement element : elements) %7B%0A                String labelText = element.getText();%0A                labels.add(labelText);%0A            %7D%0A            if (!labels.isEmpty()) %7B%0A                result = labels.toString();%0A            %7D%0A        %7D%0A        return result;%0A    %7D
 %0A%7D%0A

Allow checking of presence of error style. And allow tests on absence of error style/text on other labels