首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XCUIElement tap()不工作

XCUIElement tap()不工作
EN

Stack Overflow用户
提问于 2016-05-17 12:44:02
回答 5查看 11.4K关注 0票数 14

我有一个非常简单的XCTestCase实现,它测试按钮上的一个点击,并期望一个警报控制器出现。问题是tap()方法不起作用。在相关按钮的IBAction中放置一个断点,我意识到这个逻辑甚至没有被调用。

代码语言:javascript
复制
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()指令会使测试通过,如下所示:

代码语言:javascript
复制
    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中面临这个问题,是不是遗漏了什么?是虫子吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-05-19 10:26:06

因此,一位苹果工程师对我的窃听器报告说:

第二种可能是,您遇到了一个问题,有时会发生这样的情况:应用程序完成启动,但启动屏幕不会立即消失,分派给应用程序的事件也不会得到正确处理。 为了解决这个问题,请考虑在测试开始时稍作延迟(睡眠(1)就足够了)。

所以我做了,现在起作用了:

代码语言:javascript
复制
override func setUp() {
    super.setUp()
    continueAfterFailure = false
    app = XCUIApplication()
    app.launch()
    sleep(1)
}
票数 10
EN

Stack Overflow用户

发布于 2017-09-09 07:32:17

对于UIWebView (它是可命中的),直到我通过坐标完成了它之后,tap才起作用:

代码语言:javascript
复制
extension XCUIElement {
    func forceTap() {
        coordinate(withNormalizedOffset: CGVector(dx:0.5, dy:0.5)).tap()
    }
}

希望它能帮到别人

P.S.也适用于不可命中的物品,如标签等。

票数 9
EN

Stack Overflow用户

发布于 2016-05-18 17:56:27

我也有过类似的经历。对我来说,问题在于,我试图挖掘的元素,有时由于某种原因而不是hittable

从苹果的文档来看:

将tap事件发送到为元素计算的可命中点。

因此,如果一个元素不是hittable,那么tap操作就不起多大作用,这破坏了测试用例的逻辑。

为了解决这个问题,在点击某个元素之前,我会等待适当的元素变为可命中元素。相当直截了当。

代码语言:javascript
复制
#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];
}

@end
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37276597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档