Spaces:
Sleeping
Sleeping
File size: 12,579 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 | package com.firecrawl;
import com.firecrawl.client.FirecrawlClient;
import com.firecrawl.models.BrowserCreateResponse;
import com.firecrawl.models.BrowserDeleteResponse;
import com.firecrawl.models.BrowserExecuteResponse;
import com.firecrawl.models.BrowserListResponse;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import static org.junit.jupiter.api.Assertions.*;
/**
* Browser Sandbox Endpoint Tests
*
* Tests the browser session management functionality of the Firecrawl Java SDK.
* These tests require FIRECRAWL_API_KEY environment variable to be set.
*
* Run with: FIRECRAWL_API_KEY=fc-xxx gradle test --tests "com.firecrawl.BrowserTest"
*/
class BrowserTest {
private static FirecrawlClient client;
@BeforeAll
static void setup() {
String apiKey = System.getenv("FIRECRAWL_API_KEY");
if (apiKey != null && !apiKey.isBlank()) {
client = FirecrawlClient.fromEnv();
}
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserCreateAndDelete() {
System.out.println("Testing browser session create and delete...");
// Create a browser session
BrowserCreateResponse createRes = client.browser();
assertNotNull(createRes, "Create response should not be null");
assertTrue(createRes.isSuccess(), "Create should succeed");
assertNotNull(createRes.getId(), "Session ID should not be null");
String sessionId = createRes.getId();
System.out.println(" Created session: " + sessionId);
// Delete the browser session
BrowserDeleteResponse deleteRes = client.deleteBrowser(sessionId);
assertNotNull(deleteRes, "Delete response should not be null");
assertTrue(deleteRes.isSuccess(), "Delete should succeed");
System.out.println("β Browser create and delete test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserCreateWithOptions() {
System.out.println("Testing browser session create with options...");
// Create a session with custom TTL and activity TTL
BrowserCreateResponse createRes = client.browser(300, 120, true);
assertNotNull(createRes, "Create response should not be null");
assertTrue(createRes.isSuccess(), "Create should succeed");
assertNotNull(createRes.getId(), "Session ID should not be null");
String sessionId = createRes.getId();
System.out.println(" Created session with options: " + sessionId);
// Clean up
client.deleteBrowser(sessionId);
System.out.println("β Browser create with options test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserExecuteBash() {
System.out.println("Testing browser execute with bash...");
// Create a session
BrowserCreateResponse createRes = client.browser();
assertTrue(createRes.isSuccess(), "Create should succeed");
String sessionId = createRes.getId();
try {
// Execute bash code
BrowserExecuteResponse execRes = client.browserExecute(sessionId, "echo 'hello from java sdk'");
assertNotNull(execRes, "Execute response should not be null");
assertTrue(execRes.isSuccess(), "Execute should succeed");
assertNotNull(execRes.getStdout(), "Stdout should not be null");
assertTrue(execRes.getStdout().contains("hello from java sdk"),
"Stdout should contain our echo output");
System.out.println(" Stdout: " + execRes.getStdout().trim());
System.out.println(" Exit code: " + execRes.getExitCode());
} finally {
// Clean up
client.deleteBrowser(sessionId);
}
System.out.println("β Browser execute bash test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserExecuteNode() {
System.out.println("Testing browser execute with node...");
// Create a session
BrowserCreateResponse createRes = client.browser();
assertTrue(createRes.isSuccess(), "Create should succeed");
String sessionId = createRes.getId();
try {
// Execute node code
BrowserExecuteResponse execRes = client.browserExecute(
sessionId, "console.log(1 + 2)", "node", null);
assertNotNull(execRes, "Execute response should not be null");
assertTrue(execRes.isSuccess(), "Execute should succeed");
System.out.println(" Stdout: " + (execRes.getStdout() != null ? execRes.getStdout().trim() : "null"));
} finally {
client.deleteBrowser(sessionId);
}
System.out.println("β Browser execute node test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserExecutePython() {
System.out.println("Testing browser execute with python...");
// Create a session
BrowserCreateResponse createRes = client.browser();
assertTrue(createRes.isSuccess(), "Create should succeed");
String sessionId = createRes.getId();
try {
// Execute python code
BrowserExecuteResponse execRes = client.browserExecute(
sessionId, "print('hello from python')", "python", null);
assertNotNull(execRes, "Execute response should not be null");
assertTrue(execRes.isSuccess(), "Execute should succeed");
System.out.println(" Stdout: " + (execRes.getStdout() != null ? execRes.getStdout().trim() : "null"));
} finally {
client.deleteBrowser(sessionId);
}
System.out.println("β Browser execute python test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserExecuteWithTimeout() {
System.out.println("Testing browser execute with custom timeout...");
// Create a session
BrowserCreateResponse createRes = client.browser();
assertTrue(createRes.isSuccess(), "Create should succeed");
String sessionId = createRes.getId();
try {
// Execute with custom timeout (60 seconds)
BrowserExecuteResponse execRes = client.browserExecute(
sessionId, "echo 'timeout test'", "bash", 60);
assertNotNull(execRes, "Execute response should not be null");
assertTrue(execRes.isSuccess(), "Execute should succeed");
System.out.println(" Stdout: " + (execRes.getStdout() != null ? execRes.getStdout().trim() : "null"));
} finally {
client.deleteBrowser(sessionId);
}
System.out.println("β Browser execute with timeout test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserListSessions() {
System.out.println("Testing list browser sessions...");
// List all sessions
BrowserListResponse listRes = client.listBrowsers();
assertNotNull(listRes, "List response should not be null");
assertTrue(listRes.isSuccess(), "List should succeed");
System.out.println(" Total sessions: " + (listRes.getSessions() != null ? listRes.getSessions().size() : 0));
System.out.println("β List browser sessions test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserListActiveFilter() {
System.out.println("Testing list browser sessions with active filter...");
// Create a session so we have at least one active
BrowserCreateResponse createRes = client.browser();
assertTrue(createRes.isSuccess(), "Create should succeed");
String sessionId = createRes.getId();
try {
// List only active sessions
BrowserListResponse listRes = client.listBrowsers("active");
assertNotNull(listRes, "List response should not be null");
assertTrue(listRes.isSuccess(), "List should succeed");
assertNotNull(listRes.getSessions(), "Sessions list should not be null");
assertFalse(listRes.getSessions().isEmpty(), "Should have at least one active session");
System.out.println(" Active sessions: " + listRes.getSessions().size());
} finally {
client.deleteBrowser(sessionId);
}
System.out.println("β List active browser sessions test passed");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testBrowserFullLifecycle() {
System.out.println("Testing full browser session lifecycle...");
// 1. Create session
BrowserCreateResponse createRes = client.browser(300, 120, true);
assertTrue(createRes.isSuccess(), "Create should succeed");
assertNotNull(createRes.getId(), "Should have session ID");
String sessionId = createRes.getId();
System.out.println(" 1. Created session: " + sessionId);
// CDP URL and live view URL may be present
if (createRes.getCdpUrl() != null) {
System.out.println(" CDP URL present: true");
}
if (createRes.getLiveViewUrl() != null) {
System.out.println(" Live View URL present: true");
}
// 2. Navigate to a page
BrowserExecuteResponse navRes = client.browserExecute(
sessionId, "agent-browser open https://example.com", "bash", 30);
assertTrue(navRes.isSuccess(), "Navigation should succeed");
System.out.println(" 2. Navigated to example.com");
// 3. Take a snapshot
BrowserExecuteResponse snapRes = client.browserExecute(
sessionId, "agent-browser snapshot -i -c", "bash", 30);
assertTrue(snapRes.isSuccess(), "Snapshot should succeed");
System.out.println(" 3. Took snapshot");
// 4. Get page title
BrowserExecuteResponse titleRes = client.browserExecute(
sessionId, "agent-browser get title", "bash", 30);
assertTrue(titleRes.isSuccess(), "Get title should succeed");
System.out.println(" 4. Page title: " + (titleRes.getStdout() != null ? titleRes.getStdout().trim() : "null"));
// 5. Verify session is active
BrowserListResponse listRes = client.listBrowsers("active");
assertTrue(listRes.isSuccess(), "List should succeed");
System.out.println(" 5. Active sessions: " + (listRes.getSessions() != null ? listRes.getSessions().size() : 0));
// 6. Delete session
BrowserDeleteResponse deleteRes = client.deleteBrowser(sessionId);
assertTrue(deleteRes.isSuccess(), "Delete should succeed");
System.out.println(" 6. Deleted session");
if (deleteRes.getSessionDurationMs() != null) {
System.out.println(" Session duration: " + deleteRes.getSessionDurationMs() + "ms");
}
if (deleteRes.getCreditsBilled() != null) {
System.out.println(" Credits billed: " + deleteRes.getCreditsBilled());
}
System.out.println("β Full browser session lifecycle test passed");
}
@Test
void testBrowserExecuteRequiresSessionId() {
FirecrawlClient testClient = FirecrawlClient.builder()
.apiKey("fc-test-key")
.build();
assertThrows(NullPointerException.class, () ->
testClient.browserExecute(null, "echo test")
);
}
@Test
void testBrowserExecuteRequiresCode() {
FirecrawlClient testClient = FirecrawlClient.builder()
.apiKey("fc-test-key")
.build();
assertThrows(NullPointerException.class, () ->
testClient.browserExecute("some-session-id", null)
);
}
@Test
void testBrowserDeleteRequiresSessionId() {
FirecrawlClient testClient = FirecrawlClient.builder()
.apiKey("fc-test-key")
.build();
assertThrows(NullPointerException.class, () ->
testClient.deleteBrowser(null)
);
}
}
|