"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/dotnet/test/common/Interactions/CombinedInputActionsTest.cs" (17 Feb 2023, 14924 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# 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 "CombinedInputActionsTest.cs":
4.6.0_vs_4.7.0.
1 using NUnit.Framework;
2 using OpenQA.Selenium.Environment;
3 using OpenQA.Selenium.Internal;
4 using System;
5 using System.Collections.ObjectModel;
6 using System.Drawing;
7 using System.Runtime.InteropServices;
8
9 namespace OpenQA.Selenium.Interactions
10 {
11 [TestFixture]
12 public class CombinedInputActionsTest : DriverTestFixture
13 {
14 [SetUp]
15 public void Setup()
16 {
17 // new Actions(driver).SendKeys(Keys.Null).Perform();
18 IActionExecutor actionExecutor = driver as IActionExecutor;
19 if (actionExecutor != null)
20 {
21 actionExecutor.ResetInputState();
22 }
23 }
24
25 [TearDown]
26 public void ReleaseModifierKeys()
27 {
28 // new Actions(driver).SendKeys(Keys.Null).Perform();
29 IActionExecutor actionExecutor = driver as IActionExecutor;
30 if (actionExecutor != null)
31 {
32 actionExecutor.ResetInputState();
33 }
34 }
35
36 [Test]
37 [IgnoreBrowser(Browser.IE, "IE reports [0,0] as location for <option> elements")]
38 public void PlainClickingOnMultiSelectionList()
39 {
40 driver.Url = formSelectionPage;
41
42 ReadOnlyCollection<IWebElement> options = driver.FindElements(By.TagName("option"));
43
44 Actions actionBuider = new Actions(driver);
45 IAction selectThreeOptions = actionBuider.Click(options[1])
46 .Click(options[2])
47 .Click(options[3]).Build();
48
49 selectThreeOptions.Perform();
50
51 IWebElement showButton = driver.FindElement(By.Name("showselected"));
52 showButton.Click();
53
54 IWebElement resultElement = driver.FindElement(By.Id("result"));
55 Assert.AreEqual("cheddar", resultElement.Text, "Should have picked the third option only.");
56 }
57
58 [Test]
59 [IgnoreBrowser(Browser.IE, "IE reports [0,0] as location for <option> elements")]
60 public void ShiftClickingOnMultiSelectionList()
61 {
62 driver.Url = formSelectionPage;
63
64 ReadOnlyCollection<IWebElement> options = driver.FindElements(By.TagName("option"));
65
66 Actions actionBuider = new Actions(driver);
67 IAction selectThreeOptions = actionBuider.Click(options[1])
68 .KeyDown(Keys.Shift)
69 .Click(options[2])
70 .Click(options[3])
71 .KeyUp(Keys.Shift).Build();
72
73 selectThreeOptions.Perform();
74
75 IWebElement showButton = driver.FindElement(By.Name("showselected"));
76 showButton.Click();
77
78 IWebElement resultElement = driver.FindElement(By.Id("result"));
79 Assert.AreEqual("roquefort parmigiano cheddar", resultElement.Text, "Should have picked the last three options.");
80 }
81
82 [Test]
83 [IgnoreBrowser(Browser.IE, "IE reports [0,0] as location for <option> elements")]
84 public void ControlClickingOnMultiSelectionList()
85 {
86 string controlModifier = Keys.Control;
87 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
88 {
89 controlModifier = Keys.Command;
90 }
91
92 driver.Url = formSelectionPage;
93
94 ReadOnlyCollection<IWebElement> options = driver.FindElements(By.TagName("option"));
95
96 Actions actionBuider = new Actions(driver);
97 IAction selectThreeOptions = actionBuider.Click(options[1])
98 .KeyDown(controlModifier)
99 .Click(options[3])
100 .KeyUp(controlModifier).Build();
101
102 selectThreeOptions.Perform();
103
104 IWebElement showButton = driver.FindElement(By.Name("showselected"));
105 showButton.Click();
106
107 IWebElement resultElement = driver.FindElement(By.Id("result"));
108 Assert.AreEqual("roquefort cheddar", resultElement.Text, "Should have picked the first and third options.");
109 }
110
111 [Test]
112 public void ControlClickingOnCustomMultiSelectionList()
113 {
114 string controlModifier = Keys.Control;
115 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
116 {
117 controlModifier = Keys.Command;
118 }
119
120 driver.Url = selectableItemsPage;
121
122 IWebElement reportingElement = driver.FindElement(By.Id("infodiv"));
123
124 Assert.AreEqual("no info", reportingElement.Text);
125
126 ReadOnlyCollection<IWebElement> listItems = driver.FindElements(By.TagName("li"));
127
128 IAction selectThreeItems = new Actions(driver).KeyDown(controlModifier)
129 .Click(listItems[1])
130 .Click(listItems[3])
131 .Click(listItems[5])
132 .KeyUp(controlModifier).Build();
133
134 selectThreeItems.Perform();
135
136 Assert.AreEqual("#item2 #item4 #item6", reportingElement.Text);
137
138 // Now click on another element, make sure that's the only one selected.
139 new Actions(driver).Click(listItems[6]).Build().Perform();
140 Assert.AreEqual("#item7", reportingElement.Text);
141 }
142
143 [Test]
144 public void CanMoveMouseToAnElementInAnIframeAndClick()
145 {
146 driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("click_tests/click_in_iframe.html");
147
148 WaitFor<IWebElement>(() => driver.FindElement(By.Id("ifr")), "Did not find element");
149 driver.SwitchTo().Frame("ifr");
150
151 try
152 {
153 IWebElement link = driver.FindElement(By.Id("link"));
154
155 new Actions(driver)
156 .MoveToElement(link)
157 .Click()
158 .Perform();
159
160 WaitFor(() => driver.Title == "Submitted Successfully!", "Browser title not correct");
161 }
162 finally
163 {
164 driver.SwitchTo().DefaultContent();
165 }
166 }
167
168 [Test]
169 public void CanClickOnLinks()
170 {
171 this.NavigateToClicksPageAndClickLink();
172 }
173
174 [Test]
175 public void CanClickOnLinksWithAnOffset()
176 {
177 driver.Url = clicksPage;
178
179 WaitFor(() => { return driver.FindElement(By.Id("normal")); }, "Could not find element with id 'normal'");
180 IWebElement link = driver.FindElement(By.Id("normal"));
181
182 new Actions(driver)
183 .MoveToElement(link, 1, 1)
184 .Click()
185 .Perform();
186
187 WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title is not 'XHTML Test Page'");
188 }
189
190 [Test]
191 public void ClickAfterMoveToAnElementWithAnOffsetShouldUseLastMousePosition()
192 {
193 driver.Url = clickEventPage;
194
195 IWebElement element = driver.FindElement(By.Id("eventish"));
196 Point location = element.Location;
197 Size size = element.Size;
198
199 new Actions(driver)
200 .MoveToElement(element, 20 - size.Width / 2, 10 - size.Height / 2)
201 .Click()
202 .Perform();
203
204 WaitFor<IWebElement>(() => driver.FindElement(By.Id("pageX")), "Did not find element with ID pageX");
205
206 int x = Convert.ToInt16(Math.Round(Convert.ToDouble(driver.FindElement(By.Id("pageX")).Text)));
207 int y = Convert.ToInt16(Math.Round(Convert.ToDouble(driver.FindElement(By.Id("pageY")).Text)));
208
209 Assert.That(FuzzyPositionMatching(location.X + 20, location.Y + 10, string.Format("{0},{1}", x, y)), Is.True);
210 }
211
212 /**
213 * This test demonstrates the following problem: When the representation of
214 * the mouse in the driver keeps the wrong state, mouse movement will end
215 * up at the wrong coordinates.
216 */
217 [Test]
218 public void MouseMovementWorksWhenNavigatingToAnotherPage()
219 {
220 NavigateToClicksPageAndClickLink();
221
222 IWebElement linkId = driver.FindElement(By.Id("linkId"));
223 new Actions(driver)
224 .MoveToElement(linkId, 1, 1)
225 .Click()
226 .Perform();
227
228 WaitFor(() => { return driver.Title == "We Arrive Here"; }, "Browser title is not 'We Arrive Here'");
229 }
230
231 [Test]
232 public void ChordControlCutAndPaste()
233 {
234 string controlModifier = Keys.Control;
235 if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
236 {
237 controlModifier = Keys.Command;
238 }
239
240 driver.Url = javascriptPage;
241
242 IWebElement element = driver.FindElement(By.Id("keyReporter"));
243
244 // Must scroll element into view for W3C-compliant drivers.
245 ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView()", element);
246
247 new Actions(driver)
248 .SendKeys(element, "abc def")
249 .Perform();
250
251 Assert.AreEqual("abc def", element.GetAttribute("value"));
252
253 //TODO: Figure out why calling sendKey(Key.CONTROL + "a") and then
254 //sendKeys("x") does not work on Linux.
255 new Actions(driver).KeyDown(controlModifier)
256 .SendKeys("a" + "x")
257 .Perform();
258
259 // Release keys before next step.
260 new Actions(driver).SendKeys(Keys.Null).Perform();
261
262 Assert.AreEqual(string.Empty, element.GetAttribute("value"));
263
264 new Actions(driver).KeyDown(controlModifier)
265 .SendKeys("v")
266 .SendKeys("v")
267 .Perform();
268
269 new Actions(driver).SendKeys(Keys.Null).Perform();
270
271 Assert.AreEqual("abc defabc def", element.GetAttribute("value"));
272 }
273
274 [Test]
275 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
276 [NeedsFreshDriver(IsCreatedBeforeTest = true)]
277 public void CombiningShiftAndClickResultsInANewWindow()
278 {
279 driver.Url = linkedImage;
280 IWebElement link = driver.FindElement(By.Id("link"));
281 string originalTitle = driver.Title;
282
283 new Actions(driver)
284 .MoveToElement(link)
285 .KeyDown(Keys.Shift)
286 .Click()
287 .KeyUp(Keys.Shift)
288 .Perform();
289 WaitFor(() => { return driver.WindowHandles.Count > 1; }, "Did not receive new window");
290 Assert.AreEqual(2, driver.WindowHandles.Count, "Should have opened a new window.");
291 Assert.AreEqual(originalTitle, driver.Title, "Should not have navigated away.");
292
293 string originalHandle = driver.CurrentWindowHandle;
294 foreach(string newHandle in driver.WindowHandles)
295 {
296 if (newHandle != originalHandle)
297 {
298 driver.SwitchTo().Window(newHandle);
299 driver.Close();
300 }
301 }
302
303 driver.SwitchTo().Window(originalHandle);
304 }
305
306 [Test]
307 [IgnoreBrowser(Browser.IE, "Edge in IE Mode does not properly handle multiple windows")]
308 public void HoldingDownShiftKeyWhileClicking()
309 {
310 driver.Url = clickEventPage;
311
312 IWebElement toClick = driver.FindElement(By.Id("eventish"));
313
314 new Actions(driver).MoveToElement(toClick).KeyDown(Keys.Shift).Click().KeyUp(Keys.Shift).Perform();
315
316 IWebElement shiftInfo = WaitFor(() => { return driver.FindElement(By.Id("shiftKey")); }, "Could not find element with id 'shiftKey'");
317 Assert.AreEqual("true", shiftInfo.Text);
318 }
319
320 [Test]
321 public void CanClickOnSuckerFishStyleMenu()
322 {
323 driver.Url = javascriptPage;
324
325 // Move to a different element to make sure the mouse is not over the
326 // element with id 'item1' (from a previous test).
327 new Actions(driver).MoveToElement(driver.FindElement(By.Id("dynamo"))).Build().Perform();
328
329 IWebElement element = driver.FindElement(By.Id("menu1"));
330
331 IWebElement target = driver.FindElement(By.Id("item1"));
332 Assert.AreEqual(string.Empty, target.Text);
333 ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.background = 'green'", element);
334 new Actions(driver).MoveToElement(element).Build().Perform();
335
336 // Intentionally wait to make sure hover persists.
337 System.Threading.Thread.Sleep(2000);
338
339 target.Click();
340
341 IWebElement result = driver.FindElement(By.Id("result"));
342 WaitFor(() => { return result.Text.Contains("item 1"); }, "Result element does not contain text 'item 1'");
343 }
344
345 [Test]
346 public void CanClickOnSuckerFishMenuItem()
347 {
348 driver.Url = javascriptPage;
349
350 // Move to a different element to make sure the mouse is not over the
351 // element with id 'item1' (from a previous test).
352 new Actions(driver).MoveToElement(driver.FindElement(By.Id("dynamo"))).Build().Perform();
353
354 IWebElement element = driver.FindElement(By.Id("menu1"));
355
356 new Actions(driver).MoveToElement(element).Build().Perform();
357
358 IWebElement target = driver.FindElement(By.Id("item1"));
359
360 Assert.That(target.Displayed, "Target element was not displayed");
361 target.Click();
362
363 IWebElement result = driver.FindElement(By.Id("result"));
364 WaitFor(() => { return result.Text.Contains("item 1"); }, "Result element does not contain text 'item 1'");
365 }
366
367 [Test]
368 public void PerformsPause()
369 {
370 DateTime start = DateTime.Now;
371 new Actions(driver).Pause(TimeSpan.FromMilliseconds(1200)).Build().Perform();
372 Assert.IsTrue(DateTime.Now - start > TimeSpan.FromMilliseconds(1200));
373 }
374
375 private bool FuzzyPositionMatching(int expectedX, int expectedY, string locationTuple)
376 {
377 string[] splitString = locationTuple.Split(',');
378 int gotX = int.Parse(splitString[0].Trim());
379 int gotY = int.Parse(splitString[1].Trim());
380
381 // Everything within 5 pixels range is OK
382 const int ALLOWED_DEVIATION = 5;
383 return Math.Abs(expectedX - gotX) < ALLOWED_DEVIATION && Math.Abs(expectedY - gotY) < ALLOWED_DEVIATION;
384 }
385
386 private void NavigateToClicksPageAndClickLink()
387 {
388 driver.Url = clicksPage;
389
390 WaitFor(() => { return driver.FindElement(By.Id("normal")); }, "Could not find element with id 'normal'");
391 IWebElement link = driver.FindElement(By.Id("normal"));
392
393 new Actions(driver)
394 .Click(link)
395 .Perform();
396
397 WaitFor(() => { return driver.Title == "XHTML Test Page"; }, "Browser title is not 'XHTML Test Page'");
398 }
399 }
400 }