Access denied error ( Visual Studio and WatiN )

最后都变了- 提交于 2019-11-30 23:46:50

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();

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.

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{}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!