I'm doing UI testing using Watin (Watir, for java folks). I need to check that an element is not present in the HTML. As of today, I do this as follows:
[FindBy(Id = "pnConfirmation")]
protected Div Confirmation;
public bool ConfirmationMessageDisplayed
{
get
开发者_StackOverflow中文版{
try
{
return Confirmation.Text != "";
}
catch (ElementNotFoundException)
{
return false;
}
}
}
But this takes an awful lot of time. Is there a more effective way to do this?
Every time you call Confirmation.Text
WatiN waits until element exists. After that time ElementNotFoundException
is thrown. By default WatiN waits 30 seconds for element to show. This could be changed by setting value of Settings.WaitUntilExistsTimeOut
.
To solve your problem you can do several things. For example, you can change this line:
return Confirmation.Text != "";
to
return Confirmation.Exists && Confirmation.Text != "";
But you have to remember that this will return false
even if this element will appear after 1 second. If you want to use that solution, I think that you don't have to catch this exception if you are sure, that once it's present, it will not be deleted.
You can of course change the value of Settings.WaitUntilExistsTimeOut
. If you don't want to change this value, but you want to wait only a little, you can replace your getter with this code:
try
{
Confirmation.WaitUntilExists(1); //Wait only one second
return Confirmation.Text != "";
}
catch (WatiN.Core.Exceptions.TimeoutException) //Different exception!
{
return false;
}
精彩评论