Xcode7 ui testing: staticTexts[“XX”].swipeRight() swipes not far enough

五迷三道 提交于 2019-12-24 03:16:54

问题


For UI testing I want to perform a swipeRight-gesture to make further buttons accessible. The element I want to swipe is at the bottom of the screen. I access it by:

element.staticTexts["TEST TEXT"].swipeRight()

When performing the test the swipe goes not far enough. It does not trigger the element to slide completely to the right and show the further buttons I want to tap.

I have the feeling that swipeRight()grabs the middle of the static text and performs the gesture.

Is there any possibility to grab the element more to the left, such that it slides more to the right?

Thanks for any suggestions!


回答1:


Try this approach:

    let startPoint = element.staticTexts["TEST TEXT"].coordinateWithNormalizedOffset(CGVectorMake(0, 0)) // center of the element
    let finishPoint = startPoint.coordinateWithOffset(CGVectorMake(1000, 0))
    startPoint.pressForDuration(0, thenDragToCoordinate: finishPoint)

You can adjust 1000 to reach the effect you want.




回答2:


At least as of Swift 4.1, Xcode 9.4.1, the values you need represent a percentage of the element's width and height - 0.0 to 1.0.

For a long swipe, you need to drag across the element about 60%, so like this:

// right long swipe - move your 'x' from 0.0 to 0.6
let startPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
let endPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.6, dy: 0.0))
startPoint.press(forDuration: 0, thenDragTo: endPoint)

and:

// left long swipe - move your 'x' from 0.6 to 0.0
let startPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.6, dy: 0.0))
let endPoint = element.coordinate(withNormalizedOffset: CGVector(dx: 0.0, dy: 0.0))
startPoint.press(forDuration: 0, thenDragTo: endPoint)

You could set your dy value to 0.5, say, if you're wanting to drag at the center of the element. But, note that your dy values must be the same for swiping right or left. If you were to convert this for a swipe up or down, the dx values would need to be identical, while the dy values would change appropriately instead. (The swipe needs to move along a straight line.)

Note, also, that the forDuration value represents the number of seconds to hold the touch before dragging. The higher the value, the longer the initial press.

I posted an XCUIElement extension for long swipes in this answer.



来源:https://stackoverflow.com/questions/37158631/xcode7-ui-testing-statictextsxx-swiperight-swipes-not-far-enough

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!