feat: step shell with env variables

This commit is contained in:
lilong.129
2024-01-19 15:06:20 +08:00
parent 49eaf432f8
commit e7a8cec5db
4 changed files with 36 additions and 109 deletions

View File

@@ -428,85 +428,3 @@ func TestConvertPoints(t *testing.T) {
jsons, _ := json.Marshal(eps)
println(fmt.Sprintf("%v", string(jsons)))
}
func TestDebugDualFeed(t *testing.T) {
device, _ := NewAndroidDevice()
driver, err := device.NewDriver()
if err != nil {
t.Fatal(err)
}
_, err = driver.Driver.AppTerminate("tv.danmaku.bili")
if err != nil {
t.Fatal(err)
}
err = driver.Driver.AppLaunch("tv.danmaku.bili")
if err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
// 处理弹窗
err = driver.ClosePopupsHandler()
if err != nil {
t.Fatal(err)
}
// 进入推荐页
err = driver.TapByOCR("推荐", WithScope(0, 0, 1, 0.3))
if err != nil {
t.Fatal(err)
}
// 重复采集 10 次
for i := 0; i < 10; i++ {
err = driver.SwipeUp()
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
// 点击进入某视频
err = driver.TapXY(0.3, 0.5)
if err != nil {
t.Fatal(err)
}
time.Sleep(5 * time.Second)
// 点击播放区域,展现横屏图标
err = driver.TapXY(0.5, 0.1)
if err != nil {
t.Fatal(err)
}
time.Sleep(500 * time.Millisecond)
// 切换横屏
err = driver.TapByUIDetection(
WithScreenShotUITypes("fullScreen"))
if err != nil {
// 未找到横屏图标,该页面可能不是横版视频(直播|广告|Feed
// 退出回到推荐页
driver.Driver.PressBack()
continue
}
// 观播 10s
time.Sleep(10 * time.Second)
// 返回视频页面
err = driver.Driver.PressBack()
if err != nil {
t.Fatal(err)
}
// 返回推荐页
err = driver.Driver.PressBack()
if err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
}
}

View File

@@ -157,38 +157,20 @@ func TestRunCaseWithThinkTime(t *testing.T) {
func TestRunCaseWithShell(t *testing.T) {
testcase1 := &TestCase{
Config: NewConfig("complex shell with multiple commands"),
Config: NewConfig("complex shell with env variables").
WithVariables(map[string]interface{}{
"SS": "12345",
"ABC": "$SS",
}),
TestSteps: []IStep{
NewStep("shell21").Shell("A=123; echo $A"),
NewStep("shell22").Shell("A=123 && echo $A"),
NewStep("shell23").Shell("export A=123 && echo $A"),
NewStep("shell24").Shell("for i in {1..5}; do echo $i; sleep 1; done"),
},
}
testcase2 := &TestCase{
Config: NewConfig("complex shell with environment variables"),
TestSteps: []IStep{
NewStep("shell3").Shell("A=123; echo $A"),
},
}
testcase3 := &TestCase{
Config: NewConfig("check shell exit code"),
TestSteps: []IStep{
NewStep("shell21").
Shell("ls -a; exit 3; exit 5").
Validate().
AssertExitCode(3),
NewStep("shell22").
Shell("ls -a; exit 3 && exit 5").
Validate().
AssertExitCode(3),
NewStep("shell21").Shell("echo hello world"),
NewStep("shell21").Shell("echo $ABC"),
NewStep("shell21").Shell("which hrp"),
},
}
r := NewRunner(t)
err := r.Run(testcase1, testcase2, testcase3)
err := r.Run(testcase1)
if err != nil {
t.Fatal()
}

View File

@@ -2,6 +2,7 @@ package hrp
import (
"fmt"
"os"
"time"
"github.com/httprunner/funplugin/myexec"
@@ -84,6 +85,11 @@ func runStepShell(r *SessionRunner, step *TStep) (stepResult *StepResult, err er
ContentSize: 0,
}
vars := r.caseRunner.parsedConfig.Variables
for key, value := range vars {
os.Setenv(key, fmt.Sprintf("%v", value))
}
start := time.Now()
exitCode, err := myexec.RunShell(shell.String)
stepResult.Elapsed = time.Since(start).Milliseconds()

21
hrp/testcase3.json Normal file
View File

@@ -0,0 +1,21 @@
{
"config": {
"name": "check shell exit code"
},
"teststeps": [
{
"name": "shell21",
"shell": {
"string": "ls -a; exit 3; exit 5",
"expect_exit_code": 3
}
},
{
"name": "shell22",
"shell": {
"string": "ls -a; exit 3 && exit 5",
"expect_exit_code": 3
}
}
]
}