1+ require ( 'dotenv' ) . config ( ) ;
2+ const { webkit } = require ( "playwright" ) ;
3+ const { expect } = require ( "expect" ) ;
4+
5+ ( async ( ) => {
6+ console . log ( '🍎 Starting Playwright iOS Real Device test...' ) ;
7+ console . log ( '📱 Device: iPhone 16 (iOS 18)' ) ;
8+
9+ const capabilities = {
10+ "LT:Options" : {
11+ "platformName" : "ios" ,
12+ "deviceName" : "iPhone 16" ,
13+ "platformVersion" : "18" ,
14+ "isRealMobile" : true ,
15+ "build" : "Playwright iOS Build" ,
16+ "name" : "Playwright iOS test" ,
17+ "user" : process . env . LT_USERNAME ,
18+ "accessKey" : process . env . LT_ACCESS_KEY ,
19+ "network" : true ,
20+ "video" : true ,
21+ "console" : true ,
22+ "projectName" : "New UI" ,
23+ } ,
24+ } ;
25+
26+ console . log ( '🔗 Connecting to LambdaTest iOS cloud...' ) ;
27+ console . log ( '👤 Username:' , process . env . LT_USERNAME ) ;
28+ console . log ( '📱 Platform: iOS 18, Device: iPhone 16' ) ;
29+
30+ let browser = await webkit . connect (
31+ `wss://cdp.lambdatest.com/playwright?capabilities=${ encodeURIComponent (
32+ JSON . stringify ( capabilities ) ) } `,
33+ ) ;
34+
35+ console . log ( '✅ Connected to iOS device successfully!' ) ;
36+ console . log ( `📱 Device: iPhone 16, iOS 18` ) ;
37+
38+ console . log ( '🚀 Creating browser context...' ) ;
39+ let context = await browser . newContext ( {
40+ hasTouch : true , // Enable touch support for iOS
41+ isMobile : true // Enable mobile mode for iOS
42+ } ) ;
43+ let page = await context . newPage ( ) ;
44+
45+ console . log ( '🔍 Navigating to Wikipedia...' ) ;
46+ await page . goto ( 'https://www.wikipedia.org/' , { timeout : 30000 } ) ;
47+
48+ console . log ( '🔍 Finding search input...' ) ;
49+ let element = await page . locator ( 'input[name="search"]' ) ;
50+
51+ console . log ( '👆 Clicking search input...' ) ;
52+ await element . click ( ) ;
53+
54+ console . log ( '⌨️ Typing "playwright"...' ) ;
55+ await element . fill ( 'playwright' ) ;
56+
57+ console . log ( '👆 Clicking search input again...' ) ;
58+ await element . click ( ) ;
59+
60+ console . log ( '🔗 Current URL:' , await page . url ( ) ) ;
61+
62+ console . log ( '🔍 Finding and clicking search button...' ) ;
63+ await page . locator ( '#search-form > fieldset > button' ) . click ( ) ;
64+
65+ console . log ( '⏳ Waiting for search results...' ) ;
66+ await page . waitForTimeout ( 3000 ) ;
67+
68+ console . log ( '🔍 Counting occurrences of "19th century"...' ) ;
69+ const count = await page . getByText ( '19th century' ) . count ( ) ;
70+ console . log ( '📊 Found' , count , 'occurrences of "19th century"' ) ;
71+
72+ try {
73+ console . log ( '🧪 Verifying count...' ) ;
74+ expect ( count ) . toEqual ( 3 ) ;
75+ console . log ( '✅ iOS Test PASSED!' ) ;
76+
77+ // Mark the test as completed or failed
78+ await page . evaluate ( _ => { } , `lambdatest_action: ${ JSON . stringify ( { action : "setTestStatus" , arguments : { status : "passed" , remark : "Wikipedia test passed - found 3 occurrences of '19th century'" } , } ) } ` ) ;
79+ console . log ( '📊 Marked test as PASSED in LambdaTest dashboard' ) ;
80+
81+ await teardown ( page , context , browser )
82+ } catch ( e ) {
83+ console . log ( '❌ iOS Test FAILED!' ) ;
84+ console . log ( '💥 Error:' , e . message ) ;
85+ console . log ( '📊 Expected: 3 occurrences of "19th century"' ) ;
86+ console . log ( '📊 Actual count:' , count ) ;
87+
88+ await page . evaluate ( _ => { } , `lambdatest_action: ${ JSON . stringify ( { action : "setTestStatus" , arguments : { status : "failed" , remark : e . message } } ) } ` ) ;
89+ console . log ( '📊 Marked test as FAILED in LambdaTest dashboard' ) ;
90+
91+ await teardown ( page , context , browser )
92+ throw e . stack
93+ }
94+
95+ } ) ( ) . catch ( err => {
96+ console . error ( '💥 Unexpected error occurred in iOS test:' ) ;
97+ console . error ( err ) ;
98+ process . exit ( 1 ) ;
99+ } ) ;
100+
101+ async function teardown ( page , context , browser ) {
102+ console . log ( '🧹 Cleaning up iOS test resources...' ) ;
103+ try {
104+ console . log ( ' Closing page...' ) ;
105+ await Promise . race ( [
106+ page . close ( ) ,
107+ new Promise ( resolve => setTimeout ( resolve , 10000 ) ) // 10 second timeout
108+ ] ) ;
109+ console . log ( ' ✅ Page closed' ) ;
110+
111+ console . log ( ' Closing context...' ) ;
112+ await Promise . race ( [
113+ context . close ( ) ,
114+ new Promise ( resolve => setTimeout ( resolve , 10000 ) ) // 10 second timeout
115+ ] ) ;
116+ console . log ( ' ✅ Context closed' ) ;
117+
118+ console . log ( ' Closing browser connection...' ) ;
119+ await Promise . race ( [
120+ browser . close ( ) ,
121+ new Promise ( resolve => setTimeout ( resolve , 15000 ) ) // 15 second timeout for browser
122+ ] ) ;
123+ console . log ( ' ✅ Browser closed' ) ;
124+
125+ console . log ( '✅ iOS test completed and resources cleaned up!' ) ;
126+ } catch ( error ) {
127+ console . log ( '⚠️ Cleanup completed with warnings:' , error . message ) ;
128+ }
129+ process . exit ( 0 ) ;
130+ }
0 commit comments