gpt4 book ai didi

c# - 当我使用页面对象模式时元素为空

转载 作者:行者123 更新时间:2023-11-28 21:35:43 26 4
gpt4 key购买 nike

我是移动自动化的新手,我面临着页面对象模式的问题。当我尝试使用 FindElementById 查找元素时,一切正常,这是我的 pop 类:

public class SamplePage
{
private AndroidDriver<AndroidElement> _driver;

[FindsByAndroidUIAutomator(ID = "com.miui.calculator:id/btn_1_s")]
private readonly AndroidElement _buttonOne;

[FindsByAndroidUIAutomator(ID = "android:id/button1")]
private readonly AndroidElement _confirmButton;

public SamplePage(AndroidDriver<AndroidElement> driver)
{
_driver = driver;
PageFactory.InitElements(_driver, this);
}

public void ClickOnConfirmButton()
{
//AndroidElement _confirmButton = _driver.FindElementById("android:id/button1");
_confirmButton.Click();
}

public void ClickOnButtonOne()
{
//AndroidElement _buttonOne = _driver.FindElementById("com.miui.calculator:id/btn_1_s");
_buttonOne.Click();

}

}

这是主类

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
using System;
using AppiumDotNetSamples.Helper;

namespace AppiumDotNetSamples
{
[TestFixture()]
public class AndroidBasicInteractionsTest
{
private AndroidDriver<AndroidElement> driver;
private SamplePage _samplePage;

[SetUp()]
public void BeforeAll()
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(MobileCapabilityType.PlatformName, "Android");
capabilities.SetCapability(MobileCapabilityType.PlatformVersion, "7.1.2");
capabilities.SetCapability(MobileCapabilityType.AutomationName, "UIAutomator2");
capabilities.SetCapability(MobileCapabilityType.DeviceName, "3e52f2ee7d34");
capabilities.SetCapability("appPackage", "com.miui.calculator");
capabilities.SetCapability("appActivity", "com.miui.calculator.cal.CalculatorActivity");

driver = new AndroidDriver<AndroidElement>(new Uri("http://localhost:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(180));
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

_samplePage = new SamplePage(driver);
}

[Test()]
public void Click()
{
_samplePage.ClickOnConfirmButton();
_samplePage.ClickOnButtonOne();
}

[TearDown()]
public void AfterAll()
{
driver.Quit();
}

}
}

我做错了什么?我在小米计算器应用上进行了测试,但早些时候我在谷歌计算器等任何其他应用上遇到了同样的问题。

最佳答案

ClickConfirmButton 方法未返回驱动程序,因此它为 null。

你可能想尝试类似的东西,看看它是否有效

      public AboutPage goToAboutPage()
{
about.Click();
return new AboutPage(driver);
}

关于c# - 当我使用页面对象模式时元素为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59006429/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com