Skip to content

Commit 0dbe361

Browse files
authored
feat: automatically install wda on ios simulator
1 parent b7ca418 commit 0dbe361

File tree

3 files changed

+11
-112
lines changed

3 files changed

+11
-112
lines changed

devices/simulator.go

Lines changed: 11 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ package devices
22

33
import (
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

230226
func (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-
293246
func (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"

go.mod

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ toolchain go1.23.5
66

77
require (
88
github.com/mholt/archiver v3.1.1+incompatible
9-
github.com/otiai10/copy v1.14.1
109
github.com/spf13/cobra v1.9.1
1110
)
1211

@@ -17,11 +16,8 @@ require (
1716
github.com/google/go-cmp v0.6.0 // indirect
1817
github.com/inconshreveable/mousetrap v1.1.0 // indirect
1918
github.com/nwaples/rardecode v1.1.0 // indirect
20-
github.com/otiai10/mint v1.6.3 // indirect
2119
github.com/pierrec/lz4 v2.6.1+incompatible // indirect
2220
github.com/spf13/pflag v1.0.6 // indirect
2321
github.com/ulikunitz/xz v0.5.9 // indirect
2422
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
25-
golang.org/x/sync v0.15.0 // indirect
26-
golang.org/x/sys v0.33.0 // indirect
2723
)

go.sum

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ github.com/mholt/archiver v3.1.1+incompatible h1:1dCVxuqs0dJseYEhi5pl7MYPH9zDa1w
2323
github.com/mholt/archiver v3.1.1+incompatible/go.mod h1:Dh2dOXnSdiLxRiPoVfIr/fI1TwETms9B8CTWfeh7ROU=
2424
github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ=
2525
github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0=
26-
github.com/otiai10/copy v1.14.1 h1:5/7E6qsUMBaH5AnQ0sSLzzTg1oTECmcCmT6lvF45Na8=
27-
github.com/otiai10/copy v1.14.1/go.mod h1:oQwrEDDOci3IM8dJF0d8+jnbfPDllW6vUjNc3DoZm9I=
28-
github.com/otiai10/mint v1.6.3 h1:87qsV/aw1F5as1eH1zS/yqHY85ANKVMgkDrf9rcxbQs=
29-
github.com/otiai10/mint v1.6.3/go.mod h1:MJm72SBthJjz8qhefc4z1PYEieWmy8Bku7CjcAqyUSM=
3026
github.com/pierrec/lz4 v2.6.1+incompatible h1:9UY3+iC23yxF0UfGaYrGplQ+79Rg+h/q9FV9ix19jjM=
3127
github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
3228
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
@@ -42,10 +38,6 @@ github.com/ulikunitz/xz v0.5.9 h1:RsKRIA2MO8x56wkkcd3LbtcE/uMszhb6DpRf+3uwa3I=
4238
github.com/ulikunitz/xz v0.5.9/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
4339
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
4440
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
45-
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
46-
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
47-
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
48-
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
4941
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
5042
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5143
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)