"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/dotnet/test/common/FormHandlingTests.cs" (17 Feb 2023, 16270 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
The requested HTML page contains a <FORM> tag that is unusable on "Fossies" in "automatic" (rendered) mode so that page is shown as HTML source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file. See also the last
Fossies "Diffs" side-by-side code changes report for "FormHandlingTests.cs":
4.6.0_vs_4.7.0.
1 using System;
2 using NUnit.Framework;
3 using OpenQA.Selenium.Environment;
4
5 namespace OpenQA.Selenium
6 {
7 [TestFixture]
8 public class FormHandlingTests : DriverTestFixture
9 {
10 [Test]
11 public void ShouldClickOnSubmitInputElements()
12 {
13 driver.Url = formsPage;
14 driver.FindElement(By.Id("submitButton")).Click();
15 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
16 Assert.AreEqual(driver.Title, "We Arrive Here");
17 }
18
19 [Test]
20 public void ClickingOnUnclickableElementsDoesNothing()
21 {
22 driver.Url = formsPage;
23 driver.FindElement(By.XPath("//body")).Click();
24 }
25
26 [Test]
27 public void ShouldBeAbleToClickImageButtons()
28 {
29 driver.Url = formsPage;
30 driver.FindElement(By.Id("imageButton")).Click();
31 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
32 Assert.AreEqual(driver.Title, "We Arrive Here");
33 }
34
35 [Test]
36 public void ShouldBeAbleToSubmitForms()
37 {
38 driver.Url = formsPage;
39 driver.FindElement(By.Name("login")).Submit();
40 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
41 Assert.AreEqual(driver.Title, "We Arrive Here");
42 }
43
44 [Test]
45 public void ShouldSubmitAFormWhenAnyInputElementWithinThatFormIsSubmitted()
46 {
47 driver.Url = formsPage;
48 driver.FindElement(By.Id("checky")).Submit();
49 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
50 Assert.AreEqual(driver.Title, "We Arrive Here");
51 }
52
53 [Test]
54 public void ShouldSubmitAFormWhenAnyElementWithinThatFormIsSubmitted()
55 {
56 driver.Url = formsPage;
57 driver.FindElement(By.XPath("//form/p")).Submit();
58 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
59 Assert.AreEqual(driver.Title, "We Arrive Here");
60 }
61
62 [Test]
63 public void ShouldSubmitAFormWithIdSubmit()
64 {
65 driver.Url = formsPage;
66 driver.FindElement(By.Id("submit")).Submit();
67 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
68 Assert.AreEqual(driver.Title, "We Arrive Here");
69 }
70
71 [Test]
72 [IgnoreBrowser(Browser.IE, "Does not work")]
73 public void ShouldSubmitAFormWithNameSubmit()
74 {
75 driver.Url = formsPage;
76 driver.FindElement(By.Name("submit")).Submit();
77 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
78 Assert.AreEqual(driver.Title, "We Arrive Here");
79 }
80
81 [Test]
82 public void ShouldNotBeAbleToSubmitAnInputOutsideAForm()
83 {
84 driver.Url = formsPage;
85 Assert.That(() => driver.FindElement(By.Name("SearchableText")).Submit(), Throws.InstanceOf<WebDriverException>());
86 }
87
88 [Test]
89 public void ShouldBeAbleToEnterTextIntoATextAreaBySettingItsValue()
90 {
91 driver.Url = javascriptPage;
92 IWebElement textarea = driver.FindElement(By.Id("keyUpArea"));
93 string cheesey = "Brie and cheddar";
94 textarea.SendKeys(cheesey);
95 Assert.AreEqual(textarea.GetAttribute("value"), cheesey);
96 }
97
98 [Test]
99 public void SendKeysKeepsCapitalization()
100 {
101 driver.Url = javascriptPage;
102 IWebElement textarea = driver.FindElement(By.Id("keyUpArea"));
103 string cheesey = "BrIe And CheDdar";
104 textarea.SendKeys(cheesey);
105 Assert.AreEqual(textarea.GetAttribute("value"), cheesey);
106 }
107
108 [Test]
109 [IgnoreBrowser(Browser.Firefox)]
110 public void ShouldSubmitAFormUsingTheNewlineLiteral()
111 {
112 driver.Url = formsPage;
113 IWebElement nestedForm = driver.FindElement(By.Id("nested_form"));
114 IWebElement input = nestedForm.FindElement(By.Name("x"));
115 input.SendKeys("\n");
116
117 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
118 Assert.AreEqual("We Arrive Here", driver.Title);
119 Assert.That(driver.Url, Does.EndWith("?x=name"));
120 }
121
122 [Test]
123 public void ShouldSubmitAFormUsingTheEnterKey()
124 {
125 driver.Url = formsPage;
126 IWebElement nestedForm = driver.FindElement(By.Id("nested_form"));
127 IWebElement input = nestedForm.FindElement(By.Name("x"));
128 input.SendKeys(Keys.Enter);
129
130 WaitFor(TitleToBe("We Arrive Here"), "Browser title is not 'We Arrive Here'");
131 Assert.AreEqual("We Arrive Here", driver.Title);
132 Assert.That(driver.Url, Does.EndWith("?x=name"));
133 }
134
135 [Test]
136 public void ShouldEnterDataIntoFormFields()
137 {
138 driver.Url = xhtmlTestPage;
139 IWebElement element = driver.FindElement(By.XPath("//form[@name='someForm']/input[@id='username']"));
140 String originalValue = element.GetAttribute("value");
141 Assert.AreEqual(originalValue, "change");
142
143 element.Clear();
144 element.SendKeys("some text");
145
146 element = driver.FindElement(By.XPath("//form[@name='someForm']/input[@id='username']"));
147 String newFormValue = element.GetAttribute("value");
148 Assert.AreEqual(newFormValue, "some text");
149 }
150
151 [Test]
152 public void ShouldBeAbleToAlterTheContentsOfAFileUploadInputElement()
153 {
154 string testFileName = string.Format("test-{0}.txt", Guid.NewGuid().ToString("D"));
155 driver.Url = formsPage;
156 IWebElement uploadElement = driver.FindElement(By.Id("upload"));
157 Assert.That(uploadElement.GetAttribute("value"), Is.Null.Or.EqualTo(string.Empty));
158
159 string filePath = System.IO.Path.Combine(EnvironmentManager.Instance.CurrentDirectory, testFileName);
160 System.IO.FileInfo inputFile = new System.IO.FileInfo(filePath);
161 System.IO.StreamWriter inputFileWriter = inputFile.CreateText();
162 inputFileWriter.WriteLine("Hello world");
163 inputFileWriter.Close();
164
165 uploadElement.SendKeys(inputFile.FullName);
166
167 string uploadElementValue = uploadElement.GetAttribute("value");
168 System.IO.FileInfo outputFile = new System.IO.FileInfo(uploadElementValue.Replace('\\', System.IO.Path.DirectorySeparatorChar));
169 Assert.That(inputFile.Name, Is.EqualTo(outputFile.Name));
170 inputFile.Delete();
171 }
172
173 [Test]
174 public void ShouldBeAbleToSendKeysToAFileUploadInputElementInAnXhtmlDocument()
175 {
176 // IE before 9 doesn't handle pages served with an XHTML content type, and just prompts for to
177 // download it
178 if (TestUtilities.IsOldIE(driver))
179 {
180 return;
181 }
182
183 driver.Url = xhtmlFormPage;
184 IWebElement uploadElement = driver.FindElement(By.Id("file"));
185 Assert.AreEqual(string.Empty, uploadElement.GetAttribute("value"));
186
187 string testFileName = string.Format("test-{0}.txt", Guid.NewGuid().ToString("D"));
188 string filePath = System.IO.Path.Combine(EnvironmentManager.Instance.CurrentDirectory, testFileName);
189 System.IO.FileInfo inputFile = new System.IO.FileInfo(filePath);
190 System.IO.StreamWriter inputFileWriter = inputFile.CreateText();
191 inputFileWriter.WriteLine("Hello world");
192 inputFileWriter.Close();
193
194 uploadElement.SendKeys(inputFile.FullName);
195
196 string uploadElementValue = uploadElement.GetAttribute("value");
197 System.IO.FileInfo outputFile = new System.IO.FileInfo(uploadElementValue.Replace('\\', System.IO.Path.DirectorySeparatorChar));
198 Assert.AreEqual(inputFile.Name, outputFile.Name);
199 inputFile.Delete();
200 }
201
202 [Test]
203 [IgnoreBrowser(Browser.Safari, "Driver does not allow uploading same file multiple times.")]
204 public void ShouldBeAbleToUploadTheSameFileTwice()
205 {
206 string testFileName = string.Format("test-{0}.txt", Guid.NewGuid().ToString("D"));
207 string filePath = System.IO.Path.Combine(EnvironmentManager.Instance.CurrentDirectory, testFileName);
208 System.IO.FileInfo inputFile = new System.IO.FileInfo(filePath);
209 System.IO.StreamWriter inputFileWriter = inputFile.CreateText();
210 inputFileWriter.WriteLine("Hello world");
211 inputFileWriter.Close();
212
213 for (int i = 0; i < 2; ++i)
214 {
215 driver.Url = formsPage;
216 IWebElement uploadElement = driver.FindElement(By.Id("upload"));
217 Assert.That(uploadElement.GetAttribute("value"), Is.Null.Or.EqualTo(string.Empty));
218
219 uploadElement.SendKeys(inputFile.FullName);
220 uploadElement.Submit();
221 }
222
223 inputFile.Delete();
224 // If we get this far, then we're all good.
225 }
226
227 [Test]
228 public void SendingKeyboardEventsShouldAppendTextInInputs()
229 {
230 driver.Url = formsPage;
231 IWebElement element = driver.FindElement(By.Id("working"));
232 element.SendKeys("Some");
233 String value = element.GetAttribute("value");
234 Assert.AreEqual(value, "Some");
235
236 element.SendKeys(" text");
237 value = element.GetAttribute("value");
238 Assert.AreEqual(value, "Some text");
239 }
240
241 [Test]
242 public void SendingKeyboardEventsShouldAppendTextInInputsWithExistingValue()
243 {
244 driver.Url = formsPage;
245 IWebElement element = driver.FindElement(By.Id("inputWithText"));
246 element.SendKeys(". Some text");
247 string value = element.GetAttribute("value");
248
249 Assert.AreEqual("Example text. Some text", value);
250 }
251
252 [Test]
253 public void SendingKeyboardEventsShouldAppendTextInTextAreas()
254 {
255 driver.Url = formsPage;
256 IWebElement element = driver.FindElement(By.Id("withText"));
257
258 element.SendKeys(". Some text");
259 String value = element.GetAttribute("value");
260
261 Assert.AreEqual(value, "Example text. Some text");
262 }
263
264 [Test]
265 public void EmptyTextBoxesShouldReturnAnEmptyStringNotNull()
266 {
267 driver.Url = formsPage;
268 IWebElement emptyTextBox = driver.FindElement(By.Id("working"));
269 Assert.AreEqual(emptyTextBox.GetAttribute("value"), "");
270
271 IWebElement emptyTextArea = driver.FindElement(By.Id("emptyTextArea"));
272 Assert.AreEqual(emptyTextBox.GetAttribute("value"), "");
273 }
274
275 [Test]
276 public void HandleFormWithJavascriptAction()
277 {
278 string url = EnvironmentManager.Instance.UrlBuilder.WhereIs("form_handling_js_submit.html");
279 driver.Url = url;
280 IWebElement element = driver.FindElement(By.Id("theForm"));
281 element.Submit();
282 IAlert alert = WaitFor<IAlert>(() =>
283 {
284 try
285 {
286 return driver.SwitchTo().Alert();
287 }
288 catch (NoAlertPresentException)
289 {
290 return null;
291 }
292 }, "No alert found before timeout.");
293
294 string text = alert.Text;
295 alert.Dismiss();
296
297 Assert.AreEqual("Tasty cheese", text);
298 }
299
300 [Test]
301 public void CanClickOnASubmitButton()
302 {
303 CheckSubmitButton("internal_explicit_submit");
304 }
305
306 [Test]
307 public void CanClickOnASubmitButtonNestedSpan()
308 {
309 CheckSubmitButton("internal_span_submit");
310 }
311
312 [Test]
313 public void CanClickOnAnImplicitSubmitButton()
314 {
315 CheckSubmitButton("internal_implicit_submit");
316 }
317
318 [Test]
319 [IgnoreBrowser(Browser.IE, "IE does not support the HTML5 'form' attribute on <button> elements")]
320 public void CanClickOnAnExternalSubmitButton()
321 {
322 CheckSubmitButton("external_explicit_submit");
323 }
324
325 [Test]
326 [IgnoreBrowser(Browser.IE, "IE does not support the HTML5 'form' attribute on <button> elements")]
327 public void CanClickOnAnExternalImplicitSubmitButton()
328 {
329 CheckSubmitButton("external_implicit_submit");
330 }
331
332 [Test]
333 [IgnoreTarget("net48", "Cannot create inline page with UrlBuilder")]
334 public void CanSubmitFormWithSubmitButtonIdEqualToSubmit()
335 {
336 string blank = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
337 .WithTitle("Submitted Successfully!"));
338 driver.Url = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
339 .WithBody(string.Format("<form action='{0}'>", blank),
340 " <input type='submit' id='submit' value='Submit'>",
341 "</form>"));
342
343 driver.FindElement(By.Id("submit")).Submit();
344 WaitFor(TitleToBe("Submitted Successfully!"), "Title was not expected value");
345 }
346
347 [Test]
348 [IgnoreTarget("net48", "Cannot create inline page with UrlBuilder")]
349 public void CanSubmitFormWithSubmitButtonNameEqualToSubmit()
350 {
351 string blank = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
352 .WithTitle("Submitted Successfully!"));
353 driver.Url = EnvironmentManager.Instance.UrlBuilder.CreateInlinePage(new InlinePage()
354 .WithBody(string.Format("<form action='{0}'>", blank),
355 " <input type='submit' name='submit' value='Submit'>",
356 "</form>"));
357
358 driver.FindElement(By.Name("submit")).Submit();
359 WaitFor(TitleToBe("Submitted Successfully!"), "Title was not expected value");
360 }
361
362 //------------------------------------------------------------------
363 // Tests below here are not included in the Java test suite
364 //------------------------------------------------------------------
365 [Test]
366 public void ShouldBeAbleToClearTextFromInputElements()
367 {
368 driver.Url = formsPage;
369 IWebElement element = driver.FindElement(By.Id("working"));
370 element.SendKeys("Some text");
371 String value = element.GetAttribute("value");
372 Assert.That(value.Length, Is.GreaterThan(0));
373
374 element.Clear();
375 value = element.GetAttribute("value");
376
377 Assert.That(value.Length, Is.EqualTo(0));
378 }
379
380 [Test]
381 public void ShouldBeAbleToClearTextFromTextAreas()
382 {
383 driver.Url = formsPage;
384 IWebElement element = driver.FindElement(By.Id("withText"));
385 element.SendKeys("Some text");
386 String value = element.GetAttribute("value");
387 Assert.That(value.Length, Is.GreaterThan(0));
388
389 element.Clear();
390 value = element.GetAttribute("value");
391
392 Assert.That(value.Length, Is.EqualTo(0));
393 }
394
395 private void CheckSubmitButton(string buttonId)
396 {
397 driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("click_tests/html5_submit_buttons.html");
398 string name = "Gromit";
399
400 driver.FindElement(By.Id("name")).SendKeys(name);
401 driver.FindElement(By.Id(buttonId)).Click();
402
403 WaitFor(TitleToBe("Submitted Successfully!"), "Browser title is not 'Submitted Successfully!'");
404
405 Assert.That(driver.Url.Contains("name=" + name), "URL does not contain 'name=" + name + "'. Actual URL:" + driver.Url);
406 }
407
408 private Func<bool> TitleToBe(string desiredTitle)
409 {
410 return () =>
411 {
412 return driver.Title == desiredTitle;
413 };
414 }
415 }
416 }