如何使用watin来测试Ajax网站?

发布于 2024-10-02 06:13:48 字数 1904 浏览 1 评论 0原文

//使用watin测试一些ajax网站,当点击td时,div("appendProdctTreeDiv")将显示一些响应html,但我不知道如何获取这个div的innerhtml并点击我想要的链接; 可以通过测试“http://www.google.com/ig”给出一些例子吗?先谢谢了!


--------使用watin--------
IE ie = new IE();
....
ie.Element(Find.ById("tree")).Click();
textbox1.text=ie.Div("appendProdctTreeDiv").innerHtml;//这种方式是错误的;
...


..html 代码..

<'TD id=tree onclick=showAppendProductTree() noWrap>tab1<'/TD>

....

<'DIV 样式="宽度:100%;高度:96%;溢出:自动" id=appendProdctTreeDiv returned="false"><'/DIV>
....


function showAppendProductTree(){

showTreeTab(3);

if(document.getElementById("appendProdctTreeDiv").loaded == "false" ){

    var url ="product!changeAppendProduct.do";

var params = "";

var newProductId = document.getElementById("newProductId").value;
    new Ajax.Request(
    url,
    {
        method: 'get',
        parameters: params+"&random="+Math.random(),
        requestHeaders:["Cache-Control","no-cache"],
        onComplete: function(originalRequest){  
        var message = originalRequest.responseText;
        document.getElementById("appendProdctTreeDiv").innerHTML= message;
        document.getElementById("appendProdctTreeDiv").loaded = "true";
                    synAppendTree();
        document.getElementById("waitLoadAppendProd").style.display = "none";
        document.getElementById("searchDiv").style.display = "block";
            }
    });

}

}

function showTreeTab(tabId) {

document.getElementById("treeDiv").style.display ="block";

for(var i=1;i<=3;i++){

    if(i==tabId){
        document.getElementById("tree"+i).style.display = "block";
    }
    else{
        document.getElementById("tree"+i).style.display = "none";
    }
}

}

//using watin test some ajax sites,when click td,div("appendProdctTreeDiv") will show some response html,but i don't know how to get this div innerhtml and click link whick i wanna;
can give some examples by test “http://www.google.com/ig”?thanks in advance!

------using watin--------
IE ie = new IE();
....
ie.Element(Find.ById("tree")).Click();
textbox1.text=ie.Div("appendProdctTreeDiv").innerHtml;// this way is wrong;
...


..html code..

<'TD id=tree onclick=showAppendProductTree() noWrap>tab1<'/TD>

....

<'DIV style="WIDTH: 100%; HEIGHT: 96%; OVERFLOW: auto" id=appendProdctTreeDiv loaded="false"><'/DIV>

....


function showAppendProductTree(){

showTreeTab(3);

if(document.getElementById("appendProdctTreeDiv").loaded == "false" ){

    var url ="product!changeAppendProduct.do";

var params = "";

var newProductId = document.getElementById("newProductId").value;
    new Ajax.Request(
    url,
    {
        method: 'get',
        parameters: params+"&random="+Math.random(),
        requestHeaders:["Cache-Control","no-cache"],
        onComplete: function(originalRequest){  
        var message = originalRequest.responseText;
        document.getElementById("appendProdctTreeDiv").innerHTML= message;
        document.getElementById("appendProdctTreeDiv").loaded = "true";
                    synAppendTree();
        document.getElementById("waitLoadAppendProd").style.display = "none";
        document.getElementById("searchDiv").style.display = "block";
            }
    });

}

}

function showTreeTab(tabId) {

document.getElementById("treeDiv").style.display ="block";

for(var i=1;i<=3;i++){

    if(i==tabId){
        document.getElementById("tree"+i).style.display = "block";
    }
    else{
        document.getElementById("tree"+i).style.display = "none";
    }
}

}

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

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

发布评论

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

