如何使用iOS上的工具验证表视图中标签的名称?
我正在使用Xcode“Instruments”工具为iOS应用程序构建自动化,我需要验证我在应用程序中创建的条目的标签是否正确 出于某种原因,下面列出的代码不会导致完全通过或失败。相反,当它运行时,我在日志中得到一个“问题”警告,并且测试在没有显式关闭的情况下退出 我想更改我的测试,以便检查我知道正在创建的标签名称,因为我可以在自动化运行后使用AccessibilityView查看它 如果标签是正确的,那么我想将测试记录为通过 我已经使用如何使用iOS上的工具验证表视图中标签的名称?,ios,instruments,ui-automation,ios-ui-automation,Ios,Instruments,Ui Automation,Ios Ui Automation,我正在使用Xcode“Instruments”工具为iOS应用程序构建自动化,我需要验证我在应用程序中创建的条目的标签是否正确 出于某种原因,下面列出的代码不会导致完全通过或失败。相反,当它运行时,我在日志中得到一个“问题”警告,并且测试在没有显式关闭的情况下退出 我想更改我的测试,以便检查我知道正在创建的标签名称,因为我可以在自动化运行后使用AccessibilityView查看它 如果标签是正确的,那么我想将测试记录为通过 我已经使用uiataTarget.localTarget().log
uiataTarget.localTarget().logElementTree()
映射了我的元素树,并且在创建条目后,我使用AccessibilityInspector验证了标签的名称。问题是,我似乎无法获得验证此正确性的语法
我的可访问性检查器已验证标签名称为:MyDogs!它具有静态文本的特点,给出了{114,0},{166480}的框架
通过查看元素树(我希望我可以粘贴到这里),可以沿着以下路径找到标签:
\Target
-+
--\Application
---+
----\Window
-----+
------\TableView
-------+
--------\TableCell: name:MyDogs! rect:{0, 40},{480,166}}
---------|UIAStaticText: name:MyDogs! value:MyDogs! rect:{{0, 40},{480, 166}}
---------|UIAButton: name:story list share rect:{{439, 41},{33, 28}}
有人能告诉我怎么核实这个标签吗
我当前的代码如下所示(但没有检查标签——因为我不知道如何检查):
任何反馈或帮助都将不胜感激
---------------------------------更新--------------------------------------
谢谢大家的帮助!我实际上得到了标题的值,并将在下面显示我的解决方案。但无论我做什么,我都无法让通过/失败日志功能正常工作——问题也一直存在。我一直被激怒
问题:脚本结束时没有说明关闭此测试
测试结束时的消息。我开始相信这是仪器的一个缺陷
以下是我更新的测试:
var target = UIATarget.localTarget();
var app = UIATarget.localTarget().frontMostApp();
var testName = "LoginCreateEntry";
//Start test logging
UIALogger.logStart( testName );
//Do lots of gui automation stuff here to create the entry which will appear in my app interface.
//I want to verify that the title I gave the entry matches what appears in the app interface
var window = app.mainWindow();
var tableView = window.tableViews()[0];
var tableGroup = tableView.groups()[0];
var entryName = "My Dogs!";
var myEntry = tableView.cells()[0].name(); //<-- This is what I needed!!!
UIALogger.logMessage("My Story Title: " + myEntry); //Print out entry name in log
if (myEntry === entryName) {
UIALogger.logMessage("My entry was created!");
//Mark the test as a PASS
UIALogger.logPass (testName);
} else {
UIALogger.logMessage("My entry was not created!");
//Mark the test as a FAIL
UIALogger.loFails (testName);
}
//End test
var target=uiatatarget.localTarget();
var app=UIATarget.localTarget().frontMostApp();
var testName=“LoginCreateEntry”;
//启动测试日志记录
UIALogger.logStart(testName);
//在这里做大量的gui自动化工作来创建将出现在我的应用程序界面中的条目。
//我想验证我给条目的标题是否与应用程序界面中显示的标题匹配
var window=app.mainWindow();
var tableView=window.tableView()[0];
var tableGroup=tableView.groups()[0];
var entryName=“我的狗!”;
var myEntry=tableView.cells()[0].name()// 我建议使用。使用该库,您可以轻松创建测试用例,并检查标签是否存在,以及是否等于mydogs
你可以这样使用它
test("LoginCreateEntry", function(target,app){
//Create Entry
//....
var myEntry = target.frontMostApp().mainWindow().scrollViews().staticTexts()["My Dogs!"].name();
//Check is Label is equal to My Dogs
//Test "LoginCreateEntry" will fail if myEntry is not equal to My Dogs
assertEquals(myEntry, "My Dogs");
});
另外,至少从2012年夏天开始,您应该使用.name()
而不是.value()
来获取标签的名称,在iOS 5中,我发现我无法通过accessibilityLabel访问UIA元素,除非accessibilityIdentifier也被设置好
不幸的是,从那时起(从Xcode 4.2开始),accessibilityIdentifier只能在代码中设置,而不能在Interface Builder中设置。不确定从那以后情况是否有所改善。有关更多信息,请参见此SO问题:
但正如我所提到的,如果你可以访问应用程序的源代码,你可以通过automationIdentifier获得UIA访问权。(或者我想,可以通过访问影响某人!)
我在github上有一个项目,展示了如何在UILabel(在应用程序源代码中)上设置accessibilityIdentifier,然后编写一个UIA测试,以您的代码尝试的方式成功访问元素
首先是我的测试文件,我试图在其中找到显示字符串“!”的UILabel:
然后,在目标应用程序的源中,设置UILabel的可访问性属性如下所示:
// If device supports it, set accessibility identifiers for the UILabels
// in order to find them in UIAutomation easily. Note that the
// accessibilityIdentifier can't be set in IB yet, as of Xcode 4.3.3;
// also, it's only supported in iOS 5 and up.
topBarLabel.isAccessibilityElement = YES;
bigCharLabel.isAccessibilityElement = YES;
if ([topBarLabel respondsToSelector:@selector(accessibilityIdentifier)]) {
topBarLabel.accessibilityIdentifier = @"topBarLabel";
bigCharLabel.accessibilityIdentifier = @"bigCharLabel";
} else {
topBarLabel.accessibilityLabel = @"topBarLabel";
bigCharLabel.accessibilityLabel = @"bigCharLabel";
}
完整资料来源如下:
我对您的问题做了一些调整,使其符合您的总体风格偏好,并为您提供更为集中的标签,我希望这有助于您获得好的答案。好问题,顺便说一句!在未来的案例中可能会有一些有趣的事情,比如说,如果你自己找到了答案,那么把答案贴在上面并接受它显然是可以的看,这些信息被传递给了我们的开发团队,非常有用。很明显,测试中的应用程序并没有考虑到自动化,所以这对每个人来说都是一次学习的经历。用于大规模发布的iOS应用程序需要从一开始就考虑到自动化。顺便说一句,非常感谢!很高兴这有帮助!如果这个答案和另一个海报的答案是有帮助的,那么向上投票总是很感激的。这提醒了我,我应该投票表决你的问题。非常感谢!原来这就是我需要的:var myEntry=tableView.cells()[0].name();
// for iOS 5 - note 4 must be handled differently
var charDisplayed = window.staticTexts()["bigCharLabel"].value();
if (charDisplayed == "!") {
UIALogger.logPass("Exclamation point displayed at startup");
} else {
UIALogger.logFail("Incorrect character displayed: " + charDisplayed);
}
// If device supports it, set accessibility identifiers for the UILabels
// in order to find them in UIAutomation easily. Note that the
// accessibilityIdentifier can't be set in IB yet, as of Xcode 4.3.3;
// also, it's only supported in iOS 5 and up.
topBarLabel.isAccessibilityElement = YES;
bigCharLabel.isAccessibilityElement = YES;
if ([topBarLabel respondsToSelector:@selector(accessibilityIdentifier)]) {
topBarLabel.accessibilityIdentifier = @"topBarLabel";
bigCharLabel.accessibilityIdentifier = @"bigCharLabel";
} else {
topBarLabel.accessibilityLabel = @"topBarLabel";
bigCharLabel.accessibilityLabel = @"bigCharLabel";
}