/**
* Copyright © 1998-2016, Glodon Inc. All Rights Reserved.
*/
package org.suren.autotest.web.framework.selenium.action;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.suren.autotest.web.framework.core.action.ValueEditor;
import org.suren.autotest.web.framework.core.ui.Element;
import org.suren.autotest.web.framework.selenium.SeleniumEngine;
import org.suren.autotest.web.framework.selenium.strategy.SearchStrategyUtils;

/**
 * 给文本框中填入值
 * @author suren
 * @since jdk1.6 2016年6月29日
 */
@Component
public class SeleniumValueEditor implements ValueEditor
{
	private static final Logger logger = LoggerFactory.getLogger(SeleniumValueEditor.class);

	@Autowired
	private SeleniumEngine			engine;
	@Autowired
	private SearchStrategyUtils		searchStrategyUtils;

	@Override
	public Object getValue(Element ele)
	{
		return searchStrategyUtils.findStrategy(WebElement.class, ele).search(ele).getText();
	}

	/**
	 * 实现逻辑：点击文本框、清空文本框、填入值 
	  */
	@Override
	public void setValue(Element ele, Object value)
	{
		if(value == null)
		{
			value = "";
		}

		WebElement webEle = searchStrategyUtils.findStrategy(WebElement.class, ele).search(ele);
		if(webEle != null)
		{
			try
			{
				webEle.click();
				webEle.clear();
				webEle.sendKeys(value.toString());
			}
			catch(WebDriverException e)
			{
				if(e.getMessage().contains("Element is not clickable at point"))
				{
					((JavascriptExecutor) engine.getDriver()).executeScript("arguments[0].scrollIntoView();", webEle);

					webEle.click();
					webEle.clear();
					webEle.sendKeys(value.toString());
				}
			}
		}
		else
		{
			logger.error(String.format("can not found element [%s].", ele));
		}
	}

	@Override
	public void submit(Element ele)
	{
		WebElement webEle = searchStrategyUtils.findStrategy(WebElement.class, ele).search(ele);
		webEle.submit();
	}

	@Override
	public boolean isEnabled(Element element)
	{
		return searchStrategyUtils.findStrategy(WebElement.class, element).search(element).isEnabled();
	}

	@Override
	public boolean isHidden(Element element)
	{
		return !searchStrategyUtils.findStrategy(WebElement.class, element).search(element).isDisplayed();
	}

}

@@ -1486,24 +1486,71 @@
 %09try%0D%0A%09%09%09%7B%0D%0A
+%09%09%09%09String valueStr = value.toString();%0D%0A%09%09%09%09%0D%0A
 %09%09%09%09webEle.c
@@ -1619,16 +1619,242 @@
 ng());%0D%0A
+%09%09%09%09%0D%0A%09%09%09%09if(%22input%22.equals(webEle.getTagName()))%0D%0A%09%09%09%09%7B%0D%0A%09%09%09%09%09if(!valueStr.equals(webEle.getAttribute(%22value%22)))%0D%0A%09%09%09%09%09%7B%0D%0A%09%09%09%09%09%09webEle.click();%0D%0A%09%09%09%09%09%09webEle.clear();%0D%0A%09%09%09%09%09%09webEle.sendKeys(value.toString());%0D%0A%09%09%09%09%09%7D%0D%0A%09%09%09%09%7D%0D%0A
 %09%09%09%7D%0D%0A%09%09
@@ -1921,24 +1921,16 @@
 ntains(%22
-Element 
 is not c
@@ -2149,32 +2149,83 @@
 ing());%0D%0A%09%09%09%09%7D%0D%0A
+%09%09%09%09else%0D%0A%09%09%09%09%7B%0D%0A%09%09%09%09%09e.printStackTrace();%0D%0A%09%09%09%09%7D%0D%0A
 %09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09els

解决了文本框不在可是区域时，无法填写的问题