package nl.glasoperator.fitnesse.vodafone;

import nl.hsac.fitnesse.fixture.web.BrowserTest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverException;
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 click(String place) {
        boolean result = false;
        boolean retry = true;
        for (int i = 0;
             !result && retry && i < secondsBeforeTimeout();
             i++) {
            try {
                result = clickFirstButton(place);
                if (!result) {
                    result = super.click(place);
                }
            } catch (WebDriverException e) {
                String msg = e.getMessage();
                if (!msg.contains("Other element would receive the click")) {
                    retry = false;
                } else {
                    waitSeconds(1);
                }
            }
        }
        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 (element.isDisplayed() && element.isEnabled()) {
                    element.click();
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    public boolean clickR(String place) {
        boolean result = false;
        By byButtonText = getSeleniumHelper().byXpath("//label[contains(text(), '%s')]", place);
        List<WebElement> elements = getSeleniumHelper().driver().findElements(byButtonText);
        if (elements != null) {
            for (WebElement element : elements) {
                String id = element.getAttribute("id");
                String name = element.getAttribute("name");
                if (element.isDisplayed() && element.isEnabled()) {
                    element.click();
                    result = true;
                    break;
                }
            }
        }
        return result;
    }

    public boolean waitSeconds(int i) {
        boolean result;
        try {
            Thread.sleep(1 * 1000);
            result = true;
        } catch (InterruptedException e) {
            result = false;
        }
        return result;
    }
}

@@ -1669,709 +1669,8 @@
  %7D%0A%0A
-    public boolean clickR(String place) %7B%0A        boolean result = false;%0A        By byButtonText = getSeleniumHelper().byXpath(%22//label%5Bcontains(text(), '%25s')%5D%22, place);%0A        List%3CWebElement%3E elements = getSeleniumHelper().driver().findElements(byButtonText);%0A        if (elements != null) %7B%0A            for (WebElement element : elements) %7B%0A                String id = element.getAttribute(%22id%22);%0A                String name = element.getAttribute(%22name%22);%0A                if (element.isDisplayed() && element.isEnabled()) %7B%0A                    element.click();%0A                    result = true;%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A
     

Remove unused method