package framework.controls;

import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import framework.controls.interfaces.Detectable;

public abstract class BaseControl implements Detectable {

	private WebDriver driver;
	private WebElement baseWebElement;
	private By locator;

	public WebElement getUnderlyingWebElement() {
		return findControl(driver, locator);
	}

	private void setUnderlyingWebElement(WebElement element) {
		baseWebElement = element;
		System.out.println("WebElement found.");
	}

	@Override
	public boolean exists() {
		try {
			findControl();
			return baseWebElement.isDisplayed();
		} catch (ElementNotVisibleException e) {
			return false;
		}
	}

	protected WebElement findControl(WebDriver theDriver, By theLocator) {
		try {
			setDriver(theDriver);
			setLocator(theLocator);
			System.out.println("Looking for WebElement: " + locator.toString());
			setUnderlyingWebElement(driver.findElement(locator));
			return baseWebElement;
		} catch (NoSuchElementException e) {
			System.out.println("Failed looking for WebElement: "
					+ locator.toString());
			throw new NoSuchElementException(locator.toString());
		}
	}

	protected WebElement findControl() {
		try {
			System.out.println("Looking for WebElement: " + locator.toString());
			setUnderlyingWebElement(driver.findElement(locator));
			return baseWebElement;
		} catch (NoSuchElementException e) {
			System.out.println("Failed looking for WebElement: "
					+ locator.toString());
			throw new NoSuchElementException(locator.toString());
		}
	}

	protected void setLocator(By theLocator) {
		locator = theLocator;
	}

	protected By getLocator() {
		return locator;
	}

	protected void setDriver(WebDriver theDriver) {
		driver = theDriver;
	}

	protected WebDriver getDriver() {
		return driver;
	}
}

@@ -900,24 +900,37 @@
 ator) %7B%0A
-%09
 %09try %7B%0A
+%09%09%09int i = 0;%0A
 %09%09%09setDr
@@ -1094,32 +1094,153 @@
 ment(locator));%0A
+%09%09%09%0A%09%09%09while (!baseWebElement.isEnabled()%7C!baseWebElement.isDisplayed()&i%3C5) %7B%0A%09%09%09%09Thread.sleep(500);%0A%09%09%09%09i++;%0A%09%09%09%7D%0A%09%09%09%09%0A
 %09%09%09return baseWe
@@ -1421,32 +1421,237 @@
 or.toString());%0A
+%09%09%7D %0A%09%09catch (InterruptedException e) %7B%0A%09%09%09System.out.println(%22Failed looking for WebElement: %22%0A%09%09%09%09%09+ locator.toString());%0A%09%09%09e.printStackTrace();%0A%09%09%09throw new NoSuchElementException(locator.toString());%0A
 %09%09%7D%0A%09%7D%0A%0A%09protect
@@ -1688,16 +1688,30 @@
 %09%09try %7B%0A
+%09%09%09int i = 0;%0A
 %09%09%09Syste
@@ -1831,16 +1831,137 @@
 ator));%0A
+%09%09%09%0A%09%09%09while (!baseWebElement.isEnabled()%7C!baseWebElement.isDisplayed()&i%3C5) %7B%0A%09%09%09%09Thread.sleep(500);%0A%09%09%09%09i++;%0A%09%09%09%7D%0A%09%09%09%09%0A
 %09%09%09retur
@@ -2150,32 +2150,237 @@
 or.toString());%0A
+%09%09%7D %0A%09%09catch (InterruptedException e) %7B%0A%09%09%09System.out.println(%22Failed looking for WebElement: %22%0A%09%09%09%09%09+ locator.toString());%0A%09%09%09e.printStackTrace();%0A%09%09%09throw new NoSuchElementException(locator.toString());%0A
 %09%09%7D%0A%09%7D%0A%0A%09protect

Update BaseControl.java