Skip to main content
Code Review

Return to Question

added 6 characters in body
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177

I want to get a result from the website when I submit a form. There are several things that can go wrong.

In a simple scenario I want to return:

I want to get a result from the website when I submit a form. There are several things that can go wrong. In a simple scenario I want to return:

I want to get a result from the website when I submit a form. There are several things that can go wrong.

In a simple scenario I want to return:

Post Reopened by Heslacher, Toby Speight, Sᴀᴍ Onᴇᴌᴀ , mdfst13, Stephen Rauch
Post Closed as "Not suitable for this site" by Heslacher, yuri, Graipher, pacmaninbw , IEatBagels

I want to get a result from the website when I submit a form. There are serval waysseveral things that can go wrong. In a simple scenario I want to return:

I did that ugly try, because of NulLReferenceExceptionNullReferenceException (if form wasis filled correctly, there iswould be no summary element, and whenif form is filled uncorrectlyincorrectly, there iswould be no popup element).

I want to get result from website when I submit a form. There are serval ways that can go wrong. In simple scenario I want to return:

I did that ugly try, because of NulLReferenceException (if form was filled correctly there is no summary element, and when form is filled uncorrectly there is no popup element).

I want to get a result from the website when I submit a form. There are several things that can go wrong. In a simple scenario I want to return:

I did that ugly try, because of NullReferenceException (if form is filled correctly, there would be no summary element, and if form is filled incorrectly, there would be no popup element).

Source Link
dafie
  • 299
  • 2
  • 8

Get result from form website operation

I want to get result from website when I submit a form. There are serval ways that can go wrong. In simple scenario I want to return:

true - when form was submitted

false - when form wasn't submitted

null - when we don't know if form was submitted or not

I did that ugly try, because of NulLReferenceException (if form was filled correctly there is no summary element, and when form is filled uncorrectly there is no popup element).

How can I make this more elegant?

public bool? GetResult(TimeSpan timeout)
{
 var deadline = DateTime.Now.Add(timeout);
 do
 {
 // check if success
 try
 {
 var success = new WebDriverWait(Driver, TimeSpan.FromSeconds(5))
 .Until(ExpectedConditions.TextToBePresentInElement(Driver.FindElement(By.Id("summary")), "Success!"));
 if (success) return true;
 }
 catch (Exception ex)
 { }
 // check if not enough data
 try
 {
 var notEnoughData = new WebDriverWait(Driver, TimeSpan.FromSeconds(5))
 .Until(ExpectedConditions.TextToBePresentInElement(Driver.FindElement(By.Id("popup")), "Not enough data"));
 if (notEnoughData) return false;
 }
 catch (Exception ex)
 { }
 // check if too much data
 try
 {
 var tooMuchData = new WebDriverWait(Driver, TimeSpan.FromSeconds(5))
 .Until(ExpectedConditions.TextToBePresentInElement(Driver.FindElement(By.Id("popup")), "Too much data"));
 if (tooMuchData) return false;
 }
 catch (Exception ex)
 { }
 // check if empty data
 try
 {
 var tooMuchData = new WebDriverWait(Driver, TimeSpan.FromSeconds(5))
 .Until(ExpectedConditions.TextToBePresentInElement(Driver.FindElement(By.Id("popup")), "Empty data"));
 if (tooMuchData) return false;
 }
 catch (Exception ex)
 { }
 } while (DateTime.Now > deadline);
 return null;
}
lang-cs

AltStyle によって変換されたページ (->オリジナル) /