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

SWE-2 / SP-314 : Screenshotting fixed

SVN: 26929
parent f7a867ac
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2012 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.openbis.uitest.infra.screenshot;
import java.lang.reflect.Field;
import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;
import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;
import org.openqa.selenium.support.pagefactory.FieldDecorator;
import ch.systemsx.cisd.openbis.uitest.suite.SeleniumTest;
public class ScreenShotDecorator implements FieldDecorator
{
private DefaultFieldDecorator dec;
private ScreenShotter shotter;
public ScreenShotDecorator(ScreenShotter shotter)
{
this.dec = new DefaultFieldDecorator(new DefaultElementLocatorFactory(SeleniumTest.driver));
this.shotter = shotter;
}
@Override
public Object decorate(ClassLoader loader, Field field)
{
Object o = this.dec.decorate(loader, field);
if (o == null)
{
return null;
}
return ScreenShotProxy.newInstance(o, shotter);
}
}
/*
* Copyright 2012 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.openbis.uitest.infra.screenshot;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashSet;
public class ScreenShotProxy implements InvocationHandler
{
private Object obj;
private ScreenShotter shotter;
public static Object newInstance(Object obj, ScreenShotter shotter)
{
Collection<Class<?>> interfaces = new HashSet<Class<?>>();
Class<?> current = obj.getClass();
while (current != null)
{
for (Class<?> c : current.getInterfaces())
{
interfaces.add(c);
}
current = current.getSuperclass();
}
return java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
interfaces.toArray(new Class<?>[0]),
new ScreenShotProxy(obj, shotter));
}
private ScreenShotProxy(Object obj, ScreenShotter shotter)
{
this.obj = obj;
this.shotter = shotter;
}
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
{
Object result;
try
{
if (m.getName().equals("click") || m.getName().equals("sendKeys"))
{
shotter.screenshot();
}
result = m.invoke(this.obj, args);
} catch (InvocationTargetException e)
{
throw e.getTargetException();
} catch (Exception e)
{
throw new RuntimeException("unexpected invocation exception: " +
e.getMessage());
}
return result;
}
}
...@@ -27,7 +27,6 @@ import org.openqa.selenium.By; ...@@ -27,7 +27,6 @@ import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException; import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import ch.systemsx.cisd.openbis.uitest.infra.screenshot.ScreenShotProxy;
import ch.systemsx.cisd.openbis.uitest.infra.screenshot.ScreenShotter; import ch.systemsx.cisd.openbis.uitest.infra.screenshot.ScreenShotter;
import ch.systemsx.cisd.openbis.uitest.suite.SeleniumTest; import ch.systemsx.cisd.openbis.uitest.suite.SeleniumTest;
import ch.systemsx.cisd.openbis.uitest.widget.AtomicWidget; import ch.systemsx.cisd.openbis.uitest.widget.AtomicWidget;
...@@ -138,8 +137,7 @@ public class PageProxy ...@@ -138,8 +137,7 @@ public class PageProxy
} }
} }
widget.setContext(new WidgetContext((WebElement) ScreenShotProxy widget.setContext(new WidgetContext(element, shotter));
.newInstance(element, shotter)));
field.set(t, widget); field.set(t, widget);
} catch (IllegalArgumentException ex) } catch (IllegalArgumentException ex)
{ {
......
...@@ -16,30 +16,39 @@ ...@@ -16,30 +16,39 @@
package ch.systemsx.cisd.openbis.uitest.infra.webdriver; package ch.systemsx.cisd.openbis.uitest.infra.webdriver;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.openqa.selenium.By; import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement; import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.interactions.Actions;
import ch.systemsx.cisd.openbis.uitest.infra.screenshot.ScreenShotter;
import ch.systemsx.cisd.openbis.uitest.suite.SeleniumTest; import ch.systemsx.cisd.openbis.uitest.suite.SeleniumTest;
import ch.systemsx.cisd.openbis.uitest.widget.AtomicWidget; import ch.systemsx.cisd.openbis.uitest.widget.AtomicWidget;
/** /**
* @author anttil * @author anttil
*/ */
public class WidgetContext public class WidgetContext implements WebElement
{ {
public WebElement element; private WebElement element;
public WidgetContext(WebElement element) private ScreenShotter shotter;
public WidgetContext(WebElement element, ScreenShotter shotter)
{ {
this.element = element; this.element = element;
this.shotter = shotter;
} }
@Override
public void click() public void click()
{ {
shotter.screenshot();
element.click(); element.click();
} }
...@@ -48,11 +57,13 @@ public class WidgetContext ...@@ -48,11 +57,13 @@ public class WidgetContext
return element.isDisplayed() && element.isEnabled(); return element.isDisplayed() && element.isEnabled();
} }
@Override
public String getAttribute(String key) public String getAttribute(String key)
{ {
return element.getAttribute(key); return element.getAttribute(key);
} }
@Override
public String getTagName() public String getTagName()
{ {
return element.getTagName(); return element.getTagName();
...@@ -63,14 +74,16 @@ public class WidgetContext ...@@ -63,14 +74,16 @@ public class WidgetContext
element.sendKeys(keys); element.sendKeys(keys);
} }
@Override
public void clear() public void clear()
{ {
shotter.screenshot();
element.clear(); element.clear();
} }
public WebElement find(String xpath) public WebElement find(String xpath)
{ {
return element.findElement(By.xpath(xpath)); return new WidgetContext(element.findElement(By.xpath(xpath)), shotter);
} }
public <T extends AtomicWidget> T find(String xpath, Class<T> widgetClass) public <T extends AtomicWidget> T find(String xpath, Class<T> widgetClass)
...@@ -92,19 +105,98 @@ public class WidgetContext ...@@ -92,19 +105,98 @@ public class WidgetContext
{ {
e = e.findElement(By.xpath(".//" + t.getTagName())); e = e.findElement(By.xpath(".//" + t.getTagName()));
} }
t.setContext(new WidgetContext(e)); t.setContext(new WidgetContext(e, shotter));
return t; return t;
} }
public List<WebElement> findAll(String xpath) public List<WebElement> findAll(String xpath)
{ {
return element.findElements(By.xpath(xpath)); List<WebElement> result = new ArrayList<WebElement>();
for (WebElement e : element.findElements(By.xpath(xpath)))
{
result.add(new WidgetContext(e, shotter));
}
return result;
} }
public void mouseOver() public void mouseOver()
{ {
shotter.screenshot();
Actions builder = new Actions(SeleniumTest.driver); Actions builder = new Actions(SeleniumTest.driver);
builder.moveToElement(element).build().perform(); builder.moveToElement(element).build().perform();
} }
@Override
public WebElement findElement(By arg0)
{
return new WidgetContext(element.findElement(arg0), shotter);
}
@Override
public List<WebElement> findElements(By arg0)
{
List<WebElement> result = new ArrayList<WebElement>();
for (WebElement e : element.findElements(arg0))
{
result.add(new WidgetContext(e, shotter));
}
return result;
}
@Override
public String getCssValue(String arg0)
{
return element.getCssValue(arg0);
}
@Override
public Point getLocation()
{
return element.getLocation();
}
@Override
public Dimension getSize()
{
return element.getSize();
}
@Override
public String getText()
{
return element.getText();
}
@Override
public boolean isDisplayed()
{
return element.isDisplayed();
}
@Override
public boolean isEnabled()
{
return element.isEnabled();
}
@Override
public boolean isSelected()
{
return element.isSelected();
}
@Override
public void sendKeys(CharSequence... arg0)
{
shotter.screenshot();
element.sendKeys(arg0);
}
@Override
public void submit()
{
shotter.screenshot();
element.submit();
}
} }
...@@ -45,7 +45,7 @@ public class Dynamic implements Widget ...@@ -45,7 +45,7 @@ public class Dynamic implements Widget
AtomicWidget w = (AtomicWidget) widget; AtomicWidget w = (AtomicWidget) widget;
if (!w.getTagName().equals(context.getTagName())) if (!w.getTagName().equals(context.getTagName()))
{ {
widget.setContext(new WidgetContext(context.find(".//" + w.getTagName()))); widget.setContext((WidgetContext) context.find(".//" + w.getTagName()));
return widget; return widget;
} }
} }
......
...@@ -59,7 +59,7 @@ public class Form implements Widget ...@@ -59,7 +59,7 @@ public class Form implements Widget
e = e.findElement(By.xpath(".//" + w.getTagName())); e = e.findElement(By.xpath(".//" + w.getTagName()));
} }
w.setContext(new WidgetContext(e)); w.setContext((WidgetContext) e);
return w; return w;
} }
} }
......
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