Class AnnotationsReader

java.lang.Object
co.verisoft.fw.selenium.drivers.factory.AnnotationsReader

public class AnnotationsReader extends Object
This class is copied from Boni Garcia code, Selenium-Jupiter.
Original code can be found here.
This class reads the following annotations from the test- @DriverUrl and @DriverCapabilities

Exmample

 public class DriverTest {
     <code>@DriverUrl</code>
     private URL url = new URL("http://localhost:4723/");

     <code>@DriverCapabilities</code>
     DesiredCapabilities capabilities = new DesiredCapabilities();
     {
         capabilities.setCapability("platformName", "android");
         capabilities.setCapability("deviceName", "emulator-5554");
         capabilities.setCapability("appPackage", "com.android.chrome");
         capabilities.setCapability("appActivity", "com.google.android.apps.chrome.Main");
         capabilities.setCapability("platformVersion", "11");
         capabilities.setCapability("automationName", "uiAutomator2");
     }
 }
 


  • Constructor Details

    • AnnotationsReader

      public AnnotationsReader()
  • Method Details

    • getCapabilities

      public Optional<org.openqa.selenium.Capabilities> getCapabilities(org.springframework.context.ApplicationContext context, Parameter parameter, Optional<Object> testInstance)
      Retrieves the capabilities either from the method parameter's annotation, a field in the test instance, or from a Spring bean. The method first checks for the presence of the DriverCapabilities annotation on the method parameter. If found, it attempts to retrieve the capabilities either from a JSON file or from a Spring bean. If the annotation is not present, it checks if any field in the test instance is annotated with DriverCapabilities.
      Parameters:
      context - The Spring ApplicationContext used to retrieve the capabilities bean if necessary.
      parameter - The method parameter, which may be annotated with DriverCapabilities.
      testInstance - The test instance, which may contain fields annotated with DriverCapabilities.
      Returns:
      An Optional containing the corresponding Capabilities if found, otherwise Optional.empty().
    • getUrl

      public Optional<URL> getUrl(org.springframework.context.ApplicationContext applicationContext, Parameter parameter, Optional<Object> testInstance)
      Retrieves the URL either from a method parameter's annotation, a field in the test instance, or from a Spring bean. The method first checks for the presence of the DriverUrl annotation on the method parameter. If found, it attempts to retrieve the URL value from the annotation. If not found in the parameter, it checks if any field in the test instance is annotated with DriverUrl.
      Parameters:
      applicationContext - The Spring ApplicationContext to retrieve the URL bean if needed.
      parameter - The method parameter, which may be annotated with DriverUrl.
      testInstance - The optional test instance, which may contain fields annotated with DriverUrl.
      Returns:
      An Optional containing the corresponding URL if found, otherwise Optional.empty().
    • getCustomHeaders

      public Optional<Map<String,String>> getCustomHeaders(org.springframework.context.ApplicationContext context, Parameter parameter, Optional<Object> testInstance)
      Retrieves custom HTTP headers from various sources, including method parameter annotations, test instance fields, or a Spring bean. The method prioritizes the following sources:
      1. The @CustomHeaders annotation on the provided method parameter.
      2. A field in the test instance annotated with @CustomHeaders.
      3. A Spring bean named getCustomHeaders from the application context.
      Parameters:
      context - The Spring ApplicationContext used to retrieve the getCustomHeaders bean if needed.
      parameter - The method parameter, which may be annotated with @CustomHeaders.
      testInstance - The optional test instance, which may contain fields annotated with @CustomHeaders.
      Returns:
      An Optional containing a Map of header key-value pairs, if any are found. If no headers are found in any of the sources, Optional.empty() is returned.
    • getCommandExecutor

      public Optional<Object> getCommandExecutor(org.springframework.context.ApplicationContext applicationContext, Parameter parameter, Optional<Object> testInstance)
      Retrieves the command executor based on the given parameter and context. Looks for a DriverCommandExecutor annotation on the parameter or in a field in the test instance. If found, it either creates or retrieves the corresponding executor.
      Parameters:
      applicationContext - the Spring context to retrieve beans
      parameter - the parameter annotated with DriverCommandExecutor
      testInstance - the test instance containing potential annotated fields
      Returns:
      an Optional with the executor or empty if not found or an error occurs
    • isBoolean

      public boolean isBoolean(String s)
    • isNumeric

      public boolean isNumeric(String s)
    • getFromAnnotatedField

      public <T> T getFromAnnotatedField(Optional<Object> testInstance, Class<? extends Annotation> annotationClass, Class<T> capabilitiesClass)
      Retrieves an object from a field annotated with a specific annotation.
      Parameters:
      testInstance - The optional test instance containing fields.
      annotationClass - The annotation class to search for.
      capabilitiesClass - The type of capabilities to cast the field value to.
      Returns:
      The found field's value cast to the specified type, or null if not found.
      Throws:
      RuntimeException - if capabilitiesClass is null.
    • seekFieldAnnotatedWith

      public Optional<Object> seekFieldAnnotatedWith(Optional<Object> testInstance, Class<? extends Annotation> annotation)
    • getKeyValue

      public Optional<List<Object>> getKeyValue(String keyValue)