我正在使用Xcode“Instruments”工具为iOS应用程序构建自动化,我需要验证我在我的应用程序中创建的条目的标签是否正确。
由于某些原因,下面列出的代码不会导致稳定的通过或失败。相反,当它运行时,我在日志中得到一个“问题”警告,并且测试退出而没有明确关闭。
我想更改我的测试,以便检查我知道正在创建的标签名称,因为我可以在自动化运行后使用AccessibilityViewer查看它。
如果标签是正确的,那么我想将测试记录为通过。
我已经使用UIATarget.localTarget().logElementTree()
来映射我的元素树,并且在创建条目后我使用了AccessibilityInspector来验证我的标签名称。麻烦的是,我似乎无法获得验证这个正确的语法。
My Accessibility Inspector已验证标签名称为:MyDogs!它具有静态文本的特征,并给出{{114,0},{166,480}}
的框架通过查看元素树 - 我希望我可以粘贴在这里,看起来像沿着这条路找到标签:
\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 testName = "LoginCreateEntry";
//Start test logging
UIALogger.logStart(testName);
//This is supposed to target the entry that my automation has created.
//The flow goes, run the automation that creates the entry, then verify that the entry
//got created as expected and is visible to the user in the iPhone interface.
var myEntry = target.frontMostApp().mainWindow().scrollViews().staticTexts()["My Dogs!"].value();
var entryName = "My Dogs!";
//Do a bunch of UI automation here to create my entry, which results in the entry
//appearing in the mainWindow with the label: My Dogs!
//If myEntry evaluates to true, then call this test a pass.
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.logFail(testName);
}
//End test
任何反馈或帮助都会非常感激!!
--------------------------------- UPDATE ------------- -------------------------
感谢你的帮助!我实际上得到了标题的价值,并将在下面显示我的解决方案。但无论我做什么,我都无法使通过/失败日志记录功能正常工作 - 问题也是encountered by others。我不断得到充满激情的
Issue: Script ended without explicting closing this test
消息。我开始相信这是仪器的一个错误。
这是我更新的测试:
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
答案 0 :(得分:5)
我建议使用tuneup_js。使用该库,您可以轻松创建测试用例并检查标签是否存在且等于My Dogs
您可以像这样使用
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");
});
P.S。您应该使用.name()
而非.value()
来获取标签名称
答案 1 :(得分:5)
至少截至2012年夏季和iOS 5,我发现我无法通过accessibilityLabel访问UIA元素,除非还设置了accessibilityIdentifier。
不幸的是,从那时起(以及Xcode 4.2),accessibilityIdentifier只能在代码中设置,而不能在Interface Builder中设置。从那时起不确定事情是否有所改善。有关详细信息,请参阅此SO问题:
can I set accessibility identifier in interface builder? Xcode4.2
但正如我所提到的,如果您可以访问应用程序的源代码,那么您可以通过automationIdentifier获取UIA访问权限。 (或者我可以影响有人访问的人!)
我在github上有一个项目,它展示了如何在UILabel上设置accessibilityIdentifier(在应用程序源代码中),然后编写一个UIA测试,按照你的代码尝试的方式成功访问该元素。
首先是我的测试文件,我尝试找到显示字符串“!”的UILabel:
https://github.com/billagee/UnicodeTapper-iphone4.2/blob/master/UnicodeTapperTests/tests.js
// 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);
}
然后,在目标应用程序的源代码中,设置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";
}
完整的来源是: