简体   繁体   中英

Programmatically Pressing Buttons on a Web Page

There is a webpage on our intranet which resides at http://server1/rsyncwebgui.php which provides a quick way for us to trigger an rSync between two file shares. For uninteresting security reasons, this is the route we have to take.

The web page looks like this:

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>Rsync web gui</title>
</head>
<body>
  <script language="javascript">
    window.onload = function () { setTimeout(submitForm, 10000); }
    function submitForm() { document.getElementById('myform').submit(); }
  </script>
<h3> Execute rsync between server2 and server3</h3>
<form id="myform" method="POST">
  <input type="hidden" name="resync" value="false">
  <input type="hidden" name="scanscheduled" value="false">
  <input type="submit" value="Start sync">
</form>
<p>
  <script language="javascript">
    window.onload = function () {
      if (confirm('Are you sure to start the sync?')) {
  var formobj = document.getElementById('myform');
  formobj.elements['resync'].value = 'true';
  formobj.submit();
      }
    }
  </script>
</p>
<p><a href="/rsyncwebgui.php">Refresh page</a></p>

</body></html>

When a user clicks on the button, an "OK/Cancel" dialog pops up asking them to confirm. When OK is clicked, the page posts back and the rsync is triggered.

How would I drive this interaction from a remote, C# console application?

Can't check in VS now, but you will end with something like this :

string postDataStr = string.Format("resync=true&scanscheduled=&otherpara=xyz");
byte[] postData = Encoding.ASCII.GetBytes(postDataStr);

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("http://server1/rsyncwebgui.php"));
req.Method= "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postData.Length;
using(var reqStream = req.GetRequestStream())
{
    reqStream.Write(postData, 0, postData.Length);
}

HttpWebResponse response = (HttpWebResponse )req.GetResponse();

Use an automation library like Waitn or Selenium . Both are free, very similar APIs and both have .Net libraries.

They are for web-testing, but work as well for any web-automation (I submit and revise eBay auctions using them:)).

Both have drivers for multiple browsers.

+1 for WatiN or Selenium which HiTech Magic called out. You could also give Telerik's free testing framework a try. (Disclosure: I'm the evangelist for Test Studio, so I'm a bit biased...)

You could also use Watir, which would let you create a simple Ruby script to invoke from the command line. I use Ruby and Watir for all sorts of similar situations like this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM