ajax - 等待 WatiN 中的文本更改

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

然而,即使我在等待之后写出旧值和 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;
    }
}

以及更新后的扩展方法:

public static void WaitForTextChange(this IE ie, Element element)
{
    string old = element.Text;
    element.WaitUntil(new TextConstraint(old, true));
}

它假定旧值将在更改之前被读取,因此如果您在启动更新后使用它的时间过长,则有可能出现竞争条件,但它对我有用。

关于ajax - 等待 WatiN 中的文本更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1604780/

相关文章:

javascript - 在 gulp-watch 上运行可执行文件

docker - 如何使用 Testcafe 在 alpine docker 镜像中运行 Electron?

c# - 类似于 Watin 的 Silverlight 自动化

PHPUnit 检查方法返回类型

testing - 如何在 testcafe 命令行中提供嵌套文件夹路径?

c# - 从 WatiN 中选择不更新 knockout View 模型

testing - 是否可以使用 Watin 查看页面加载后的响应 header 是什么

jquery - 让 ajax 根据其成功返回 JSON 或 HTML 是一种不好的做法吗?

javascript - 如果多个调用之一失败,则 JSONP 请求调用不成功

javascript - Ajax Complete 在成功准备好之前被触发,如何防止这种行为?