【问题标题】:How to switch off/on network in xamarin android/iOS app from xamarin uitest in C#如何从 C# 中的 xamarin uitest 关闭/打开 xamarin android/iOS 应用程序中的网络
【发布时间】:2019-12-17 01:11:31
【问题描述】:

我正在用 c# 编写 xamarin uitest 来自动化 xamarin 移动应用程序。我有 apk 和 ipa 文件。我想在启动应用程序后切换网络连接。如何使用 uitest 打开/关闭网络。 在appium中我们可以使用togglewifi()来改变状态。在xamarin中有没有可用的方法?

【问题讨论】:

  • 在 SO 上发帖之前,请先研究您的问题。 This C# Corner 博客文章描述了如何在 xamarin 中打开/关闭 wifi(我在谷歌搜索几秒钟后发现)

标签: c# android xamarin ui-automation uitest


【解决方案1】:

我们无法在 Xamarin for iOS 的代码中打开/关闭 wifi。但是你可以在启动应用程序后打开设置页面。

DependencyService如果在Forms中调用就可以使用

在 iOS 中

using xxx.iOS;
using Foundation;
using UIKit;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.iOS
{
    public class OpenSettingsImplement : ISettingsService
    {
        public void OpenSettings()
        {
            var url = new NSUrl($"App-Prefs:");
            UIApplication.SharedApplication.OpenUrl(url);
        }
    }
}

在安卓中

using Android.Content;

using xxx;
using xxx.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenSettingsImplement))]
namespace xxx.Droid
{
    public class OpenSettingsImplement : ISettingsService
    {
        public void OpenSettings()
        {
            Intent intentOpenSettings = new Intent();
            intentOpenSettings.SetAction(Android.Provider.Settings.ActionAirplaneModeSettings);
            Android.App.Application.Context.StartActivity(intentOpenSettings);
        }
    }
}

在表单中,添加一个新界面

public interface ISettingsService
{
  void OpenSettings();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-28
    相关资源
    最近更新 更多