在深入研究了 WatiN 的限制之后,我最终找到了自己的解决方案。
这是解决方案:
public class TextConstraint : Constraint
{
private readonly string _text;
private readonly bool _negate;
public TextConstraint(string text)
{
_text = text;
_negate = false;
}
public TextConstraint(string text, bool negate)
{
_text = text;
_negate = negate;
}
public override void WriteDescriptionTo(TextWriter writer)
{
writer.Write("Find text to{0} match {1}.", _negate ? " not" : "", _text);
}
protected override bool MatchesImpl(IAttributeBag attributeBag, ConstraintContext context)
{
return (attributeBag.GetAdapter<Element>().Text == _text) ^ _negate;
}
}
以及更新的扩展方法:
public static void WaitForTextChange(this IE ie, Element element)
{
string old = element.Text;
element.WaitUntil(new TextConstraint(old, true));
}
它假定旧值将在更改之前被读取,因此如果您在启动更新后使用它太长时间,则可能会出现竞争条件,但它适用于我。