I have been tasked with finding a solution to quite a novel issue. I have a variety of httpclient calls that I have to make in order to authenticate against a 3rd party vendor. However, part of this process involves dynmaically generated values being created in javascript and passed to a form, which is then posted to the 3rd party. As I'm using the httpclient class, I cannot obviously generate/run the javascript and thus the process comes to a halt right here (the posting of these values creates an important authentication cookie for an intermediate step).
So, I'd like to be able to take this simple html, which contains a form and some javascript, and have my c# code evaluate this and then retrieve the values that javscript has assigned to the form. I'd then use these values and continue with the workflow processes.
I could take a clunky route and use the webbrowser control. However, as this is being used in a non visual environment, I'd like to be able pass the html string into some sort of emulator and receive the parsed html back as a return. Below is an example of the simple html that I'd be dealing with:
<html>
<head>
<script type="text/javascript">
function testLoad() {
document.forms[0].elements[0].value = "some guid id plus the date:" + Date.getDate + 'some random js value';
document.forms[0].elements[1].value = decodeURIComponent(document.forms[0].elements[1].value);
document.forms[0].elements[2].value = decodeURIComponent(document.forms[0].elements[2].value);
// optionally submit -or just get the returned form values and post from htmlclient
document.forms[0].submit();
}</script>
<noscript>Please enable JavaScript to view the page content.</noscript>
</head>
<body onload="testLoad()">
<form method="POST" action="/" />
<input type="hidden" name="test_id" value="idstuff" />
<input type="hidden" name="test_123" value="encoded value" />
<input type="hidden" name="test_another" value="1.01" />
</form>
</body>
</html>
Once the html has been returned from the emulated process, I'd then use HtmlAgilityPack to grab the form values that have been populated by the javascript function (testLoad()) and progress to the next steps.
Am I aiming too high here, or has this bridge been crossed a few times. I've looked at http://wiki.awesomium.com, csExWB, jint and a few others, but none seem to take the really simple approach that I'm hoping for here. Think of my quest as being able to use the initial html as a parameter and have the emlulator return the patched html.
Hope the above is clear - I am wishing to evaluate the html/js from a serverside process and then move onto the next process within my c# workflow!.
[edit] - this looks VERY promising: http://www.tomdupont.net/2013/08/phantomjs-headless-browser-for-net-webdriver.html. I've taken the tips here and am using PhantomJs with Selenium... so far, so good!!
[oh and just to point out, this is not for any sinister use, the 3rd party in question just doesn't yet have a b2b api in place to permit the interop that we require between us]