From dd4b3df39e42e4be56336da56d3260f010975aa6 Mon Sep 17 00:00:00 2001
From: anttil <anttil>
Date: Tue, 2 Oct 2012 07:57:23 +0000
Subject: [PATCH] SWE-2 / SP-314 : Screenshotting fixed

SVN: 26929
---
 .../infra/screenshot/ScreenShotDecorator.java |  51 ---------
 .../infra/screenshot/ScreenShotProxy.java     |  79 -------------
 .../uitest/infra/webdriver/PageProxy.java     |   4 +-
 .../uitest/infra/webdriver/WidgetContext.java | 104 +++++++++++++++++-
 .../cisd/openbis/uitest/widget/Dynamic.java   |   2 +-
 .../cisd/openbis/uitest/widget/Form.java      |   2 +-
 6 files changed, 101 insertions(+), 141 deletions(-)
 delete mode 100644 ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotDecorator.java
 delete mode 100644 ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotProxy.java

diff --git a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotDecorator.java b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotDecorator.java
deleted file mode 100644
index 289af9a6538..00000000000
--- a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotDecorator.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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);
-    }
-
-}
diff --git a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotProxy.java b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotProxy.java
deleted file mode 100644
index 3ee9b143ef5..00000000000
--- a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/screenshot/ScreenShotProxy.java
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * 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;
-    }
-}
diff --git a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/PageProxy.java b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/PageProxy.java
index a2e799dd335..2d23e32abda 100644
--- a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/PageProxy.java
+++ b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/PageProxy.java
@@ -27,7 +27,6 @@ import org.openqa.selenium.By;
 import org.openqa.selenium.StaleElementReferenceException;
 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.suite.SeleniumTest;
 import ch.systemsx.cisd.openbis.uitest.widget.AtomicWidget;
@@ -138,8 +137,7 @@ public class PageProxy
                             }
                         }
 
-                        widget.setContext(new WidgetContext((WebElement) ScreenShotProxy
-                                .newInstance(element, shotter)));
+                        widget.setContext(new WidgetContext(element, shotter));
                         field.set(t, widget);
                     } catch (IllegalArgumentException ex)
                     {
diff --git a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/WidgetContext.java b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/WidgetContext.java
index 50da9ad351a..c7c51b42317 100644
--- a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/WidgetContext.java
+++ b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/infra/webdriver/WidgetContext.java
@@ -16,30 +16,39 @@
 
 package ch.systemsx.cisd.openbis.uitest.infra.webdriver;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.openqa.selenium.By;
+import org.openqa.selenium.Dimension;
+import org.openqa.selenium.Point;
 import org.openqa.selenium.WebElement;
 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.widget.AtomicWidget;
 
 /**
  * @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.shotter = shotter;
     }
 
+    @Override
     public void click()
     {
+        shotter.screenshot();
         element.click();
     }
 
@@ -48,11 +57,13 @@ public class WidgetContext
         return element.isDisplayed() && element.isEnabled();
     }
 
+    @Override
     public String getAttribute(String key)
     {
         return element.getAttribute(key);
     }
 
+    @Override
     public String getTagName()
     {
         return element.getTagName();
@@ -63,14 +74,16 @@ public class WidgetContext
         element.sendKeys(keys);
     }
 
+    @Override
     public void clear()
     {
+        shotter.screenshot();
         element.clear();
     }
 
     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)
@@ -92,19 +105,98 @@ public class WidgetContext
         {
             e = e.findElement(By.xpath(".//" + t.getTagName()));
         }
-        t.setContext(new WidgetContext(e));
+        t.setContext(new WidgetContext(e, shotter));
         return t;
     }
 
     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()
     {
+        shotter.screenshot();
         Actions builder = new Actions(SeleniumTest.driver);
         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();
+    }
+
 }
diff --git a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Dynamic.java b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Dynamic.java
index 4e9146464f2..ee99c96d3f8 100644
--- a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Dynamic.java
+++ b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Dynamic.java
@@ -45,7 +45,7 @@ public class Dynamic implements Widget
             AtomicWidget w = (AtomicWidget) widget;
             if (!w.getTagName().equals(context.getTagName()))
             {
-                widget.setContext(new WidgetContext(context.find(".//" + w.getTagName())));
+                widget.setContext((WidgetContext) context.find(".//" + w.getTagName()));
                 return widget;
             }
         }
diff --git a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Form.java b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Form.java
index 8a656954b75..5ec9aee4f60 100644
--- a/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Form.java
+++ b/ui-test/source/java/ch/systemsx/cisd/openbis/uitest/widget/Form.java
@@ -59,7 +59,7 @@ public class Form implements Widget
                     e = e.findElement(By.xpath(".//" + w.getTagName()));
                 }
 
-                w.setContext(new WidgetContext(e));
+                w.setContext((WidgetContext) e);
                 return w;
             }
         }
-- 
GitLab