0
0
Selenium Javatesting~7 mins

Custom annotations in Selenium Java

Choose your learning style9 modes available
Introduction

Custom annotations help you add extra information to your test code in a clear and reusable way.

You want to mark tests with special tags like @Smoke or @Regression to group them.
You need to add metadata to tests, like author name or test priority.
You want to create your own rules that can be checked before or after tests run.
You want to reduce repeated code by using annotations to configure tests.
Syntax
Selenium Java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface YourAnnotation {
    String value() default "";
    int priority() default 1;
}

Use @Retention(RetentionPolicy.RUNTIME) to keep the annotation available during test execution.

Define annotation elements like methods with default values for flexibility.

Examples
This annotation stores author and category info for tests.
Selenium Java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface TestInfo {
    String author();
    String category() default "General";
}
Use the custom annotation on a test method to add metadata.
Selenium Java
@TestInfo(author = "Alice", category = "Smoke")
public void testLogin() {
    // test code
}
Read annotation values at runtime using reflection.
Selenium Java
import java.lang.reflect.Method;

Method method = MyTestClass.class.getMethod("testLogin");
TestInfo info = method.getAnnotation(TestInfo.class);
System.out.println("Author: " + info.author());
Sample Program

This program defines a custom annotation @TestInfo with author and category. It applies it to a test method and reads the annotation values using reflection before running the test.

Selenium Java
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

@Retention(RetentionPolicy.RUNTIME)
@interface TestInfo {
    String author();
    String category() default "General";
}

public class CustomAnnotationExample {

    @TestInfo(author = "Bob", category = "Regression")
    public void testSearch() {
        System.out.println("Running testSearch");
    }

    public static void main(String[] args) throws Exception {
        Method method = CustomAnnotationExample.class.getMethod("testSearch");
        TestInfo info = method.getAnnotation(TestInfo.class);
        System.out.println("Author: " + info.author());
        System.out.println("Category: " + info.category());

        CustomAnnotationExample example = new CustomAnnotationExample();
        example.testSearch();
    }
}
OutputSuccess
Important Notes

Custom annotations must be retained at runtime to be accessible during test execution.

Use reflection carefully; it can slow down tests if overused.

Annotations help keep test code clean and organized by separating metadata from logic.

Summary

Custom annotations add reusable metadata to your tests.

They are defined with @interface and retained at runtime.

You can read annotation data using Java reflection to control test behavior.