/**
 * Copyright 2000-present Liferay, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liferay.blade.sample.test.functional.utils;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * @author Lawrence Lee
 */
public class BladeSampleFunctionalActionUtil {

	public static void customClick(WebDriver webDriver, WebElement webElement) {
		Actions action = new Actions(webDriver);

		Actions actionMoveTo = action.moveToElement(webElement);

		Action buildActionMoveTo = actionMoveTo.build();

		buildActionMoveTo.perform();

		WebDriverWait wait = new WebDriverWait(webDriver, 30);

		WebElement element = wait.until(
			ExpectedConditions.elementToBeClickable(webElement));

		element.click();
	}

	public static WebDriver implicitWait(WebDriver webDriver) {
		Options webDriverOptions = webDriver.manage();

		Timeouts timeouts = webDriverOptions.timeouts();

		timeouts.implicitlyWait(30, TimeUnit.SECONDS);

		return webDriver;
	}

	public static boolean isClickable(
		WebDriver webDriver, WebElement webelement) {

		WebDriverWait webDriverWait = new WebDriverWait(webDriver, 30);

		try {
			webDriverWait.until(
				ExpectedConditions.elementToBeClickable(webelement));

			return true;
		}
		catch (org.openqa.selenium.TimeoutException te) {
			return false;
		}
	}

	public static boolean isPageLoaded(WebDriver webDriver, String string) {
		WebDriverWait webDriverWait = new WebDriverWait(webDriver, 30);

		try {
			webDriverWait.until(ExpectedConditions.urlMatches(string));

			return true;
		}
		catch (org.openqa.selenium.TimeoutException te) {
			return false;
		}
	}

	public static boolean isTextPresent(
		WebDriver webDriver, WebElement webelement, String string) {

		WebDriverWait webDriverWait = new WebDriverWait(webDriver, 30);

		try {
			webDriverWait.until(
				ExpectedConditions.textToBePresentInElement(
					webelement, string));

			return true;
		}
		catch (org.openqa.selenium.TimeoutException te) {
			return false;
		}
	}

	public static boolean isVisible(
		WebDriver webDriver, WebElement webelement) {

		WebDriverWait webDriverWait = new WebDriverWait(webDriver, 30);

		try {
			webDriverWait.until(ExpectedConditions.visibilityOf(webelement));

			return true;
		}
		catch (org.openqa.selenium.TimeoutException te) {
			return false;
		}
	}

}
@@ -868,56 +868,8 @@
 nt;%0A
-import org.openqa.selenium.interactions.Action;%0A
 impo
@@ -1299,250 +1299,190 @@
 tion
- buildActionMoveTo = actionMoveTo.build(
+s actionOffset = actionMoveTo.moveByOffset(1, 1
 );%0A%0A%09%09
-build
 Action
-MoveTo.perform();%0A%0A%09%09WebDriverWait wait = new WebDriverWait(webDriver, 30);%0A%0A%09%09WebElement element = wait.until(%0A%09%09%09ExpectedConditions.elementToBeClickable(webElement));%0A%0A%09%09element.click
+s revertOffset = actionOffset.moveByOffset(-1, -1);%0A%0A%09%09Actions clickElement = revertOffset.click();%0A%0A%09%09clickElement.perform
 ();%0A

improve customClick function