mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 23:12:41 +08:00
refactor: convertToAbsoluteCoordinates
This commit is contained in:
47
pkg/uixt/driver_utils.go
Normal file
47
pkg/uixt/driver_utils.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/httprunner/httprunner/v5/code"
|
||||
)
|
||||
|
||||
func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64) (
|
||||
absFromX, absFromY, absToX, absToY float64, err error) {
|
||||
|
||||
err = assertCoordinatesRelative(fromX, fromY, toX, toY)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
windowSize, err := driver.WindowSize()
|
||||
if err != nil {
|
||||
err = errors.Wrap(code.DeviceGetInfoError, err.Error())
|
||||
return
|
||||
}
|
||||
width := windowSize.Width
|
||||
height := windowSize.Height
|
||||
|
||||
absFromX = float64(width) * fromX
|
||||
absFromY = float64(height) * fromY
|
||||
absToX = float64(width) * toX
|
||||
absToY = float64(height) * toY
|
||||
|
||||
return absFromX, absFromY, absToX, absToY, nil
|
||||
}
|
||||
|
||||
func assertCoordinatesRelative(fromX, fromY, toX, toY float64) error {
|
||||
if !assertRelative(fromX) || !assertRelative(fromY) ||
|
||||
!assertRelative(toX) || !assertRelative(toY) {
|
||||
return errors.Wrap(code.InvalidCaseError,
|
||||
fmt.Sprintf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1",
|
||||
fromX, fromY, toX, toY))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func assertRelative(p float64) bool {
|
||||
return p >= 0 && p <= 1
|
||||
}
|
||||
Reference in New Issue
Block a user