1

I am trying to test a web page that has ajax calls to update a price. An ajax call is fired on page load to update an initially empty div.

This is the extension method I'm using to wait for a change in the inner text of the div.

public static void WaitForTextChange(this IE ie, string id)
{
    string old = ie.Element(id).Text;
    ie.Element(id).WaitUntil(!Find.ByText(old));
}

However it's not pausing even though when I write out the old value and ie.Element(id).Text after the wait, they are both null. I can't debug as this acts as a pause.

Can the Find.ByText not handle nulls or have I got something wrong.

Has anyone got some code working similar to this?

4

1 に答える 1

2

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));
}

変更前に古い値が読み取られることを前提としているため、更新を開始してから長時間使用すると競合状態が発生する可能性がわずかにありますが、私にとってはうまくいきます。

于 2009-10-22T05:03:32.457 に答える