Web Automation from C++

Web automation from C++

You should look into PhantomJS (a headless WebKit browser), which comes with GhostDriver, which is the WebDriver protocol implementation for PhantomJS.

You will still need to use one of the WebDriver language bindings, which I'm not aware of any of the language bindings that are in C++, but perhaps one of the available languages could be used by your team for automation purposes.

Worst case, you could always create your WebDriver script in Python, and call the Python script from your C++ application.

Web automation using C# WebBrowser

The WebBrowser class is not designed for this, hence why you are coming up with your problems.

You need to look into a tool that is designed for web automation.

Since you are using C#, Selenium has a wonderful set of C# bindings, and it can solve your problems because you'll be to use different locators (locating an element by a CSS selector or XPath specifically).

http://docs.seleniumhq.org/

Is There Any Client Side Web Automation

PhantomJS worked without requiring admin rights, but it is already paused, as per their official page, so the only hack to my mind is, ask networking staff to install visual studio (with C# setup) on all PCs, this way I can just hand over the apps to all and they can open and run from VS, instead of direct exe, and it will eliminate requiring updating of apps as users will just need to get the latest app folder. A down side is un necessary window of VS and the memory but the benefits would outweigh this issue.

Web automation using .NET

You can use the System.Windows.Forms.WebBrowser control (MSDN Documentation). For testing, it allows your to do the things that could be done in a browser. It easily executes JavaScript without any additional effort. If something went wrong, you will be able to visually see the state that the site is in.

example:

private void buttonStart_Click(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate("http://www.wikipedia.org/");
}

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement search = webBrowser1.Document.GetElementById("searchInput");
if(search != null)
{
search.SetAttribute("value", "Superman");
foreach(HtmlElement ele in search.Parent.Children)
{
if (ele.TagName.ToLower() == "input" && ele.Name.ToLower() == "go")
{
ele.InvokeMember("click");
break;
}
}
}
}

To answer your question: how to check a checkbox

for the HTML:

<input type="checkbox" id="testCheck"></input>

the code:

search = webBrowser1.Document.GetElementById("testCheck");
if (search != null)
search.SetAttribute("checked", "true");

actually, the specific "how to" depends greatly on what is the actual HTML.


For handling your multi-threaded problem:

private delegate void StartTestHandler(string url);
private void StartTest(string url)
{
if (InvokeRequired)
Invoke(new StartTestHandler(StartTest), url);
else
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate(url);
}
}

InvokeRequired, checks whether the current thread is the UI thread (actually, the thread that the form was created in). If it is not, then it will try to run StartTest in the required thread.

Automating Internet Explorer using C/C++

I strongly recommend the Microsoft Visual C++ compiler COM support.

Generally it works like this:

#import "c:\path\to\typelib.tlb"

#import "c:\path\to\library.dll"

#import "c:\path\to\program.exe"

This then makes it very easy to use Internet Explorer or any other COM object from C++.

See here for documentation:

  • http://msdn.microsoft.com/en-us/library/h31ekh7e.aspx

Browser Automation using C# - Write & Read

You can use Selenium with C# to do this, not sure if HtmlAgilityPack supports UI interacting.



Related Topics



Leave a reply



Submit