繁体   English   中英

C#Webdriver FindsBy-在页面对象框架类的ID变量中将具有相似文本的FindsBy语句组合

[英]C# Webdriver FindsBy - Combining FindsBy Statements with similar text in ID variable in a Page Object Framework Class

我已经改编了在C#中创建的页面对象框架,该框架使用Selenium PageObjects库查找页面元素。

在我的程序中,我有一个带有FindsBy语句的类,该语句查找ID为“ ngp_total_records”的文本字段(请参见下文)

[FindsBy(How = How.Id, Using = "ngp_total_records")]
public IWebElement txtNGPTotalRecords { get; set; }

我也有一段代码可以找到相似的文本字段,但是id不同(请参见下文)

[FindsBy(How = How.Id, Using = "ngp_usi_total_records")]
public IWebElement txtNGPUSITotalRecords { get; set; }

问题:有没有一种方法可以将上面的FindsBy语句组合为一个,以便每次执行程序时都可以找到“ ngp_total_records” id和“ ngp_usi_total_records” id?

好。 我想我明白您的要求。 如果使用产品类型1,则元素1是可见的。 如果使用产品类型2,则元素2是可见的。 是? 希望? 在这种情况下,我会这样做...

由于ID不同,您仍然需要两个元素。 我将两个“ finder”元素设为私有,然后有一个public元素,该元素代表给定产品可用的元素。 在页面的构造函数中,您将检查是否存在哪个元素并将其分配给public元素

[FindsBy(How = How.Id, Using = "ngp_total_records")]
private IWebElement txtNGPTotalRecords { get; set; }

[FindsBy(How = How.Id, Using = "ngp_usi_total_records")]
private IWebElement txtNGPUSITotalRecords { get; set; }

// no FindsBy here... this gets set in its getter
public IWebElement txtTotalRecords;

txtTotalRecords的getter中,将其设置为存在的任何元素。 我认为您可能会遇到的一个问题是,当元素不存在时,PageFactory会发生什么? 它会抛出还是继续前进? 我不知道,因为我不使用PageFactory。


编辑1:根据JimEvans修正值更新了答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM