我有一个非常简单的XCTestCase实现,它测试按钮上的一个点击,并期望一个警报控制器出现。问题是tap()方法不起作用。在相关按钮的IBAction中放置一个断点,我意识到这个逻辑甚至没有被调用。
class uitestsampleUITests: XCTestCase {
var app: XCUIApplication!
override func setUp() {
super.setUp()
continueAfterFailure = false
app = XCUIApplication()
app.launch()
}
func testButton() {
let button = app.buttons["Button"]
button.tap()
expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: button, handler: nil)
waitForExpectationsWithTimeout(5.0, handler: nil)
}
}此外,复制button.tap()指令会使测试通过,如下所示:
func testButton() {
let button = app.buttons["Button"]
button.tap()
button.tap()
expectationForPredicate(NSPredicate(format: "exists == 1"), evaluatedWithObject: button, handler: nil)
waitForExpectationsWithTimeout(5.0, handler: nil)
}我在Xcode 7.3.1中面临这个问题,是不是遗漏了什么?是虫子吗?
发布于 2016-05-19 10:26:06
因此,一位苹果工程师对我的窃听器报告说:
第二种可能是,您遇到了一个问题,有时会发生这样的情况:应用程序完成启动,但启动屏幕不会立即消失,分派给应用程序的事件也不会得到正确处理。 为了解决这个问题,请考虑在测试开始时稍作延迟(睡眠(1)就足够了)。
所以我做了,现在起作用了:
override func setUp() {
super.setUp()
continueAfterFailure = false
app = XCUIApplication()
app.launch()
sleep(1)
}发布于 2017-09-09 07:32:17
对于UIWebView (它是可命中的),直到我通过坐标完成了它之后,tap才起作用:
extension XCUIElement {
func forceTap() {
coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5)).tap()
}
}希望它能帮到别人
P.S.也适用于不可命中的物品,如标签等。
发布于 2016-05-18 17:56:27
我也有过类似的经历。对我来说,问题在于,我试图挖掘的元素,有时由于某种原因而不是hittable。
从苹果的文档来看:
将tap事件发送到为元素计算的可命中点。
因此,如果一个元素不是hittable,那么tap操作就不起多大作用,这破坏了测试用例的逻辑。
为了解决这个问题,在点击某个元素之前,我会等待适当的元素变为可命中元素。相当直截了当。
#import <XCTest/XCTest.h>
@interface XCUIElement (Tap)
- (void)tapInTestCase:(XCTestCase *)testCase;
@end
@implementation XCUIElement (Tap)
- (void)tapInTestCase:(XCTestCase *)testCase
{
// wait until the element is hittable
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"hittable == true"];
[testCase expectationForPredicate:predicate evaluatedWithObject:element handler:nil];
[testCase waitForExpectationsWithTimeout:5.0f handler:nil];
// and then tap
[self tap];
}
@endhttps://stackoverflow.com/questions/37276597
复制相似问题