Xamarin.UITests - 在真实设备上测试 - iOS - 应用程序权限弹出问题 [英] Xamarin.UITests - testing on real device - iOS - app permissions popups issue

查看:17
本文介绍了Xamarin.UITests - 在真实设备上测试 - iOS - 应用程序权限弹出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iOS 应用需要一些权限(GPS、推送通知).当应用程序第一次启动时,iOS 会询问用户是否同意将这些权限授予应用程序.我已经编写了一些 UITests 并希望在本地连接的 iPhone 上自动运行它们.

问题是我无法覆盖权限问题并且我的测试失败.我发现 IDE (Xamarin Studio) 部署的应用程序会要求权限,但通过 UITests 部署的应用程序不会.所以我尝试了 .AppBundle(path_to_app) 但它说这仅对部署到模拟器有效.

<块引用>

SetUp:System.Exception:此应用程序包不适用于运行模拟器.要解决此问题,请确保您的目标设备是一个模拟器.DTPlatformName 是iphoneos",而不是iphonesimulator"应用程序 Info.plist.

就像它试图将 iPhone 应用程序部署到模拟器一样.但是 Xamarin Studio 中的 Target 设置为真实设备.我试图添加 .DeviceIdentifier.当与 .InstalledApp 一起使用时,它正在启动(仍在请求权限).但是当我使用 DeviceIdentifierAppBundle 时,出现了与上面相同的错误.

我的测试在 Test Cloud 上运行良好.它们在模拟器上运行良好.当我手动部署到设备、启动应用并批准权限然后运行 ​​UI 测试时,它们工作正常.

我无法实现的是让 UITests 覆盖真实设备上的权限问题.有人做过这个吗?

最后一件事是我发现在 AppBundle 方法的文档中将强制在模拟器上运行"https://developer.xamarin.com/api/member/Xamarin.UITest.Configuration.iOSAppConfigurator.AppBundle/p/System.String/

所以我可能注定要完成这项任务,但也许有人知道解决方法?

解决方案

您可以使用 InvokeUIA 关闭 UITest 的系统对话框.下面的测试通过点击 iOS 系统警报的确定"按钮进行:

[测试]public void AppLaunches (){app.Screenshot ("第一个屏幕.");app.InvokeUia("uia.query('[:view {:marked "OK"}]')");app.InvokeUia("uia.tapMark("OK")");}

一个有效的示例应用程序 &UITest 也在这里:https://github.com/King-of-Spades/InvokeUia-for-System-Dialogs

关于测试云中系统对话框的警告

您在 Test Cloud 中没有看到此问题的原因是因为 Test Cloud 会自动解除系统警报;所以通常你不必担心它.但是,如果您的警报启动得太快;以便它在自动化完全启动您的应用程序之前出现,然后它将无法检测到 &解除警报并导致您的测试失败.

因此,您要确保在 Test Cloud 中运行您的应用程序时,权限请求被延迟,或者如果特定测试没有明确需要它们,您甚至可以停用它们.此 Calabash 指南中提供了更多信息:https://github.com/calabash/calabash-ios/wiki/Managing-Privacy-Alerts%3A--Location-Services%2C-APNS%2C-Contacts>

(即使是 Calabash,您也可以在 UITest 中使用相同的策略;尽管使用的是 C# 语法.)

Xcode 8/iOS 10 更新

Xcode 8/iOS 10 删除了 UIAutomation,因此只有在您使用 Xcode 7 和 iOS 7-9 时才能继续使用 InvokeUIA 解决方法.参考资料:

I've iOS app that needs some privileges (GPS, Push notifications). When app starts for a first time iOS asks user if they're ok with granting those permissions to application. I've written some UITests and want to automate running them on locally connected iPhone.

The problem is that I cannot override permissions questions and my tests fails. I found out that application deployed by IDE (Xamarin Studio) will ask for permissions, but application deployed via UITests will not. So I tried with .AppBundle(path_to_app) but it says this is only valid for deploying to Simulator.

SetUp : System.Exception : This app bundle is not valid for running on a simulator. To fix this issue please ensure that your target device is a simulator. DTPlatformName is 'iphoneos', not 'iphonesimulator' in the apps Info.plist.

Like it's trying to deploy iPhone app to Simulator. But Target in Xamarin Studio is set to real device. I tried to add .DeviceIdentifier. When Used with .InstalledApp it was starting up (still asking for permissions). But when I used DeviceIdentifier and AppBundle there was the same error as above.

My tests works fine on Test Cloud. They work fine on Simulator. They work fine when I deploy to device manually, start app and approve permissions then run UI tests.

What I cannot achieve is to make UITests override permissions questions on real device. Anyone made this work?

Last thing is that I found is in documentation for AppBundle method "Will force a run on simulator" https://developer.xamarin.com/api/member/Xamarin.UITest.Configuration.iOSAppConfigurator.AppBundle/p/System.String/

So I may be doomed with the task but maybe someone knows a workaround?

解决方案

You can dismiss system dialogs with UITest by using InvokeUIA. The test below works by tapping the "OK" button of an iOS system alert:

[Test]
public void AppLaunches ()
{
    app.Screenshot ("First screen.");
    app.InvokeUia ("uia.query('[:view {:marked "OK"}]')"); 
    app.InvokeUia ("uia.tapMark("OK")"); 
}

A working sample app & UITest is also here: https://github.com/King-of-Spades/InvokeUia-for-System-Dialogs

Warning about system dialogs in Test Cloud

The reason that you don't see this issue in Test Cloud is because Test Cloud automatically dismisses the system alerts; so usually you don't have to worry about it. However, if your alert launches too soon; so that it appears before the automation has fully started your app, then it will be unable to detect & dismiss the alert and cause your test to fail.

So you want to make sure that when running your app in Test Cloud that the request for permissions are delayed, or you can even deactivate them if they aren't explicitly needed for a particular test. More information is available in this Calabash guide: https://github.com/calabash/calabash-ios/wiki/Managing-Privacy-Alerts%3A--Location-Services%2C-APNS%2C-Contacts

(Even though it's Calabash, you can use the same strategy in UITest; albeit with a C# syntax.)

Update for Xcode 8 / iOS 10

Xcode 8 / iOS 10 removed UIAutomation, so the InvokeUIA workaround will only continue to be possible if you're using Xcode 7 and iOS 7-9. References:

这篇关于Xamarin.UITests - 在真实设备上测试 - iOS - 应用程序权限弹出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