首页 > 解决方案 > 无法使用 appium 和 c# 在 ionic5 应用程序中检测选择器元素

问题描述

using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Remote;
using System;
using System.Threading;
using Xunit;


namespace XUnitTestProject2
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            Thread.Sleep(3000);
            var driver = InitiliseDriver();
            
            //Login Page
            driver.FindElement(By.Id("username")).SendKeys("TestUser");
            driver.FindElement(MobileBy.Id("password")).SendKeys("newpassword");
            driver.FindElement(MobileBy.Id("91268f5d-d21e-4ef5-9886-c1d19f2799a7")).Click();
        }
        private RemoteWebDriver InitiliseDriver()
        {
            //Set the capabilities
            DesiredCapabilities cap = new DesiredCapabilities();
            cap.SetCapability("platformName", "Android");
            cap.SetCapability("platformVersion", "9.0");
            cap.SetCapability("app", "C:\\Users\\jamesa\\Downloads\\app-debug.apk");
            //cap.SetCapability("automationName", "uiautomator2");
            return new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), cap);          
        }
    }
}

这是我得到的会话详细信息我确实有选择器 ID,但服务器没有选择它

注意: - 我能够成功启动应用程序,但服务器无法选择元素。- 应用程序以 Ionic 方式构建(Ionic 5)

标签: c#seleniumselenium-webdriverappiumappium-android

解决方案


您需要将驱动程序上下文设置为 WEBVIEW,ionic 使用 webview(非本机)构建应用程序,您也可能希望在调试模式下构建应用程序。以下是有关使用 appium 测试混合应用程序的更多详细信息。

在您的情况下,您需要在执行测试之前添加类似的内容

driver.context("WEBVIEW");

推荐阅读