tags:

views:

723

answers:

3

Hi

I'm using WatiN testing tool using Visual studio 2005. When i'm trying to select a value from the list box i'm getting access denied error. plz suggest some solution.

+1  A: 

This is a bug in the select list where if the list is not ready to accept input, and it can throw one several exception types. We solve it like this:

try
{
    _domContainer.SelectList(_control.WatinAttribute).Focus();
    _domContainer.SelectList(_control.WatinAttribute).Select(value);
}

catch (Exception e)
{ 
    Console.WriteLine("Select list eception caught: " + e.Message + e.StackTrace);

    // we have tried once already and failed, so let's wait for half a second
    System.Threading.Thread.Sleep(500);
    _domContainer.SelectList(_control.WatinAttribute).Select(value);
}

And yes I know that swallowing all exceptions like this is normally bad, but if the exception occurs again, it is thrown to the test code and the test fails.

Bruce McLeod
+3  A: 

I have seen this a lot with select lists recently when using the WatiN 2.0 beta. Instead of using the aSelectList.Select(strText) option, it seems to work better when you do this:

ie.SelectList(Find.ById("MySelect")).Option(Find.ByText("Option 1")).Select();

This can also happen when changing an ASP.NET control that cause an auto-postback. The first change will register, but the next element you try to access will throw an "Access Denied" error because it is still trying to access the old page. In this case you can try using ie.WaitForComplete(), but sometimes this is required:

ie.SelectList(Find.ById("AutoPostBackSelect")).Option(Find.ByText("Option")).Select();
System.Threading.Thread.Sleep(200); //Sleep to make sure post back registers
ie.WaitForComplete();
ie.SelectList(Find.ById("MySelect")).Refresh()
ie.SelectList(Find.ById("MySelect")).Option(Find.ByText("Option 1")).Select();
Greg Bray
A: 

I noticed this happens if you try and select a value that is already selected.

You can work around this with a pre-check:

if(_sel_ddlPeriodFromDay.GetValue("value")!="1")
   _sel_ddlPeriodFromDay.SelectByValue("1");

or maybe use a try catch?

try{_sel_ddlPeriodFromDay.SelectByValue("1");}
catch{}