评论(2

不回头走下去 2024-10-09 06:13:48

我认为您有几种选择来等待回复。选项 1:

  • 首先,WatiN 默认情况下会等待 30 秒,以便元素显示在页面上。因此,如果您知道要单击的链接的 ID 或文本,我希望以下代码能够工作:

    ie.Div("appendProdctTreeDiv").Link(Find.ByText("链接的文本")).Click();

如果没有,这里是选项2:

  • 在触发ajax调用的脚本中,在oncomplete回调函数中,div的loaded属性被设置为true(在您包含的html中为false)。您可以在等待调用中使用它,然后获取innerhtml或单击您想要单击的链接:

    ie.Div("appendProdctTreeDiv").WaitUntil("loaded", "true");

哈特哈,
杰罗恩

I think you have several options to wait for the response. Option 1:

  • First of all, WatiN by default waits 30 seconds for an element to show up on the page. So if you know the id or text of the link you want to click on I would expect the following code to work:

    ie.Div("appendProdctTreeDiv").Link(Find.ByText("text of the link")).Click();

If it doesn't, here is option 2:

  • In the script firing the ajax call, in the oncomplete callback function the loaded attribute of the div gets set to true (is false in the html you included). You can use this in a wait until call and then get the innerhtml or click the link you want to click:

    ie.Div("appendProdctTreeDiv").WaitUntil("loaded", "true");

HTH,
Jeroen

↙温凉少女 2024-10-09 06:13:48

当您获取innerHtml 时,异步AJAX 调用可能仍在进行中。

<块引用>

ie.Element(Find.ById("tree")).Click();

textbox1.text=ie.Div("appendProdctTreeDiv").innerHtml;//这种方式是错误的;

尝试这样的事情,看看最终是否会得到预期的结果。这不是最好的方法,但检查很容易。

<块引用>

ie.Element(Find.ById("tree")).Click();

System.Threading.Thread.Sleep(5000); //等待5秒;如果您的异步调用需要更长的时间,请超过 5 秒。

textbox1.text=ie.Div("appendProdctTreeDiv").innerHtml;//这种方式是错误的;

如果站点使用 ASP.NET,要确定页面是否处于异步调用中,您可以评估以下 JavaScript。

<块引用>

Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()

调用如下

<块引用>

bool isInAsync = bool.Parse(ie.Eval("Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()"));

然而,google.com/ig 确实使用 MS AJAX,所以我无法使用上面的 javascript 提出一个工作示例。

使用 wait 和 google.com/ig 的翻译小工具的示例。每次运行时在 TypeText 中放入不同的单词或清除缓存。
即.GoTo(“www.google.com/ig”);
系统.Threading.Thread.Sleep(2000); //等待页面小工具加载
Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);

        ie.Frame("remote_iframe_10").Form("dictForm").TextField("dictQuery").TypeText("bajar");
        ie.Frame("remote_iframe_10").Form("dictForm").Buttons[0].Click();

        Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);
        System.Threading.Thread.Sleep(2000);  //Wait for gadget to finish here.
        Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);

It is likely that the asynchronous AJAX call is still in progress when you are getting the innerHtml.

ie.Element(Find.ById("tree")).Click();

textbox1.text=ie.Div("appendProdctTreeDiv").innerHtml;// this way is wrong;

Try something like this to see if you end up with expected results. This is the not so great way to do it, but checking is easy.

ie.Element(Find.ById("tree")).Click();

System.Threading.Thread.Sleep(5000); //Waits for 5 seconds; make more than 5 seconds if your async call takes longer.

textbox1.text=ie.Div("appendProdctTreeDiv").innerHtml;// this way is wrong;

If the site is using ASP.NET, to determine if the page is in an async call you can eval the following javascript.

Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()

Call like

bool isInAsync = bool.Parse(ie.Eval("Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()"));

However, google.com/ig does using MS AJAX so I cannot come up with a working example using the above javascript.

Example using wait and google.com/ig's translation gadget. Put in a different word into TypeText on each run or clear your cache.
ie.GoTo("www.google.com/ig");
System.Threading.Thread.Sleep(2000); //Wait for page gadgets to load
Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);

        ie.Frame("remote_iframe_10").Form("dictForm").TextField("dictQuery").TypeText("bajar");
        ie.Frame("remote_iframe_10").Form("dictForm").Buttons[0].Click();

        Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);
        System.Threading.Thread.Sleep(2000);  //Wait for gadget to finish here.
        Console.WriteLine(ie.Frame("remote_iframe_10").Div("dict_content").InnerHtml);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文