ASP.NET Web Application using Watin

user3277259

I'm experimenting with Watin in Visual Studio 2012 (C#), but I cannot seem to get anything to work. Currently I have two text boxes in an aspx page (txtbox1 and txtbox2). I am trying to have Watin automatically input numbers into these two text boxes and then click on the "Calculate" button which will add the two numbers together. I installed the Watin framework and added the Watin.Core dll as a reference. Here is my code for so far in my .aspx page:

using WatiN.Core;

[STAThread]  
protected void Page_Load(object sender, EventArgs e)
{
    IE ie = new IE("http://localhost:5243/Addition.aspx");

    ie.TextField(Find.ByName("txtbox1")).TypeText("1");
    ie.TextField(Find.ByName("txtbox2")).TypeText("2");
    ie.Button(Find.ByValue("Calculate")).Click();
}

I keep getting the error message stating:"The CurrentThread needs to have it's ApartmentState set to ApartmentState.STA to be able to automate Internet Explorer". Do you know what can cause this? Thanks for any help in advance!

noseratio

First of all, you're trying to automate Internet Explorer from the server-side ASP.NET code. This is generally a bad idea. This article describes the implications of doing this with Office, the same concerns apply to Internet Explorer.

That said, to succeed with what you're trying to do, you'd need to run an STA thread on the server side, and run your Watin code inside that thread. Placing [STAThread] on your ASP.NET Page_Load handler won't automagically make this happen.

Here is how it can be done, but again, doing so on the server is not recommended:

protected void Page_Load(object sender, EventArgs e)
{
    RunOnStaThread<object>(() =>
    {
        IE ie = new IE("http://localhost:5243/Addition.aspx");

        ie.TextField(Find.ByName("txtbox1")).TypeText("1");
        ie.TextField(Find.ByName("txtbox2")).TypeText("2");
        ie.Button(Find.ByValue("Calculate")).Click();

        return null;
    });
}

static T RunOnStaThread<T>(Func<T> func)
{
    var tcs = new TaskCompletionSource<T>();

    var thread = new Thread(() => 
    {
        try
        {
            tcs.SetResult(func());
        }
        catch (Exception ex)
        {
            tcs.SetException(ex);
        }
    });

    thread.IsBackground = true;
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();

    try 
    {
        return tcs.Task.Result;
    }
    finally
    {
        thread.Join();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using R in asp.net web application

From Dev

ASP.NET web service using IUSR, not Application Pool Identity

From Dev

ASP.net Web Forms Application login using external DB

From Dev

ASP.NET web service using IUSR, not Application Pool Identity

From Java

ASP.NET Web Site or ASP.NET Web Application?

From Dev

“ASP.NET web application” and unmanaged DLL

From Dev

Pure asp.net Web API application

From Dev

Implementing admin login ASP .Net Web application

From Dev

ASP.NET Web Application Slowness

From Dev

ASP NET 5 Web Application Deployment on IIS

From Dev

How to deploy ASP.net web application

From Dev

Random number in URL of ASP .NET web application

From Dev

Asp.net web application and sharepoint list

From Dev

Horizontal scroll using shift + scroll in asp.net web application's Gridview?

From Dev

Using Web API inside a ASP.NET 5 MVC 6 Application

From Dev

Using ELMAH inside my asp.net mvc 4 & MVC5 web application

From Dev

are there any drawbacks or risks of using Parallel.Foreach with WebClient() inside my asp.net mvc web application

From Dev

How to save file using ng-file-upload in ASP .NET Web API 2 application?

From Dev

How should I share TSQL scripts in an ASP.NET MVC 4 web application project using (localDB)?

From Dev

asp.net - Web API 2 Singe Page Application - Using built in Login form doesn't authorize?

From Dev

Wiring up ASP.NET Web Application using Forms Authentication with SqlMembership Provider on Azure

From Dev

UI thread is being blocked when using async/await with Task in ASP.NET web application

From Dev

How to return list of files using ASP.NET Web API to Angular Application

From Dev

Using ftp upload to site C# ASP.NET web form application

From Dev

Wiring up ASP.NET Web Application using Forms Authentication with SqlMembership Provider on Azure

From Dev

Uploading files to Web Application in a sub-dirctory using ASP.NET MVC

From Dev

Steps for profiling SQL related operations in ASP.Net Web application using Mini profiler

From Dev

Trying to insert data into SQL Server database using asp.net web application, getting an error

From Dev

How can i show captured fingerprint in web application using asp.net C#

Related Related

  1. 1

    Using R in asp.net web application

  2. 2

    ASP.NET web service using IUSR, not Application Pool Identity

  3. 3

    ASP.net Web Forms Application login using external DB

  4. 4

    ASP.NET web service using IUSR, not Application Pool Identity

  5. 5

    ASP.NET Web Site or ASP.NET Web Application?

  6. 6

    “ASP.NET web application” and unmanaged DLL

  7. 7

    Pure asp.net Web API application

  8. 8

    Implementing admin login ASP .Net Web application

  9. 9

    ASP.NET Web Application Slowness

  10. 10

    ASP NET 5 Web Application Deployment on IIS

  11. 11

    How to deploy ASP.net web application

  12. 12

    Random number in URL of ASP .NET web application

  13. 13

    Asp.net web application and sharepoint list

  14. 14

    Horizontal scroll using shift + scroll in asp.net web application's Gridview?

  15. 15

    Using Web API inside a ASP.NET 5 MVC 6 Application

  16. 16

    Using ELMAH inside my asp.net mvc 4 & MVC5 web application

  17. 17

    are there any drawbacks or risks of using Parallel.Foreach with WebClient() inside my asp.net mvc web application

  18. 18

    How to save file using ng-file-upload in ASP .NET Web API 2 application?

  19. 19

    How should I share TSQL scripts in an ASP.NET MVC 4 web application project using (localDB)?

  20. 20

    asp.net - Web API 2 Singe Page Application - Using built in Login form doesn't authorize?

  21. 21

    Wiring up ASP.NET Web Application using Forms Authentication with SqlMembership Provider on Azure

  22. 22

    UI thread is being blocked when using async/await with Task in ASP.NET web application

  23. 23

    How to return list of files using ASP.NET Web API to Angular Application

  24. 24

    Using ftp upload to site C# ASP.NET web form application

  25. 25

    Wiring up ASP.NET Web Application using Forms Authentication with SqlMembership Provider on Azure

  26. 26

    Uploading files to Web Application in a sub-dirctory using ASP.NET MVC

  27. 27

    Steps for profiling SQL related operations in ASP.Net Web application using Mini profiler

  28. 28

    Trying to insert data into SQL Server database using asp.net web application, getting an error

  29. 29

    How can i show captured fingerprint in web application using asp.net C#

HotTag

Archive