mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-04 23:16:57 +08:00
change: make assert msg optional
This commit is contained in:
@@ -38,6 +38,12 @@ const (
|
|||||||
uiLongClick MobileMethod = "long_click"
|
uiLongClick MobileMethod = "long_click"
|
||||||
uiSwipe MobileMethod = "swipe"
|
uiSwipe MobileMethod = "swipe"
|
||||||
uiInput MobileMethod = "input"
|
uiInput MobileMethod = "input"
|
||||||
|
|
||||||
|
// UI validation
|
||||||
|
assertionNameExists string = "name_exists"
|
||||||
|
assertionNameNotExists string = "name_not_exists"
|
||||||
|
assertionXpathExists string = "xpath_exists"
|
||||||
|
assertionXpathNotExists string = "xpath_not_exists"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MobileAction struct {
|
type MobileAction struct {
|
||||||
|
|||||||
+26
-5
@@ -1,5 +1,7 @@
|
|||||||
package hrp
|
package hrp
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type AndroidAction struct {
|
type AndroidAction struct {
|
||||||
MobileAction
|
MobileAction
|
||||||
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"`
|
Serial string `json:"serial,omitempty" yaml:"serial,omitempty"`
|
||||||
@@ -164,12 +166,31 @@ type StepAndroidValidation struct {
|
|||||||
step *TStep
|
step *TStep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepAndroidValidation) AssertXpathExists(expectedXpath string, msg string) *StepAndroidValidation {
|
func (s *StepAndroidValidation) AssertXpathExists(expectedXpath string, msg ...string) *StepAndroidValidation {
|
||||||
v := Validator{
|
v := Validator{
|
||||||
Check: "UI",
|
Check: "UI",
|
||||||
Assert: "xpath_exists",
|
Assert: assertionXpathExists,
|
||||||
Expect: expectedXpath,
|
Expect: expectedXpath,
|
||||||
Message: msg,
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("xpath [%s] not found", expectedXpath)
|
||||||
|
}
|
||||||
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepAndroidValidation) AssertXpathNotExists(expectedXpath string, msg ...string) *StepAndroidValidation {
|
||||||
|
v := Validator{
|
||||||
|
Check: "UI",
|
||||||
|
Assert: assertionXpathNotExists,
|
||||||
|
Expect: expectedXpath,
|
||||||
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("xpath [%s] should not exist", expectedXpath)
|
||||||
}
|
}
|
||||||
s.step.Validators = append(s.step.Validators, v)
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
return s
|
return s
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package hrp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAndroidAction(t *testing.T) {
|
||||||
|
testCase := &TestCase{
|
||||||
|
Config: NewConfig("android ui action"),
|
||||||
|
TestSteps: []IStep{
|
||||||
|
NewStep("launch douyin").
|
||||||
|
Android().Serial("xxx").Click("抖音").
|
||||||
|
Validate().
|
||||||
|
AssertXpathExists("首页", "首页 tab 不存在").
|
||||||
|
AssertXpathExists("消息", "消息 tab 不存在"),
|
||||||
|
NewStep("swipe up and down").
|
||||||
|
Android().Serial("xxx").SwipeUp().SwipeUp().SwipeDown(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tCase := testCase.ToTCase()
|
||||||
|
fmt.Println(tCase)
|
||||||
|
|
||||||
|
// err := NewRunner(t).Run(testCase)
|
||||||
|
// if err != nil {
|
||||||
|
// t.Fatal(err)
|
||||||
|
// }
|
||||||
|
}
|
||||||
+40
-24
@@ -242,45 +242,61 @@ type StepIOSValidation struct {
|
|||||||
step *TStep
|
step *TStep
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepIOSValidation) AssertNameExists(expectedName string, msg string) *StepIOSValidation {
|
func (s *StepIOSValidation) AssertNameExists(expectedName string, msg ...string) *StepIOSValidation {
|
||||||
v := Validator{
|
v := Validator{
|
||||||
Check: "UI",
|
Check: "UI",
|
||||||
Assert: "name_exists",
|
Assert: assertionNameExists,
|
||||||
Expect: expectedName,
|
Expect: expectedName,
|
||||||
Message: msg,
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("[%s] not found", expectedName)
|
||||||
}
|
}
|
||||||
s.step.Validators = append(s.step.Validators, v)
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepIOSValidation) AssertNameNotExists(expectedName string, msg string) *StepIOSValidation {
|
func (s *StepIOSValidation) AssertNameNotExists(expectedName string, msg ...string) *StepIOSValidation {
|
||||||
v := Validator{
|
v := Validator{
|
||||||
Check: "UI",
|
Check: "UI",
|
||||||
Assert: "name_not_exists",
|
Assert: assertionNameNotExists,
|
||||||
Expect: expectedName,
|
Expect: expectedName,
|
||||||
Message: msg,
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("[%s] should not exist", expectedName)
|
||||||
}
|
}
|
||||||
s.step.Validators = append(s.step.Validators, v)
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepIOSValidation) AssertXpathExists(expectedXpath string, msg string) *StepIOSValidation {
|
func (s *StepIOSValidation) AssertXpathExists(expectedXpath string, msg ...string) *StepIOSValidation {
|
||||||
v := Validator{
|
v := Validator{
|
||||||
Check: "UI",
|
Check: "UI",
|
||||||
Assert: "xpath_exists",
|
Assert: assertionXpathExists,
|
||||||
Expect: expectedXpath,
|
Expect: expectedXpath,
|
||||||
Message: msg,
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("xpath [%s] not found", expectedXpath)
|
||||||
}
|
}
|
||||||
s.step.Validators = append(s.step.Validators, v)
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StepIOSValidation) AssertXpathNotExists(expectedXpath string, msg string) *StepIOSValidation {
|
func (s *StepIOSValidation) AssertXpathNotExists(expectedXpath string, msg ...string) *StepIOSValidation {
|
||||||
v := Validator{
|
v := Validator{
|
||||||
Check: "UI",
|
Check: "UI",
|
||||||
Assert: "xpath_not_exists",
|
Assert: assertionXpathNotExists,
|
||||||
Expect: expectedXpath,
|
Expect: expectedXpath,
|
||||||
Message: msg,
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("xpath [%s] should not exist", expectedXpath)
|
||||||
}
|
}
|
||||||
s.step.Validators = append(s.step.Validators, v)
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
return s
|
return s
|
||||||
@@ -673,13 +689,13 @@ func (w *wdaClient) doValidation(iValidators []interface{}) (validateResults []*
|
|||||||
|
|
||||||
var result bool
|
var result bool
|
||||||
switch validator.Assert {
|
switch validator.Assert {
|
||||||
case "xpath_exists":
|
case assertionXpathExists:
|
||||||
result = w.assertXpath(expected, true)
|
result = w.assertXpath(expected, true)
|
||||||
case "xpath_not_exists":
|
case assertionXpathNotExists:
|
||||||
result = w.assertXpath(expected, false)
|
result = w.assertXpath(expected, false)
|
||||||
case "name_exists":
|
case assertionNameExists:
|
||||||
result = w.assertName(expected, true)
|
result = w.assertName(expected, true)
|
||||||
case "name_not_exists":
|
case assertionNameNotExists:
|
||||||
result = w.assertName(expected, false)
|
result = w.assertName(expected, false)
|
||||||
}
|
}
|
||||||
if result {
|
if result {
|
||||||
|
|||||||
@@ -5,28 +5,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAndroidAction(t *testing.T) {
|
|
||||||
testCase := &TestCase{
|
|
||||||
Config: NewConfig("android ui action"),
|
|
||||||
TestSteps: []IStep{
|
|
||||||
NewStep("launch douyin").
|
|
||||||
Android().Serial("xxx").Click("抖音").
|
|
||||||
Validate().
|
|
||||||
AssertXpathExists("首页", "首页 tab 不存在").
|
|
||||||
AssertXpathExists("消息", "消息 tab 不存在"),
|
|
||||||
NewStep("swipe up and down").
|
|
||||||
Android().Serial("xxx").SwipeUp().SwipeUp().SwipeDown(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
tCase := testCase.ToTCase()
|
|
||||||
fmt.Println(tCase)
|
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIOSSettingsAction(t *testing.T) {
|
func TestIOSSettingsAction(t *testing.T) {
|
||||||
testCase := &TestCase{
|
testCase := &TestCase{
|
||||||
Config: NewConfig("ios ui action on Settings"),
|
Config: NewConfig("ios ui action on Settings"),
|
||||||
@@ -34,17 +12,18 @@ func TestIOSSettingsAction(t *testing.T) {
|
|||||||
NewStep("launch Settings").
|
NewStep("launch Settings").
|
||||||
IOS().Home().Click("//*[@label='设置']").
|
IOS().Home().Click("//*[@label='设置']").
|
||||||
Validate().
|
Validate().
|
||||||
AssertNameExists("飞行模式", "「飞行模式」不存在").
|
AssertNameExists("飞行模式").
|
||||||
AssertNameNotExists("飞行模式2", "「飞行模式2」不存在"),
|
AssertNameNotExists("飞行模式2"),
|
||||||
NewStep("swipe up and down").
|
NewStep("swipe up and down").
|
||||||
IOS().SwipeUp().SwipeUp().SwipeDown(),
|
IOS().SwipeUp().SwipeUp().SwipeDown(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fmt.Println(testCase)
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
// err := NewRunner(t).Run(testCase)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIOSSearchApp(t *testing.T) {
|
func TestIOSSearchApp(t *testing.T) {
|
||||||
@@ -54,16 +33,17 @@ func TestIOSSearchApp(t *testing.T) {
|
|||||||
NewStep("进入 App 资源库 搜索框").
|
NewStep("进入 App 资源库 搜索框").
|
||||||
IOS().Home().SwipeLeft().Times(2).Click("dewey-search-field").
|
IOS().Home().SwipeLeft().Times(2).Click("dewey-search-field").
|
||||||
Validate().
|
Validate().
|
||||||
AssertNameExists("取消", "「取消」不存在"),
|
AssertNameExists("取消"),
|
||||||
NewStep("搜索抖音").
|
NewStep("搜索抖音").
|
||||||
IOS().Input("抖音\n"),
|
IOS().Input("抖音\n"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fmt.Println(testCase)
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
// err := NewRunner(t).Run(testCase)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIOSAppLaunch(t *testing.T) {
|
func TestIOSAppLaunch(t *testing.T) {
|
||||||
@@ -80,11 +60,12 @@ func TestIOSAppLaunch(t *testing.T) {
|
|||||||
IOS().AppLaunchUnattached("com.ss.iphone.article.News"),
|
IOS().AppLaunchUnattached("com.ss.iphone.article.News"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fmt.Println(testCase)
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
// err := NewRunner(t).Run(testCase)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIOSWeixinLive(t *testing.T) {
|
func TestIOSWeixinLive(t *testing.T) {
|
||||||
@@ -103,18 +84,19 @@ func TestIOSWeixinLive(t *testing.T) {
|
|||||||
Click("发现").Sleep(5). // 进入「发现页」;等待 5 秒确保加载完成
|
Click("发现").Sleep(5). // 进入「发现页」;等待 5 秒确保加载完成
|
||||||
Click([]float64{0.5, 0.3}). // 基于坐标位置点击「直播」;TODO:通过 OCR 识别「直播」
|
Click([]float64{0.5, 0.3}). // 基于坐标位置点击「直播」;TODO:通过 OCR 识别「直播」
|
||||||
Validate().
|
Validate().
|
||||||
AssertNameExists("直播", "「直播」不存在"),
|
AssertNameExists("直播"),
|
||||||
NewStep("向上滑动 5 次").
|
NewStep("向上滑动 5 次").
|
||||||
IOS().
|
IOS().
|
||||||
SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存
|
SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存
|
||||||
SwipeUp().Times(2).ScreenShot(), // 再上划 2 次,截图保存
|
SwipeUp().Times(2).ScreenShot(), // 再上划 2 次,截图保存
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fmt.Println(testCase)
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
// err := NewRunner(t).Run(testCase)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIOSCameraPhotoCapture(t *testing.T) {
|
func TestIOSCameraPhotoCapture(t *testing.T) {
|
||||||
@@ -131,11 +113,12 @@ func TestIOSCameraPhotoCapture(t *testing.T) {
|
|||||||
IOS().Click("PhotoCapture"),
|
IOS().Click("PhotoCapture"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fmt.Println(testCase)
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
// err := NewRunner(t).Run(testCase)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIOSCameraVideoCapture(t *testing.T) {
|
func TestIOSCameraVideoCapture(t *testing.T) {
|
||||||
@@ -160,11 +143,12 @@ func TestIOSCameraVideoCapture(t *testing.T) {
|
|||||||
Click("VideoCapture"), // 停止录像
|
Click("VideoCapture"), // 停止录像
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fmt.Println(testCase)
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
// err := NewRunner(t).Run(testCase)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIOSDouyinAction(t *testing.T) {
|
func TestIOSDouyinAction(t *testing.T) {
|
||||||
@@ -180,9 +164,10 @@ func TestIOSDouyinAction(t *testing.T) {
|
|||||||
IOS().SwipeUp().Times(3).SwipeDown(),
|
IOS().SwipeUp().Times(3).SwipeDown(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
fmt.Println(testCase)
|
||||||
|
|
||||||
err := NewRunner(t).Run(testCase)
|
// err := NewRunner(t).Run(testCase)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
t.Fatal(err)
|
// t.Fatal(err)
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user