【问题标题】:How to find the current context in which Appium is running?如何找到 Appium 正在运行的当前上下文?
【发布时间】:2018-11-08 01:09:20
【问题描述】:

我正在使用 Appium 自动化混合应用程序。 下面的代码可用于在 Java 中找到设置适当的上下文。

String context = driver.getContext();

C#中的等价代码是什么?

【问题讨论】:

    标签: c# selenium-webdriver appium appium-android


    【解决方案1】:

    如果您使用 IDE,您应该能够看到驱动程序对象可用的所有命令。

    在 IDE 之外查看所有已定义命令的地方是:https://github.com/appium/appium-dotnet-driver/blob/master/appium-dotnet-driver/Appium/AppiumDriverCommand.cs#L179

    driver.GetContext()
    

    【讨论】:

    • 我正在使用 Visual Studio。但我看不到 GetContext。我需要使用哪些软件包?
    • 您需要包含我在此处链接的 appium-dotnet-driver 包。它包括 Appium 特定的 GetContext 命令。在 Selenium 包中找不到 GetContext,因为上下文只能移动。
    【解决方案2】:

    在 C# 中没有像 Java 那样称为 getContext() 的方法,相反,您可以使用名为 Context 的属性来获取当前上下文并通过将上下文(要切换的内容)值分配给该属性来切换到另一个上下文。

        public ReadOnlyCollection<string> Contexts { get; }
        public string Context { get; set; }
    

    例如:如果要获取当前上下文:

    string s = driver.Context;
    

    如果你想切换到 webView 你可以试试这个

    driver.Context="WEBVIEW_<PackageName>";
    

    如果您需要获取当前视图中的所有可用上下文,请使用“上下文”属性

    public List<string> GetContexts()
        {
            List<string> AllContexts= new List<string>();
            foreach (var context in (driver.Contexts))
            {
                AllContexts.Add(context);
            }
            return AllContexts;
        }
    

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2021-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多