Skip to content

Instantly share code, notes, and snippets.

@AlexP11223
Created November 11, 2013 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexP11223/7409349 to your computer and use it in GitHub Desktop.
Save AlexP11223/7409349 to your computer and use it in GitHub Desktop.
using System;
using Awesomium.Core;
namespace AwesomiumNewTest
{
public static class WebViewHelper
{
public static string GetJsSingleXpathString(string xpath)
{
return
String.Format(
"document.evaluate(\"{0}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue", xpath);
}
public static string GetJsXpathString(string xpath)
{
return
String.Format(
"document.evaluate(\"{0}\", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)", xpath);
}
}
class Google
{
WebView m_view = null;
bool m_running = true;
bool m_finishedLoad = false;
public Google()
{
m_view = WebCore.CreateWebView(1024, 768);
m_view.LoadingFrameComplete += onFinishLoading;
m_view.Source = "http://www.google.com".ToUri();
}
public void update()
{
try
{
while (m_running)
{
WebCore.Update();
if (m_finishedLoad)
{
screenShot();
runCommands();
}
System.Threading.Thread.Sleep(1000);
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format("Error on step {0}: {1}", mNr, ex.Message));
}
}
static int mNr = 1;
public void runCommands()
{
switch (mNr)
{
case 1:
{
string searchTboxJsXpath =
WebViewHelper.GetJsSingleXpathString(String.Format("//input[@name='{0}']", "q"));
dynamic searchTbox = (JSObject) m_view.ExecuteJavascriptWithResult(searchTboxJsXpath);
if (searchTbox == null)
throw new Exception("Search textbox not found.");
searchTbox.value = "test";
break;
}
case 2:
{
string searchTboxJsXpath =
WebViewHelper.GetJsSingleXpathString(String.Format("//input[@name='{0}']", "q"));
dynamic searchTbox = (JSObject)m_view.ExecuteJavascriptWithResult(searchTboxJsXpath);
if (searchTbox == null)
throw new Exception("Search textbox not found.");
dynamic form = searchTbox.form;
form.submit();
m_finishedLoad = false;
break;
}
default:
{
m_running = false;
break;
}
}
mNr++;
}
public void screenShot()
{
string fName = string.Format("test{0}.jpg", mNr.ToString());
BitmapSurface bs = (BitmapSurface)m_view.Surface;
bs.SaveToJPEG(fName);
}
public void onFinishLoading(object sender, FrameEventArgs e)
{
if (e.IsMainFrame)
m_finishedLoad = true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment