mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-07 08:49:37 +08:00
feat: capture pcap file for iOS, including CLI hrp ios pcap
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
- feat: run xctest before start ios automation
|
- feat: run xctest before start ios automation
|
||||||
- feat: run step with specified loop times
|
- feat: run step with specified loop times
|
||||||
- feat: add options for FindTexts
|
- feat: add options for FindTexts
|
||||||
- feat: capture pcap file for iOS
|
- feat: capture pcap file for iOS, including CLI `hrp ios pcap` and option `uixt.WithIOSPcapOn(true)`
|
||||||
- refactor: move all UI APIs to uixt pkg
|
- refactor: move all UI APIs to uixt pkg
|
||||||
- docs: add examples for UI APIs
|
- docs: add examples for UI APIs
|
||||||
|
|
||||||
|
|||||||
58
hrp/cmd/ios/pcap.go
Normal file
58
hrp/cmd/ios/pcap.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package ios
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
|
"github.com/httprunner/httprunner/v4/hrp/internal/env"
|
||||||
|
"github.com/httprunner/httprunner/v4/hrp/pkg/uixt"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var pcapCmd = &cobra.Command{
|
||||||
|
Use: "pcap",
|
||||||
|
Short: "capture ios network packets",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
device, err := uixt.NewIOSDevice(
|
||||||
|
uixt.WithUDID(udid),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err).Msg("failed to init ios device")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = builtin.EnsureFolderExists(env.ResultsPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = device.StartPcap(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer device.StopPcap()
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
timer := time.NewTimer(time.Duration(timeDuration) * time.Second)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-timer.C:
|
||||||
|
return nil
|
||||||
|
case <-c:
|
||||||
|
log.Warn().Msg("received signal, stop pcap")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var timeDuration int
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
pcapCmd.Flags().StringVarP(&udid, "udid", "u", "", "specify device by udid")
|
||||||
|
pcapCmd.Flags().IntVarP(&timeDuration, "duration", "t", 10, "specify time duraion in seconds")
|
||||||
|
iosRootCmd.AddCommand(pcapCmd)
|
||||||
|
}
|
||||||
@@ -388,13 +388,13 @@ func (dev *IOSDevice) StopPerf() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dev *IOSDevice) StartPcap() error {
|
func (dev *IOSDevice) StartPcap() error {
|
||||||
|
log.Info().Msg("start packet capture")
|
||||||
packets, err := dev.d.PcapStart()
|
packets, err := dev.d.PcapStart()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
dev.pcapFile = filepath.Join(env.ResultsPath, "dump.pcap")
|
dev.pcapFile = filepath.Join(env.ResultsPath, "dump.pcap")
|
||||||
log.Info().Str("pcapFile", dev.pcapFile).Msg("create pcap file")
|
|
||||||
file, err := os.OpenFile(dev.pcapFile,
|
file, err := os.OpenFile(dev.pcapFile,
|
||||||
os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o755)
|
os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -435,6 +435,7 @@ func (dev *IOSDevice) StopPcap() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
close(dev.pcapStop)
|
close(dev.pcapStop)
|
||||||
|
log.Info().Str("pcapFile", dev.pcapFile).Msg("stop packet capture")
|
||||||
return dev.pcapFile
|
return dev.pcapFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user