@@ -2,19 +2,15 @@ package devices
22
33import (
44 "bytes"
5- "context"
65 "encoding/json"
76 "fmt"
8- "io"
97 "log"
10- "net/http"
118 "os"
129 "os/exec"
1310 "time"
1411
1512 "github.com/mobile-next/mobilecli/devices/wda"
1613 "github.com/mobile-next/mobilecli/utils"
17- "github.com/otiai10/copy"
1814)
1915
2016// AppInfo corresponds to the structure from plutil output
@@ -228,76 +224,33 @@ func (s SimulatorDevice) WaitUntilAppExists(bundleID string) error {
228224}
229225
230226func (s SimulatorDevice ) DownloadWebDriverAgent () (string , error ) {
231- url := "https://github.com/appium/WebDriverAgent/releases/download/v9.13.0 /WebDriverAgentRunner-Runner .zip"
227+ url := "https://github.com/appium/WebDriverAgent/releases/download/v9.15.1 /WebDriverAgentRunner-Build-Sim-arm64 .zip"
232228
233229 tmpFile , err := os .CreateTemp ("" , "wda-*.zip" )
234- log .Printf ("Created temp file: %s" , tmpFile .Name ())
235230 if err != nil {
236231 return "" , fmt .Errorf ("failed to create temp file: %v" , err )
237232 }
233+ tmpFile .Close ()
238234
239- client := & http.Client {
240- Timeout : 5 * time .Second ,
241- }
242-
243- ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
244- defer cancel ()
245-
246- req , err := http .NewRequestWithContext (ctx , "GET" , url , nil )
247- if err != nil {
248- return "" , fmt .Errorf ("failed to download WebDriverAgent: %v" , err )
249- }
235+ log .Printf ("Downloading WebDriverAgent to: %s" , tmpFile .Name ())
250236
251- resp , err := client . Do ( req )
237+ err = utils . DownloadFile ( url , tmpFile . Name () )
252238 if err != nil {
239+ os .Remove (tmpFile .Name ())
253240 return "" , fmt .Errorf ("failed to download WebDriverAgent: %v" , err )
254241 }
255242
256- defer resp .Body .Close ()
257-
258- if resp .StatusCode != http .StatusOK {
259- return "" , fmt .Errorf ("failed to download WebDriverAgent: status code %d" , resp .StatusCode )
260- }
261-
262- _ , err = io .Copy (tmpFile , resp .Body )
263- if err != nil {
264- return "" , err
265- }
266-
267- defer tmpFile .Close ()
268-
269243 return tmpFile .Name (), nil
270244}
271245
272- func copyFile (src , dst string ) error {
273- srcFile , err := os .Open (src )
274- if err != nil {
275- return fmt .Errorf ("failed to open source file %s: %v" , src , err )
276- }
277- defer srcFile .Close ()
278-
279- dstFile , err := os .Create (dst )
280- if err != nil {
281- return fmt .Errorf ("failed to create destination file %s: %v" , dst , err )
282- }
283- defer dstFile .Close ()
284-
285- _ , err = io .Copy (dstFile , srcFile )
286- if err != nil {
287- return fmt .Errorf ("failed to copy file %s: %v" , src , err )
288- }
289-
290- return nil
291- }
292-
293246func (s SimulatorDevice ) InstallWebDriverAgent () error {
294247
295248 file , err := s .DownloadWebDriverAgent ()
296249 if err != nil {
297250 return fmt .Errorf ("failed to download WebDriverAgent: %v" , err )
298251 }
299252
300- // defer os.Remove(file)
253+ defer os .Remove (file )
301254
302255 log .Printf ("Downloaded WebDriverAgent to %s" , file )
303256
@@ -306,49 +259,9 @@ func (s SimulatorDevice) InstallWebDriverAgent() error {
306259 return fmt .Errorf ("failed to unzip WebDriverAgent: %v" , err )
307260 }
308261
262+ defer os .RemoveAll (dir )
309263 log .Printf ("Unzipped WebDriverAgent to %s" , dir )
310264
311- // copy frameworks into this directory
312- frameworks := []string {
313- "Testing.framework" ,
314- "WebDriverAgentRunner.xctest" ,
315- "XCTAutomationSupport.framework" ,
316- "XCTest.framework" ,
317- "XCTestCore.framework" ,
318- "XCTestSupport.framework" ,
319- "XCUIAutomation.framework" ,
320- "XCUnit.framework" ,
321- }
322-
323- files := []string {
324- "libXCTestSwiftSupport.dylib" ,
325- }
326-
327- xcodePath := "/Applications/Xcode.app/Contents/Developer"
328- iPhoneSimulatorPath := xcodePath + "/Platforms/iPhoneSimulator.platform/Developer"
329- frameworksPath := iPhoneSimulatorPath + "/Library/Frameworks/"
330-
331- // copy recursively all files in frameworks to the simulator
332- for _ , framework := range frameworks {
333- src := frameworksPath + framework
334- dst := dir + "/" + framework
335- err = copy .Copy (src , dst )
336- if err != nil {
337- os .RemoveAll (dir )
338- return fmt .Errorf ("failed to copy framework %s: %v" , src , err )
339- }
340- }
341-
342- for _ , file := range files {
343- src := iPhoneSimulatorPath + "/usr/lib/" + file
344- dst := dir + "/" + file
345- err = copyFile (src , dst )
346- if err != nil {
347- os .RemoveAll (dir )
348- return fmt .Errorf ("failed to copy file %s: %v" , src , err )
349- }
350- }
351-
352265 err = InstallApp (s .UDID , dir + "/WebDriverAgentRunner-Runner.app" )
353266 if err != nil {
354267 return fmt .Errorf ("failed to install WebDriverAgent: %v" , err )
@@ -359,7 +272,6 @@ func (s SimulatorDevice) InstallWebDriverAgent() error {
359272 return fmt .Errorf ("failed to wait for WebDriverAgent to be installed: %v" , err )
360273 }
361274
362- defer os .RemoveAll (dir )
363275 return nil
364276}
365277
@@ -384,11 +296,10 @@ func (s SimulatorDevice) StartAgent() error {
384296
385297 if ! installed {
386298 log .Printf ("WebdriverAgent is not installed. Will try to install now" )
387- return fmt .Errorf ("WebdriverAgent is not installed" )
388- // err = s.InstallWebDriverAgent()
389- // if err != nil {
390- // return fmt.Errorf("SimulatorDevice: failed to install WebDriverAgent: %v", err)
391- // }
299+ err = s .InstallWebDriverAgent ()
300+ if err != nil {
301+ return fmt .Errorf ("SimulatorDevice: failed to install WebDriverAgent: %v" , err )
302+ }
392303 }
393304
394305 webdriverPackageName := "com.facebook.WebDriverAgentRunner.xctrunner"
0 commit comments