Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web automation using .NET

Tags:

c#

.net

I am a very newbie programmer. Does anyone of you know how to do Web automation with C#? Basically, I just want auto implement some simple action on the web. After I have opened up the web link, i just want to perform the actions below automatically.

  1. Automatically Input some value and Click on "Run" button.
  2. Check In the ComboBox and Click on "Download" button.

How can I do it with C#? My friend introduce me to use Powershell but I guess .Net do provide this kind of library too. Any suggestion or link for me to refer?

like image 426
DEN Avatar asked Mar 08 '11 02:03

DEN


People also ask

Can we use .NET in Selenium?

We can use Selenium for . NET applications. We should have Visual Studio 2019 installed in the system along with Selenium webdriver and any browser like Firefox, Chrome, and so on. Then we must utilize the NUnit framework.

Can you use C# for automation?

C# is useful for automation testing because it allows the automation test engineer to develop an application with the help of Visual Studio on the . Net framework. C# is another programming language that also supports the binding with Selenium. And this language binding will be updated along with the java program.

Can you use C# in Selenium?

Selenium is an open-source, web Automation Testing tool that supports multiple browsers and multiple operating systems. It allows testers to use multiple programming languages such as Java, C#, Python, . Net, Ruby, PHP, and Perl for coding automated tests.

What is the tool for web browser automation?

The most popular open source web automation tool is Selenium. If you insist on using an open source tool, it should be on top of your list.


2 Answers

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.

like image 105
Fun Mun Pieng Avatar answered Oct 17 '22 11:10

Fun Mun Pieng


If you want to simulate a real browser then WatiN will be a good fit for you. (Selenium is another alternative, but I do not recommend it for you).

If you want to work on the HTTP level, then use WebRequest and related classes.

like image 5
Dmytrii Nagirniak Avatar answered Oct 17 '22 13:10

Dmytrii Nagirniak