c# - Selenium:FindsBy 与集合

标签 c# selenium testing readonly-collection

我是测试初学者,有一个问题。如何正确使用ReadOnlyCollection<IWebElement>如果我使用属性 FindsBy 。开始测试后我的集合始终为空。这是我的 C# 代码:

        [FindsBy(How = How.Name, Using = "role")]
        public ReadOnlyCollection<IWebElement> radPercentage { get; }

这里是测试网站:http://testwisely.com/demo/survey

我想做这样的事情:radPercentage[2].Click();

最佳答案

在使用集合之前,您需要调用InitElements。传递驱动程序和包含 FindsBy 属性的类的实例(在我的代码中为“this”)。

IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://testwisely.com/demo/survey");
PageFactory.InitElements(driver, this);
IWebElement radio = this.radPercentage[2];

InitElements 方法期望属性的类型为 IWebElementIWebElementIList

[FindsBy(How = How.Name, Using = "role")]
public IList<IWebElement> radPercentage;

关于c# - Selenium:FindsBy 与集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38079410/

相关文章:

安卓 : testing a service

ruby-on-rails - 在 rails 中测试嵌套路由

android - 如何自动化一个没有任何 iOS 平台编写的 UI 的库?

javascript - Selenium:执行 javascript 命令时测试卡住 "return document.readyState"

javascript - Jest 在第一次失败后停止测试套件

c# - 这个静态字典字段线程安全吗?

使用 Homebrew cask 安装时,Selenium WebDriver 无法找到 Firefox

c# - struct INotifyPropertyChanged 不适用于 ctor

c# - 如何将 ContextMenuStrip 添加到 ToolStripButton?

c# - 使用 Entity Framework 5 Code-First 和 POCO 实现 N 层数据层?