WAIN 中是否有在每次页面加载时执行的页面检查器?

发布于 2024-10-26 07:25:45 字数 126 浏览 3 评论 0原文

我正在使用 WATIR 和 WATIN 编写自动化脚本。 Watir 有一种称为页面检查器的东西,它是在每次页面加载时运行的代码片段。 WAIN 有类似的东西吗?我希望在每次页面加载时运行一段代码。通常,这用于检查页面错误或页面加载图像。

I am writing automation scripts using WATIR and WATIN. Watir has something called page checkers, which are code snippets that run on each page load. Is there something similar in WATIN ? I want a piece of code to run on each page load. Generally this is used to check for page errors or page loading images.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏有森光若流苏 2024-11-02 07:25:45

判断页面何时加载并不是那么容易。我很快用谷歌搜索了您提到的 Watir 中的页面检查器,并找到了 一篇关于 Watir 中页面检查器的文章。请参阅文章下面的第一条评论。 AFAIK 在 WatiN 中确实很相似。

不幸的是,我在 WatiN 中没有看到任何类似的功能(在内部调用 WaitForComplete 后不会触发任何事件。您可以做的最简单的事情就是子类化,例如 IE 类:

class MyIE : IE
{
    public MyIE(string url) : base(url) { } //TODO: add constructors

    public override void WaitForComplete(int waitForCompleteTimeOut)
    {
        base.WaitForComplete(waitForCompleteTimeOut);
        Console.WriteLine("Page has been loaded");
    }
}

但是,情况与所提到的评论中描述的类似(运行比仅页面加载更频繁),

我认为更好的方法是使用 Page 类 。 WatiN 库。 watin.org 网页示例:

var ie = new MyIE("http://watin.org/");

var homePage = ie.Page<HomePage>();
Console.WriteLine(homePage.FirstFeature);
homePage.DocumentationLink.Click();

var documentationPage = ie.Page<DocumentationPage>();
Console.WriteLine(documentationPage.FAQLink.Url);

要运行该代码,您需要以下类:

abstract class WatiNBasePage : Page
{
    [FindBy(Id = "header")]
    public Div HeaderDiv { get; set; }

    public Link HomeLink { get { return HeaderDiv.Link(Find.ByText("Home")); } }

    public Link DocumentationLink { get { return HeaderDiv.Link(Find.ByText("Documentation")); } }

    protected override void InitializeContents()
    {
        base.InitializeContents();
        VerifyDocumentProperties(UnverifiedDocument, errorMessage => { throw new Exception(errorMessage); }); //TODO: modify if needed
    }

    protected override void VerifyDocumentProperties(Document document, Page.ErrorReporter errorReporter)
    {
        base.VerifyDocumentProperties(document, errorReporter);
        if (!HomeLink.Exists)
            errorReporter("HomeLink not exists");
        //TODO: more checks here
    }
}

class HomePage : WatiNBasePage
{
    [FindBy(Id = "features")]
    public Table FeatureTable { get; set; }

    public string FirstFeature { get { return FeatureTable.Span(Find.First()).Text; } }
}

class DocumentationPage : WatiNBasePage
{
    [FindBy(Text = "Frequently Asked Questions")]
    public Link FAQLink { get; set; }
}

基本上,您需要实现 VerifyDocumentProperties 上面的代码将检查是否 HomeLink。 > 存在,但也许您想检查 DocumentationLink 是否存在等。第二件事是修改对 VerifyDocumentProperties 的调用。现在,如果验证失败,Exception<。 /code> 将在调用 ie.Page() (其中 T 是 WatinBaseClass 的子类)后抛出

,即使您不需要使用“页面检查器” 。 ,使用 Page 类仍然非常有用并且可以澄清代码,所以我真的推荐使用它。我很遗憾在开始与 WatiN 合作时没有发现它。

It is not really that easy to tell when page loads. I quickly googled about that page checkers in Watir, that you mentioned and found an article about page checkers in Watir. See first comment bellow the article. AFAIK it's really similar in WatiN.

Unfortunately, I don't see any similar functionality in WatiN (no event is fired after internal call to WaitForComplete. The easiest thing you could do is to subclass eg. IE class:

class MyIE : IE
{
    public MyIE(string url) : base(url) { } //TODO: add constructors

    public override void WaitForComplete(int waitForCompleteTimeOut)
    {
        base.WaitForComplete(waitForCompleteTimeOut);
        Console.WriteLine("Page has been loaded");
    }
}

However, the situation will be similar to described in mentioned comment (runs a lot more regularly than just page load).

I think that better approach would be using Page class from WatiN library. It is well documented. Example for watin.org webpage:

var ie = new MyIE("http://watin.org/");

var homePage = ie.Page<HomePage>();
Console.WriteLine(homePage.FirstFeature);
homePage.DocumentationLink.Click();

var documentationPage = ie.Page<DocumentationPage>();
Console.WriteLine(documentationPage.FAQLink.Url);

To run that code you need following classes:

abstract class WatiNBasePage : Page
{
    [FindBy(Id = "header")]
    public Div HeaderDiv { get; set; }

    public Link HomeLink { get { return HeaderDiv.Link(Find.ByText("Home")); } }

    public Link DocumentationLink { get { return HeaderDiv.Link(Find.ByText("Documentation")); } }

    protected override void InitializeContents()
    {
        base.InitializeContents();
        VerifyDocumentProperties(UnverifiedDocument, errorMessage => { throw new Exception(errorMessage); }); //TODO: modify if needed
    }

    protected override void VerifyDocumentProperties(Document document, Page.ErrorReporter errorReporter)
    {
        base.VerifyDocumentProperties(document, errorReporter);
        if (!HomeLink.Exists)
            errorReporter("HomeLink not exists");
        //TODO: more checks here
    }
}

class HomePage : WatiNBasePage
{
    [FindBy(Id = "features")]
    public Table FeatureTable { get; set; }

    public string FirstFeature { get { return FeatureTable.Span(Find.First()).Text; } }
}

class DocumentationPage : WatiNBasePage
{
    [FindBy(Text = "Frequently Asked Questions")]
    public Link FAQLink { get; set; }
}

Basically you need to implement VerifyDocumentProperties. Above code will check if HomeLink exists, but maybe you would like to check if DocumentationLink exists etc. The second thing is to modify call to VerifyDocumentProperties. Now, if verification fails, Exception will be thrown after calling ie.Page<T>() (where T is a subclass of WatinBaseClass).

In my opinion, even if you don't need to use "page checkers", using Page class is still really useful and clarifies the code, so I really recommend using it. I regret that I haven't discovered it when I was starting work with WatiN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文