All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.automationrockstars.design.gir.webdriver.WebCache Maven / Gradle / Ivy

package com.automationrockstars.design.gir.webdriver;

import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;

import com.automationrockstars.base.ConfigLoader;
import com.automationrockstars.design.gir.webdriver.plugin.UiDriverPlugin;
import com.automationrockstars.design.gir.webdriver.plugin.UiDriverPluginService;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Maps;

public class WebCache  {
	private final static CacheCleaner cleaner = new CacheCleaner();
	static {
		UiDriverPluginService.registerPlugin(cleaner);
	}
	
	static class CacheCleaner implements  UiDriverPlugin{ 
		@Override
		public void beforeInstantiateDriver() {
		}

		@Override
		public void beforeGetDriver() {
		}

		@Override
		public void afterGetDriver(WebDriver driver) {
		}

		@Override
		public void beforeCloseDriver(WebDriver driver) {
			
		}

		@Override
		public void afterCloseDriver() {
			webCache.remove();
		}

		@Override
		public void afterInstantiateDriver(WebDriver driver) {		
		}
	}

	private static final ThreadLocal>>>> webCache = new ThreadLocal>>>>(){

		@Override
		protected Map>>> initialValue(){
			return Maps.newConcurrentMap();
		}

	};
	
	
	private static final ThreadLocal> lastQuery = new ThreadLocal();
	private static final ThreadLocal lastQueryCount = new ThreadLocal<>();
	private static final Integer REPEAT_TRESHOLD = ConfigLoader.config().getInteger("web.cache.max.repeat", 10); 
	private boolean isRepeated(SearchContext s, By by){
	if (lastQuery.get() != null && lastQuery.get().getKey().equals(s) && lastQuery.get().getValue().equals(by)){
			if (lastQueryCount.get() > REPEAT_TRESHOLD){
				return true;
			} else {
				lastQueryCount.set(lastQueryCount.get()+1);
				return false;
			}
		} else {
			lastQuery.set(Collections.singletonMap(s, by).entrySet().iterator().next());
			lastQueryCount.set(0);
			return false;
		}
	}
	private List find(SearchContext s, By by){
		Map>> els = webCache.get().get(s);
		boolean valid = false;
		if (els == null){
			els = Maps.newConcurrentMap();
			els.put(by,new SoftReference(s.findElements(by)));
			webCache.get().put(s, els);
			valid = true;
		} else {
			SoftReference> targets = els.get(by);			
			if (targets == null || targets.get() == null || targets.get().isEmpty()
					|| isRepeated(s, by)){
				targets = new SoftReference(s.findElements(by));
				els.put(by, targets);
				valid = true;
			} 
		} 
		if (! valid){
			FluentIterable result = FluentIterable.from(webCache.get().get(s).get(by).get());
			result = result.filter(new Predicate() {
				@Override
				public boolean apply(WebElement input) {
					try {
						return input.getTagName() != null;
					} catch (WebDriverException e){
						return false;
					}
				}				
			});
			if (result.isEmpty()){
				els.put(by,new SoftReference( s.findElements(by)));
				valid = true;
			} 
		}
		return webCache.get().get(s).get(by).get();
	}

	private WebCache(){

	}
	private static final ThreadLocal cacheFinders = new InheritableThreadLocal(){
		@Override
		protected WebCache initialValue(){
			return new WebCache();
		}
	};
	public static List fromCache(SearchContext s,By by){
		return cacheFinders.get().find(s, by);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy