Ajax 正在等待WatiN中的文本更改
我正在尝试测试一个网页,该网页通过ajax调用来更新价格。 页面加载时会触发ajax调用,以更新初始为空的div 这是我用来等待div内部文本更改的扩展方法Ajax 正在等待WatiN中的文本更改,ajax,testing,watin,wait,Ajax,Testing,Watin,Wait,我正在尝试测试一个网页,该网页通过ajax调用来更新价格。 页面加载时会触发ajax调用,以更新初始为空的div 这是我用来等待div内部文本更改的扩展方法 public static void WaitForTextChange(this IE ie, string id) { string old = ie.Element(id).Text; ie.Element(id).WaitUntil(!Find.ByText(old)); } 然而,它并没有暂停,即使当我在等待之后
public static void WaitForTextChange(this IE ie, string id)
{
string old = ie.Element(id).Text;
ie.Element(id).WaitUntil(!Find.ByText(old));
}
然而,它并没有暂停,即使当我在等待之后写出旧值和ie.Element(id).Text时,它们都是空的。我无法调试,因为这会导致暂停
Find.ByText是否不能处理空值,或者我是否出错
有人有类似的代码吗?在深入研究了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;
}
}
它假定在更改之前会读取旧值,因此,如果在启动更新后使用它太长时间,则有可能出现争用情况,但实际上它对我有效。。我遇到了另一个问题。有时,我正在更新的价格不会改变,在这种情况下,我希望超时,然后使用.WaitUntil(newtextconstraint(old,true),2000)继续执行下一个用户操作;只是被卡住了。
public static void WaitForTextChange(this IE ie, Element element)
{
string old = element.Text;
element.WaitUntil(new TextConstraint(old, true));
}