Advertisement
Guest User

Wrap.java

a guest
Jan 20th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. import com.epam.healenium.appium.AppiumEngine;
  2. import com.typesafe.config.Config;
  3. import com.typesafe.config.ConfigFactory;
  4. import io.appium.java_client.AppiumDriver;
  5. import java.net.URL;
  6. import javassist.util.proxy.ProxyFactory;
  7. import lombok.extern.slf4j.Slf4j;
  8.  
  9.  
  10. @Slf4j
  11. public  final  class  DriverWrapper {
  12.  
  13.     /**
  14.      * Instantiates the self-healing driver.
  15.      *
  16.      * @param delegate the original driver.
  17.      */
  18.     public static AppiumDriver wrap(AppiumDriver delegate) {
  19.         Config config = ConfigFactory.systemProperties().withFallback(ConfigFactory.load());
  20.         AppiumEngine engine = new AppiumEngine(delegate, config);
  21.         return create(engine);
  22.     }
  23.  
  24.     static AppiumDriver create(AppiumEngine engine){
  25.         try{
  26.             AppiumDriver origin = engine.getWebDriver();
  27.             ProxyFactory factory = new ProxyFactory();
  28.             factory.setSuperclass(origin.getClass());
  29.             factory.setFilter(
  30.                 method -> {
  31.                     String methodName = method.getName();
  32.                     return methodName.startsWith("findElement") || methodName.equalsIgnoreCase("switchTo");
  33.                 }
  34.             );
  35.             return (AppiumDriver)factory.create(
  36.                 new Class<?>[]{URL.class, Capabilities.class},
  37.                 new Object[]{origin.getRemoteAddress(), origin.getCapabilities()},
  38.                 new ProxyMethodHandler(engine)
  39.             );
  40.         } catch (Exception ex){
  41.             log.error("Failed to create wrapper!", ex);
  42.             return engine.getWebDriver();
  43.         }
  44.     }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement