如何使用XCUITest测试UIImageView是否包含具有特定名称的图像? [英] How to test if a UIImageView contains an image with a specific name using XCUITest?
问题描述
我有一个collectionView,其中有一堆单元格,每个单元格中包含一个UIImageView.我想测试是否在每个单元格中,imageView的图像是否匹配正确的图像名称.
I have a collectionView that has a bunch of cells that contain a single UIImageView in each cell. I want to test wether or not in each cell, the imageView's image matches the correct image name.
在生产代码中,我向UIImageView示例添加了一个可访问性标识符:我的图像视图".我遍历包含图像名称的字符串数组,并根据索引设置单元格的图像,例如:["image0.png","image1.png","image2.png"],因此索引为0-2的单元格将分别有那些图像.
In the production code, I've added an accessibility identifier to the UIImageView example: "My Image View". I loop through an array of strings containing image names and set the cell's image in accordance to the index, example: ["image0.png", "image1.png", "image2.png"] so cells at index 0-2 would have those images respectively.
在我的XCUITest文件中,我正在尝试如下操作:
In my XCUITest file I'm trying something like this:
XCTAssert(cells.images["My Image View"].exists, "message here")
但是,这不会检查单元格的imageView是否具有正确的图像.谁能提出更好的解决方案?
But this doesn't check if the cell's imageView has the right image. Can anyone suggest a better solution?
我之前访问过的一些参考文献:
Some references I visited beforehand:
如何在iOS XCTest中测试UIImageView元素?
很抱歉,以前是否有人问过这个问题,我什么都找不到.
I apologize if this question has been asked before, I couldn't find anything.
我找到了解决方法.
let image = cells.element(matching: , identifier: )
我不知道identifier参数实际上使用的是图像名称,因此如果我传递图像名称,则可以断言该图像的存在.
I didn't know that the identifier parameter actually uses the image name so If I pass the image name, I can assert the existence of the image.
let image = cells.element(matching: .image, identifier: "myImage.png")
这有效.因此,当我遍历包含图像名称的字符串数组时,我还可以检查与图像索引对应的索引处的单元格是否正确.
this works. So when I loop through an array of strings containing the image name, I can also check if the cell at the index corresponding to the image index is correct.
我也忘记提及图像不是存储在资产中,而是通过json获取.
I also forgot to mention that the images aren't being stored in assets, but are being fetched via json.
cesarmarch在下面的回答是最接近的,因此我将其标记为正确.
cesarmarch's answer below was the closest so I marked that as correct.
推荐答案
我只是使用
XCTAssert(cells.images ["Image_Name_In_Resource_Directory" .exists,此处的消息")
检查当前图像是否很好用,并且工作正常.
to check if the current image is the good to use and it works fine.
这篇关于如何使用XCUITest测试UIImageView是否包含具有特定名称的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!