-->

how to wait until the textbox enable in watin

2019-03-06 00:47发布

问题:

i have one textbox on my page on the load event textbox is disable for 10 then its enable so how to wait for 10 sec in watin. i am try to this code

IE ie = new IE("http://localhost:2034/WebForm3.aspx");
         ie.ShowWindow(WatiN.Core.Native.Windows.NativeMethods.WindowShowStyle.Maximize);
         ie.TextField("TextBox1").TypeText("Fer");

but it gives the error that TextBox1 is disable so i want to wait for some time. so how to do this? please help me?

回答1:

You can just sleep the thread for a fixed amount of time in order for the test to wait:

            System.Threading.Thread.Sleep(10000);

The time is in milliseconds. Maybe you would need to increase that number a little bit, but still this should work.



回答2:

If it is ran when the webpage loads you could probably just use this:

ie.WaitForComplete();

Wait for complete will make the test wait until all of the elements and frames are loaded before carrying out the next task, in your case locating the textbox for input.

The only problem I have with putting the thread to sleep is that it can take a bit longer than you may need. This way you know it is running as soon as the page is done. (Which may vary a bit).



回答3:

Two better ways than just using a long Thread.Sleep() to wait for fields to enable due to a script or asnyc call; eg Ajax. (Note: I'd be ashamed to admit how many long Thread.Sleeps() I have in my code.)

The below are both referencing a checkbox, but the same concepts should work just fine on a textbox or other control.

1) Check the disabled attribute.

myPage.myCheckbox.WaitUntil("disabled", false.ToString())

2) Poll the field, sleeping a short amount of time at each check. I did this poll pattern as the above simple one liner above didn't work; I don't remember why as it has been quite a while.

int sleepTime = 100;
int numberOfPolls = 50;

for (int i = 0; i < 50; i++)
{
    if (myPage.myCheckbox.Enabled == false)
    {
        Thread.Sleep(100);
    }
    else
    {
        break;
    }
}


回答4:

You could try this...

ie.Element("TextBox1").WaitUntil<Element>(element => element.Enabled);


标签: asp.net watin