Class AnnotationsReader
java.lang.Object
co.verisoft.fw.selenium.drivers.factory.AnnotationsReader
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
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionOptional<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.getCommandExecutor
(org.springframework.context.ApplicationContext applicationContext, Parameter parameter, Optional<Object> testInstance) Retrieves the command executor based on the given parameter and context.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.<T> T
getFromAnnotatedField
(Optional<Object> testInstance, Class<? extends Annotation> annotationClass, Class<T> capabilitiesClass) Retrieves an object from a field annotated with a specific annotation.getKeyValue
(String keyValue) 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.boolean
boolean
seekFieldAnnotatedWith
(Optional<Object> testInstance, Class<? extends Annotation> annotation)
-
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 theDriverCapabilities
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 withDriverCapabilities
.- Parameters:
context
- The SpringApplicationContext
used to retrieve the capabilities bean if necessary.parameter
- The method parameter, which may be annotated withDriverCapabilities
.testInstance
- The test instance, which may contain fields annotated withDriverCapabilities
.- Returns:
- An
Optional
containing the correspondingCapabilities
if found, otherwiseOptional.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 theDriverUrl
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 withDriverUrl
.- Parameters:
applicationContext
- The SpringApplicationContext
to retrieve the URL bean if needed.parameter
- The method parameter, which may be annotated withDriverUrl
.testInstance
- The optional test instance, which may contain fields annotated withDriverUrl
.- Returns:
- An
Optional
containing the correspondingURL
if found, otherwiseOptional.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:- The
@CustomHeaders
annotation on the provided method parameter. - A field in the test instance annotated with
@CustomHeaders
. - A Spring bean named
getCustomHeaders
from the application context.
- Parameters:
context
- The SpringApplicationContext
used to retrieve thegetCustomHeaders
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 aMap
of header key-value pairs, if any are found. If no headers are found in any of the sources,Optional.empty()
is returned.
- The
-
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 aDriverCommandExecutor
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 beansparameter
- the parameter annotated withDriverCommandExecutor
testInstance
- the test instance containing potential annotated fields- Returns:
- an
Optional
with the executor or empty if not found or an error occurs
-
isBoolean
-
isNumeric
-
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
- ifcapabilitiesClass
isnull
.
-
seekFieldAnnotatedWith
-
getKeyValue
-