Skip to content

Instantly share code, notes, and snippets.

@gazmartina
Forked from robertkhrona/AutomationSample.cs
Created January 20, 2016 15:34
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 gazmartina/d4060d3c7e15a016c6bf to your computer and use it in GitHub Desktop.
Save gazmartina/d4060d3c7e15a016c6bf to your computer and use it in GitHub Desktop.
A simple example of web automation using Awesomium.
using System;
using System.Collections.Generic;
using System.Linq;
using AwesomiumSharp;
using System.Threading;
namespace Automation
{
class AutomationSample
{
WebView webView;
bool running = true;
public bool finishedLoading = false;
public AutomationSample()
{
// Setup WebCore
WebCore.Config conf = new WebCore.Config();
WebCore.Initialize(conf);
// Setup WebView
webView = WebCore.CreateWebview(1024, 768);
// Setup Callbacks
webView.OnFinishLoading += onFinishLoading;
webView.OnBeginLoading += onBeginLoading;
Console.WriteLine("Loading Form...");
webView.LoadURL("http://form.jotform.com/form/11023750372");
}
public void update()
{
while(running == true)
{
WebCore.Update();
if(finishedLoading == true)
{
runCommands();
}
Console.WriteLine("Waiting...");
Thread.Sleep(4000); // Wait 4 seconds between each command
}
}
static int commandNumber = 1;
public void runCommands()
{
switch(commandNumber)
{
case 1:
{
Console.WriteLine("Typing Username...");
webView.ExecuteJavascript("document.getElementById('input_3').value='Username'");
break;
}
case 2:
{
Console.WriteLine("Typing Password...");
webView.ExecuteJavascript("document.getElementById('input_4').value='Password'");
break;
}
case 3:
{
Console.WriteLine("Typing First Name...");
click(410,150);
typeKeys("John");
break;
}
case 4:
{
Console.WriteLine("Typing Last Name...");
click(410,197);
typeKeys("Doe");
break;
}
case 5:
{
Console.WriteLine("Selecting Gender as Male...");
webView.ExecuteJavascript("document.getElementById('input_7_0').checked=true");
break;
}
case 6:
{
Console.WriteLine("Selecting 'Search Engine' in combo box...");
webView.ExecuteJavascript("document.getElementById('input_8').value='Search Engine'");
break;
}
default:
{
webView.Render().SaveToPNG("result.png", true);
System.Diagnostics.Process.Start("result.png");
Console.WriteLine("Done running all commands, shutting down webcore...");
WebCore.Shutdown();
running = false;
break;
}
}
commandNumber++;
}
public void click(int x, int y)
{
webView.InjectMouseMove(x,y);
webView.InjectMouseDown(MouseButton.Left);
webView.InjectMouseMove(x,y);
webView.InjectMouseUp(MouseButton.Left);
}
public void typeKeys(string key)
{
for(int i=0; i < key.Length; i++){
WebKeyboardEvent keyEvent = new WebKeyboardEvent();
keyEvent.type = WebKeyType.KeyDown;
keyEvent.virtualKeyCode = key[i];
webView.InjectKeyboardEvent(keyEvent);
keyEvent.type = WebKeyType.Char;
keyEvent.text = new ushort[] { key[i], 0, 0, 0 };
webView.InjectKeyboardEvent(keyEvent);
keyEvent.type = WebKeyType.KeyUp;
keyEvent.virtualKeyCode = key[i];
webView.InjectKeyboardEvent(keyEvent);
}
}
public void onFinishLoading(object sender, WebView.FinishLoadingEventArgs e)
{
finishedLoading = true;
}
public void onBeginLoading(object sender, WebView.BeginLoadingEventArgs e)
{
finishedLoading = false;
}
public static void Main(string[] args)
{
AutomationSample main = new AutomationSample();
main.update();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment