是否可以以编程方式填写Ajax表单?

时间:2021-10-18 12:15:04

I'm doing some automation work and can make my way around a site & post to HTML forms okay, but now I'm up against a new challenge, Ajax forms.

我正在做一些自动化工作,可以绕过一个站点并发布到HTML表单,但是现在我遇到了一个新的挑战,Ajax表单。

Since there's no source to read, I'm left wondering if it's possible to fill in an Ajax form progamatically, in C#. I'm currently using a non-visible axWebBrowser.

由于没有可供阅读的资源,我想知道是否可以在C#中以progamatically方式填写Ajax表单。我目前正在使用不可见的axWebBrowser。

Thanks in advance for your help!

在此先感谢您的帮助!

2 个解决方案

#1


Yes, but I recommend using a different approach to requesting/responding to the server pages including the regular pages, and the AJAX handler pages.

是的,但我建议使用不同的方法来请求/响应服务器页面,包括常规页面和AJAX处理程序页面。

In c#, try using the WebRequest/WebResponse or the more specialized HttpWebRequest/HttpWebResponse classes.

在c#中,尝试使用WebRequest / WebResponse或更专业的HttpWebRequest / HttpWebResponse类。

Ajax is no more than a "fancy" name for a technology that allows Javascript to make HTTP requests to a server which usually implements some handlers that produce specialized, light-weight content for the Javascript caller (comonly encoded as JSON).

对于允许Javascript向服务器发出HTTP请求的技术,Ajax只不过是一个“花哨”的名称,该服务器通常实现一些处理程序,这些处理程序为Javascript调用程序生成专门的轻量级内容(通常编码为JSON)。

Therefore in order to simulate AJAX calls, all you have to do is inspect your target application (the web page that you want to "post" to) and see what format is used for the AJAX communications - then replicate the page's Javascript behavior from C# using the WebREquest/WebResponse classes.

因此,为了模拟AJAX调用,您所要做的就是检查目标应用程序(您要“发布”到的网页)并查看用于AJAX通信的格式 - 然后从C#复制页面的Javascript行为使用WebREquest / WebResponse类。

See Firebug - a great tool that allows you to inspect a web page to determine what calls it makes, to which pages and what those pages respond. It does a pretty good job at inspecting AJAX calls too.

请参阅Firebug - 一个很棒的工具,它允许您检查网页以确定它所做的调用,哪些页面以及这些页面响应的内容。它在检查AJAX调用方面做得非常好。

Here's a very simple example of how to do a web request:

以下是如何执行Web请求的一个非常简单的示例:

HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://www.mysite.com");

using (HttpWebResponse resp = (HttpWebResponse)wReq.GetResponse())
{
   // NOTE: A better approach would be to use the encoding returned by the server in
   // the Response headers (I'm using UTF 8 for brevity)
   using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
   {
       string content = sr.ReadToEnd();
       // Do something with the content
   }
}    

A POST is also a request, but with a different method. See this page for an example of how to do a very simple post.

POST也是一个请求,但使用不同的方法。有关如何执行非常简单的帖子的示例,请参阅此页面。

EDIT - Details on Inspecting the page behavior with Firebug

编辑 - 有关使用Firebug检查页面行为的详细信息

What I mean by inspecting the page you're trying to replicate is to use a tool (I use Firebug - on Firefox) to determine the flow of information between the page and the server.

通过检查您尝试复制的页面,我的意思是使用工具(我在Firefox上使用Firebug)来确定页面和服务器之间的信息流。

With Firebug, you can do this by using the "Net" and "Console" panels. The Net panel lists all requests executed by the browser while loading the page. While the "Console" will list communications between the page and the server that take place after the page has loaded. Those communications that take place after the page has loaded are essentially the AJAX calls that you'll want to replicate (Note: Network monitoring has to be enbled in Firebug for this to work)

使用Firebug,您可以使用“Net”和“Console”面板执行此操作。 “Net”面板列出了加载页面时浏览器执行的所有请求。而“控制台”将列出页面加载后发生的页面与服务器之间的通信。页面加载后发生的那些通信本质上是您要复制的AJAX调用(注意:必须在Firebug中进行网络监视才能使其工作)

Check out Michael Sync's tutorial to learn more about Firebug and experiment with the Console panel to learn more about the AJAX requests.

查看Michael Sync的教程,了解有关Firebug的更多信息,并使用Console面板进行实验,以了解有关AJAX请求的更多信息。

Regarding "replicate the page's behavior from C# using the WebRequest/WebResponse" - what you have to realize is that like I said earlier, the Javascript AJAX call is nothing more than an HTTP Request. It's an HTTP Request that the Javacript makes "behind the scenes", or out-of-band, to the web server. To replicate this, it is really no different than replicating a normal GET or a normal POST like I showed above. And this is where Firebug comes in to play. Using it you can view the requests, as the Javascript makes them - look at the Console panel, and see what the Request message looks like.

关于“使用WebRequest / WebResponse从C#复制页面的行为” - 您必须意识到的是,正如我之前所说,Javascript AJAX调用只不过是HTTP请求。这是Javacript在Web服务器“幕后”或带外进行的HTTP请求。要复制它,它与复制正常的GET或正常的POST没有什么不同,就像我上面所示。这就是Firebug的用武之地。使用它可以查看请求,就像Javascript生成它们一样 - 查看Console面板,看看Request消息是什么样的。

Then you can use the same technique as above, using the HttpWebRequest/HttpWebResponse to make the same type of request as the Javascript does, only do it from C# instead.

然后你可以使用与上面相同的技术,使用HttpWebRequest / HttpWebResponse来生成与Javascript相同类型的请求,只能从C#中执行。

Gregg, I hope this clarifies my answer a little bit but beyond this I suggest playing with Firebug and maybe learning more about how the HTTP protocol works and how AJAX works as a technology.

Gregg,我希望这能澄清我的答案,但除此之外,我建议玩Firebug并且可能更多地了解HTTP协议的工作方式以及AJAX如何作为一种技术。

#2


Have you looked at using Selenium. AFAIK, you can write the test cases in C# and I know our testers have successfully used it before to UI Test a Ajax enabled ASP.NET site

你看过使用Selenium吗? AFAIK,您可以在C#中编写测试用例,我知道我们的测试人员在使用UI测试启用了Ajax的ASP.NET站点之前已成功使用它

http://seleniumhq.org/

#1


Yes, but I recommend using a different approach to requesting/responding to the server pages including the regular pages, and the AJAX handler pages.

是的,但我建议使用不同的方法来请求/响应服务器页面,包括常规页面和AJAX处理程序页面。

In c#, try using the WebRequest/WebResponse or the more specialized HttpWebRequest/HttpWebResponse classes.

在c#中,尝试使用WebRequest / WebResponse或更专业的HttpWebRequest / HttpWebResponse类。

Ajax is no more than a "fancy" name for a technology that allows Javascript to make HTTP requests to a server which usually implements some handlers that produce specialized, light-weight content for the Javascript caller (comonly encoded as JSON).

对于允许Javascript向服务器发出HTTP请求的技术,Ajax只不过是一个“花哨”的名称,该服务器通常实现一些处理程序,这些处理程序为Javascript调用程序生成专门的轻量级内容(通常编码为JSON)。

Therefore in order to simulate AJAX calls, all you have to do is inspect your target application (the web page that you want to "post" to) and see what format is used for the AJAX communications - then replicate the page's Javascript behavior from C# using the WebREquest/WebResponse classes.

因此,为了模拟AJAX调用,您所要做的就是检查目标应用程序(您要“发布”到的网页)并查看用于AJAX通信的格式 - 然后从C#复制页面的Javascript行为使用WebREquest / WebResponse类。

See Firebug - a great tool that allows you to inspect a web page to determine what calls it makes, to which pages and what those pages respond. It does a pretty good job at inspecting AJAX calls too.

请参阅Firebug - 一个很棒的工具,它允许您检查网页以确定它所做的调用,哪些页面以及这些页面响应的内容。它在检查AJAX调用方面做得非常好。

Here's a very simple example of how to do a web request:

以下是如何执行Web请求的一个非常简单的示例:

HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://www.mysite.com");

using (HttpWebResponse resp = (HttpWebResponse)wReq.GetResponse())
{
   // NOTE: A better approach would be to use the encoding returned by the server in
   // the Response headers (I'm using UTF 8 for brevity)
   using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
   {
       string content = sr.ReadToEnd();
       // Do something with the content
   }
}    

A POST is also a request, but with a different method. See this page for an example of how to do a very simple post.

POST也是一个请求,但使用不同的方法。有关如何执行非常简单的帖子的示例,请参阅此页面。

EDIT - Details on Inspecting the page behavior with Firebug

编辑 - 有关使用Firebug检查页面行为的详细信息

What I mean by inspecting the page you're trying to replicate is to use a tool (I use Firebug - on Firefox) to determine the flow of information between the page and the server.

通过检查您尝试复制的页面,我的意思是使用工具(我在Firefox上使用Firebug)来确定页面和服务器之间的信息流。

With Firebug, you can do this by using the "Net" and "Console" panels. The Net panel lists all requests executed by the browser while loading the page. While the "Console" will list communications between the page and the server that take place after the page has loaded. Those communications that take place after the page has loaded are essentially the AJAX calls that you'll want to replicate (Note: Network monitoring has to be enbled in Firebug for this to work)

使用Firebug,您可以使用“Net”和“Console”面板执行此操作。 “Net”面板列出了加载页面时浏览器执行的所有请求。而“控制台”将列出页面加载后发生的页面与服务器之间的通信。页面加载后发生的那些通信本质上是您要复制的AJAX调用(注意:必须在Firebug中进行网络监视才能使其工作)

Check out Michael Sync's tutorial to learn more about Firebug and experiment with the Console panel to learn more about the AJAX requests.

查看Michael Sync的教程,了解有关Firebug的更多信息,并使用Console面板进行实验,以了解有关AJAX请求的更多信息。

Regarding "replicate the page's behavior from C# using the WebRequest/WebResponse" - what you have to realize is that like I said earlier, the Javascript AJAX call is nothing more than an HTTP Request. It's an HTTP Request that the Javacript makes "behind the scenes", or out-of-band, to the web server. To replicate this, it is really no different than replicating a normal GET or a normal POST like I showed above. And this is where Firebug comes in to play. Using it you can view the requests, as the Javascript makes them - look at the Console panel, and see what the Request message looks like.

关于“使用WebRequest / WebResponse从C#复制页面的行为” - 您必须意识到的是,正如我之前所说,Javascript AJAX调用只不过是HTTP请求。这是Javacript在Web服务器“幕后”或带外进行的HTTP请求。要复制它,它与复制正常的GET或正常的POST没有什么不同,就像我上面所示。这就是Firebug的用武之地。使用它可以查看请求,就像Javascript生成它们一样 - 查看Console面板,看看Request消息是什么样的。

Then you can use the same technique as above, using the HttpWebRequest/HttpWebResponse to make the same type of request as the Javascript does, only do it from C# instead.

然后你可以使用与上面相同的技术,使用HttpWebRequest / HttpWebResponse来生成与Javascript相同类型的请求,只能从C#中执行。

Gregg, I hope this clarifies my answer a little bit but beyond this I suggest playing with Firebug and maybe learning more about how the HTTP protocol works and how AJAX works as a technology.

Gregg,我希望这能澄清我的答案,但除此之外,我建议玩Firebug并且可能更多地了解HTTP协议的工作方式以及AJAX如何作为一种技术。

#2


Have you looked at using Selenium. AFAIK, you can write the test cases in C# and I know our testers have successfully used it before to UI Test a Ajax enabled ASP.NET site

你看过使用Selenium吗? AFAIK,您可以在C#中编写测试用例,我知道我们的测试人员在使用UI测试启用了Ajax的ASP.NET站点之前已成功使用它

http://seleniumhq.org/