Test Overview
This test uses a custom annotation to mark a Selenium test method. It verifies that the test navigates to a webpage, finds a button by its ID, clicks it, and checks the resulting page title.
This test uses a custom annotation to mark a Selenium test method. It verifies that the test navigates to a webpage, finds a button by its ID, clicks it, and checks the resulting page title.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import static org.junit.jupiter.api.Assertions.assertEquals; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @interface CustomTest { } @ExtendWith(CustomTestExtension.class) public class CustomAnnotationTest { WebDriver driver; @CustomTest @Test public void testButtonClick() { driver = new ChromeDriver(); driver.get("https://example.com"); WebElement button = driver.findElement(By.id("start-button")); button.click(); String title = driver.getTitle(); assertEquals("Welcome Page", title); driver.quit(); } } // CustomTestExtension would be implemented to process @CustomTest annotations, but is omitted here for brevity.
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and ChromeDriver is initialized | Browser window opens with a blank page | — | PASS |
| 2 | Navigates to https://example.com | Browser displays the example.com homepage with a button having id 'start-button' | — | PASS |
| 3 | Finds the button element by id 'start-button' | Button element is located on the page | — | PASS |
| 4 | Clicks the button | Page navigates or updates to the Welcome Page | — | PASS |
| 5 | Gets the page title | Browser shows the Welcome Page | Check if page title equals 'Welcome Page' | PASS |
| 6 | Quits the browser | Browser window closes | — | PASS |
custom annotations in Selenium Java tests?TestInfo in Java?@interface and define methods without bodies for elements.public @interface TestInfo and defines methods with optional default values.printTestInfo() is called?
import java.lang.reflect.Method;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface TestInfo {
String author();
int priority() default 1;
}
public class SampleTest {
@TestInfo(author = "Alice", priority = 3)
public void testMethod() {}
public static void printTestInfo() throws Exception {
Method m = SampleTest.class.getMethod("testMethod");
TestInfo info = m.getAnnotation(TestInfo.class);
System.out.println(info.author() + ":" + info.priority());
}
}getAnnotation(TestInfo.class) fetches the annotation instance on testMethod.author="Alice" and priority=3, so printing them outputs "Alice:3".@interface TestInfo {
String author();
int priority() default 1;
}
And this usage:
@TestInfo(author = "Bob")
public void test() {}
Why might the following code throw a NullPointerException when accessing priority()?
TestInfo info = testMethod.getAnnotation(TestInfo.class); int p = info.priority();
@Retention(RetentionPolicy.RUNTIME), it is not available at runtime, so getAnnotation returns null.info is null, calling info.priority() throws NullPointerException.@StepInfo to mark test steps with a description and an optional severity level (default "LOW"). You also want to process these annotations at runtime to print all step descriptions with their severity. Which combination of annotation elements and retention policy is correct to achieve this?@Retention(RetentionPolicy.RUNTIME).description() as required and severity() with default "LOW" for optional use.Pass the test and this pattern moves unknown