Skip to content

Latest commit

 

History

History
138 lines (102 loc) · 3.57 KB

java.mdx

File metadata and controls

138 lines (102 loc) · 3.57 KB
title Java SDK
description Test email flows in JUnit, Selenium and Playwright for Java — OTPs and magic links auto-extracted.

Install

Gradle:

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    testImplementation 'com.github.zerodrop-dev:zerodrop-java:v0.2.0'
}

Maven:

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependency>
    <groupId>com.github.zerodrop-dev</groupId>
    <artifactId>zerodrop-java</artifactId>
    <version>v0.2.0</version>
    <scope>test</scope>
</dependency>

Java 11+ · one dependency (Gson).

Quick start

import dev.zerodrop.ZeroDrop;
import dev.zerodrop.Email;
import java.time.Duration;

ZeroDrop mail = ZeroDrop.create();
String inbox = mail.generateInbox();
// => "swift-x7k29ab@zerodrop-sandbox.online"

// Trigger your app's email flow with the inbox address...

Email email = mail.waitForLatest(inbox, Duration.ofSeconds(15));

email.otp();       // Optional["123456"] — auto-extracted, no regex
email.magicLink(); // Optional["https://..."] — auto-extracted

JUnit 5 + Selenium

@Test
void emailVerificationWithOtp() {
    ZeroDrop mail = ZeroDrop.create();
    String inbox = mail.generateInbox();

    driver.get("https://yourapp.com/signup");
    driver.findElement(By.name("email")).sendKeys(inbox);
    driver.findElement(By.name("password")).sendKeys("TestPassword123!");
    driver.findElement(By.cssSelector("button[type=submit]")).click();

    Email email = mail.waitForLatest(inbox, Duration.ofSeconds(15));

    String otp = email.otp().orElseThrow();
    driver.findElement(By.name("otp")).sendKeys(otp);
    driver.findElement(By.cssSelector("button[type=submit]")).click();

    assertTrue(driver.getCurrentUrl().endsWith("/dashboard"));
}

Filtering

import dev.zerodrop.Filter;

Filter filter = Filter.builder()
    .from("noreply@yourapp.com")
    .hasOtp(true)
    .build();

Email email = mail.waitForLatest(inbox, Duration.ofSeconds(15), filter);

All string filters are case-insensitive partial matches.

Parallel tests

generateInbox() runs locally — safe with JUnit parallel execution and TestNG parallel suites. Every call returns a unique isolated inbox.

Free API key (private inboxes)

Without a key, anyone who knows an inbox's name can read it. Sign in with GitHub at zerodrop.dev/keys for a free key and set it as ZERODROP_API_KEY. With v0.2.0 or later, the client reads it, generateInbox() starts names with your key's prefix, and only your key can read those inboxes:

ZeroDrop mail = ZeroDrop.create(); // reads ZERODROP_API_KEY
mail.generateInbox();
// "k7x2-swift-x7k29ab@zerodrop-sandbox.online", where k7x2 is your prefix
mail.prefix(); // Optional[k7x2]

Error handling

try {
    Email email = mail.waitForLatest(inbox, Duration.ofSeconds(15));
} catch (ZeroDropException.Timeout e) {
    // No email arrived
} catch (ZeroDropException.Auth e) {
    // The inbox belongs to another key's prefix, or your key is wrong; e.getMessage() says which
} catch (ZeroDropException.Network e) {
    // Transport failure
}

Workspaces & self-hosting

ZeroDrop mail = ZeroDrop.builder()
    .apiKey(System.getenv("ZERODROP_API_KEY")) // optional since v0.2.0: the client reads it anyway
    .baseUrl("https://your-instance.yourdomain.com")
    .build();

Links