Skip to content
Snippets Groups Projects
Commit 4e2fbc08 authored by anttil's avatar anttil
Browse files

SWE-2 / SP-314 : Fix stale reference problem

SVN: 26974
parent 1bc76cfc
No related branches found
No related tags found
No related merge requests found
......@@ -17,8 +17,14 @@
package ch.systemsx.cisd.openbis.uitest.infra.webdriver;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement;
import ch.systemsx.cisd.openbis.uitest.infra.dsl.SeleniumTest;
......@@ -41,11 +47,63 @@ public class PageProxy
@SuppressWarnings("unchecked")
public <T> T get(final Class<T> clazz)
{
ProxyFactory factory = new ProxyFactory();
factory.setSuperclass(clazz);
MethodHandler handler = new MethodHandler()
{
@Override
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
throws Throwable
{
try
{
return proceed.invoke(self, args);
} catch (InvocationTargetException e)
{
if (e.getTargetException() instanceof StaleElementReferenceException)
{
System.out.println("STALE REFERENCE - RELOADING " + self);
init(clazz, (T) self);
return proceed.invoke(self, args);
} else
{
throw e.getTargetException();
}
}
}
};
T t;
try
{
T t = clazz.newInstance();
t = (T) factory.create(new Class<?>[0], new Object[0], handler);
} catch (IllegalArgumentException ex1)
{
throw new RuntimeException(ex1);
} catch (NoSuchMethodException ex1)
{
throw new RuntimeException(ex1);
} catch (InstantiationException ex1)
{
throw new RuntimeException(ex1);
} catch (IllegalAccessException ex1)
{
throw new RuntimeException(ex1);
} catch (InvocationTargetException ex1)
{
throw new RuntimeException(ex1);
}
return init(clazz, t);
}
Class<T> pageClass = clazz;
private <T> T init(final Class<T> clazz, T t)
{
try
{
Class<?> pageClass = clazz;
while (pageClass != null)
{
for (Field field : pageClass.getDeclaredFields())
......@@ -102,7 +160,7 @@ public class PageProxy
}
}
pageClass = (Class<T>) pageClass.getSuperclass();
pageClass = pageClass.getSuperclass();
}
return t;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment