【发布时间】:2021-03-24 00:30:24
【问题描述】:
我正在尝试创建一个能够缓存属性值的自定义属性,该属性的类型是 IWebElement,创建起来有点昂贵,并且可能引发 NoSuchElement 异常。
我已经像这样实现了一些成功:
protected Lazy<IWebElement> lazyWebElement;
protected virtual IWebElement cachedWebElement
{
get
{
if (!lazyWebElement.IsValueCreated)
{
try
{
lazyWebElement = new Lazy<IWebElement>(() => driver.FindElement(By.Id("someElement")),
LazyThreadSafetyMode.PublicationOnly);
}
catch (NoSuchElementException)
{
throw new NoSuchElementException("someElement is not present in DOM");
}
}
return lazyModal.Value;
}
}
而我想做的是
[CachedWebElement]
protected virtual IWebElement cachedWebElement => driver.FindElement(By.Id("someElement"));
但Attribute 不允许在其构造函数中使用复杂类型,因此我无法将driver.FindElement(By.Id("someElement")) 作为参数传递。
如果我在派生类中覆盖该属性,那么将其缓存起来会很棒,因为现在我正在使用支持字段来保存值,并且整个机制都会丢失。
谢谢。
【问题讨论】:
-
你读过
CacheLookup属性和PageObject工厂吗? (developers.perfectomobile.com/plugins/servlet/…) -
是的,但是
PageObject已经被弃用,并且不会在 Selenium 4 jimevansmusic.blogspot.com/2018/03/… 上继续使用,这就是我尝试重新创建它的想法的地方 -
如果它已被弃用,我想开发人员正在用一些东西替换它?
标签: c# selenium custom-attributes