@@ -2,22 +2,29 @@ import { Client } from "@modelcontextprotocol/sdk/client/index.js";
22import { describe , it , expect , beforeEach , afterEach } from "vitest" ;
33import { startClient } from "../utils" ;
44import { INTEGRATION_TEST_TIMEOUT } from "../constants" ;
5+ import { launchBrowser } from "testplane/unstable" ;
56
6- const sessionMinimalMock = {
7- sessionId : "ffe4129f99be1125e304242500121efa" ,
8- sessionCaps : {
9- browserName : "chrome" ,
10- browserVersion : "137.0.7151.119" ,
11- setWindowRect : true ,
7+ export const BROWSER_NAME = ( process . env . BROWSER || "chrome" ) . toLowerCase ( ) as string ;
8+
9+ export const BROWSER_CONFIG = {
10+ desiredCapabilities : {
11+ browserName : BROWSER_NAME ,
1212 } ,
13- sessionOpts : {
14- protocol : "http" ,
15- hostname : "127.0.0.1" ,
16- port : 49426 ,
17- path : "/" ,
18- strictSSL : true ,
13+ headless : true ,
14+ system : {
15+ debug : Boolean ( process . env . DEBUG ) || false ,
1916 } ,
2017} ;
18+
19+ const checkProcessExists = ( pid : number ) : boolean => {
20+ try {
21+ process . kill ( pid , 0 ) ;
22+ return true ;
23+ } catch {
24+ return false ;
25+ }
26+ } ;
27+
2128describe . only (
2229 "tools/attachToBrowser" ,
2330 ( ) => {
@@ -45,11 +52,55 @@ describe.only(
4552 } ) ;
4653
4754 describe ( "attachToBrowser tool execution" , ( ) => {
55+ it ( "error if connect to unexisting session" , async ( ) => {
56+ const result = await client . callTool ( {
57+ name : "attachToBrowser" ,
58+ arguments : {
59+ session : {
60+ sessionId : "ffe4129f99be1125e304242500121efa" ,
61+ sessionCaps : {
62+ browserName : "chrome" ,
63+ browserVersion : "137.0.7151.119" ,
64+ setWindowRect : true ,
65+ } ,
66+ sessionOpts : {
67+ protocol : "http" ,
68+ hostname : "127.0.0.1" ,
69+ port : 49426 ,
70+ path : "/" ,
71+ strictSSL : true ,
72+ } ,
73+ } ,
74+ } ,
75+ } ) ;
76+
77+ expect ( result . isError ) . toBe ( true ) ;
78+ expect ( result . content ) . toBeDefined ( ) ;
79+
80+ const content = result . content as Array < { type : string ; text : string } > ;
81+
82+ expect ( content ) . toHaveLength ( 1 ) ;
83+ expect ( content [ 0 ] . type ) . toBe ( "text" ) ;
84+ expect ( content [ 0 ] . text ) . toBe ( "❌ Can not attach to browser using existing session options" ) ;
85+ } ) ;
86+
4887 it ( "should attach to existing browser session" , async ( ) => {
88+ const browser : WebdriverIO . Browser & { getDriverPid ?: ( ) => number | undefined } =
89+ await launchBrowser ( BROWSER_CONFIG ) ;
90+ const driverPid = ( await browser . getDriverPid ! ( ) ) as number ;
91+
4992 const result = await client . callTool ( {
5093 name : "attachToBrowser" ,
5194 arguments : {
52- session : sessionMinimalMock ,
95+ session : {
96+ sessionId : browser . sessionId ,
97+ sessionCaps : browser . capabilities ,
98+ sessionOpts : {
99+ capabilities : browser . capabilities ,
100+ ...browser . options ,
101+ } ,
102+ driverPid,
103+ } ,
53104 } ,
54105 } ) ;
55106
@@ -60,6 +111,20 @@ describe.only(
60111 expect ( content ) . toHaveLength ( 1 ) ;
61112 expect ( content [ 0 ] . type ) . toBe ( "text" ) ;
62113 expect ( content [ 0 ] . text ) . toBe ( "Successfully attached to existing browser session" ) ;
114+
115+ // Check that browser process exist
116+ expect ( checkProcessExists ( driverPid ) ) . toBe ( true ) ;
117+
118+ // Call closeBrowser tool
119+ await client . callTool ( {
120+ name : "closeBrowser" ,
121+ arguments : { } ,
122+ } ) ;
123+
124+ await browser . pause ( 100 ) ;
125+
126+ // Check that browser process doesn't exist
127+ expect ( checkProcessExists ( driverPid ) ) . toBe ( false ) ;
63128 } ) ;
64129 } ) ;
65130 } ,
0 commit comments