I'm using the WatiN testing tool with Visual Studio 2005. When I try to select a value from my list box I am getting an "access denied" error.
问题:
回答1:
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();
回答2:
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.
回答3:
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{}