"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/dotnet/test/common/Interactions/BasicMouseInterfaceTest.cs" (17 Feb 2023, 19307 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 "BasicMouseInterfaceTest.cs": 4.6.0_vs_4.7.0.

    1 using System;
    2 using NUnit.Framework;
    3 using System.Text.RegularExpressions;
    4 using System.Drawing;
    5 using OpenQA.Selenium.Internal;
    6 using OpenQA.Selenium.Environment;
    7 
    8 namespace OpenQA.Selenium.Interactions
    9 {
   10     [TestFixture]
   11     public class BasicMouseInterfaceTest : DriverTestFixture
   12     {
   13         [SetUp]
   14         public void SetupTest()
   15         {
   16             IActionExecutor actionExecutor = driver as IActionExecutor;
   17             if (actionExecutor != null)
   18             {
   19                 actionExecutor.ResetInputState();
   20             }
   21         }
   22 
   23         [Test]
   24         public void ShouldAllowDraggingElementWithMouseMovesItToAnotherList()
   25         {
   26             PerformDragAndDropWithMouse();
   27             IWebElement dragInto = driver.FindElement(By.Id("sortable1"));
   28             Assert.AreEqual(6, dragInto.FindElements(By.TagName("li")).Count);
   29         }
   30 
   31         // This test is very similar to DraggingElementWithMouse. The only
   32         // difference is that this test also verifies the correct events were fired.
   33         [Test]
   34         public void DraggingElementWithMouseFiresEvents()
   35         {
   36             PerformDragAndDropWithMouse();
   37             IWebElement dragReporter = driver.FindElement(By.Id("dragging_reports"));
   38             // This is failing under HtmlUnit. A bug was filed.
   39             Assert.That(dragReporter.Text, Does.Match("Nothing happened\\. (?:DragOut *)+DropIn RightItem 3"));
   40         }
   41 
   42         [Test]
   43         public void ShouldAllowDoubleClickThenNavigate()
   44         {
   45             driver.Url = javascriptPage;
   46 
   47             IWebElement toDoubleClick = driver.FindElement(By.Id("doubleClickField"));
   48 
   49             Actions actionProvider = new Actions(driver);
   50             IAction dblClick = actionProvider.DoubleClick(toDoubleClick).Build();
   51 
   52             dblClick.Perform();
   53             driver.Url = droppableItems;
   54         }
   55 
   56         [Test]
   57         public void ShouldAllowDragAndDrop()
   58         {
   59             driver.Url = droppableItems;
   60 
   61             DateTime waitEndTime = DateTime.Now.Add(TimeSpan.FromSeconds(15));
   62 
   63             while (!IsElementAvailable(driver, By.Id("draggable")) && (DateTime.Now < waitEndTime))
   64             {
   65                 System.Threading.Thread.Sleep(200);
   66             }
   67 
   68             if (!IsElementAvailable(driver, By.Id("draggable")))
   69             {
   70                 throw new Exception("Could not find draggable element after 15 seconds.");
   71             }
   72 
   73             IWebElement toDrag = driver.FindElement(By.Id("draggable"));
   74             IWebElement dropInto = driver.FindElement(By.Id("droppable"));
   75 
   76             Actions actionProvider = new Actions(driver);
   77 
   78             IAction holdDrag = actionProvider.ClickAndHold(toDrag).Build();
   79 
   80             IAction move = actionProvider.MoveToElement(dropInto).Build();
   81 
   82             IAction drop = actionProvider.Release(dropInto).Build();
   83 
   84             holdDrag.Perform();
   85             move.Perform();
   86             drop.Perform();
   87 
   88             dropInto = driver.FindElement(By.Id("droppable"));
   89             string text = dropInto.FindElement(By.TagName("p")).Text;
   90 
   91             Assert.AreEqual("Dropped!", text);
   92         }
   93 
   94         [Test]
   95         public void ShouldAllowDoubleClick()
   96         {
   97             driver.Url = javascriptPage;
   98 
   99             IWebElement toDoubleClick = driver.FindElement(By.Id("doubleClickField"));
  100 
  101             Actions actionProvider = new Actions(driver);
  102             IAction dblClick = actionProvider.DoubleClick(toDoubleClick).Build();
  103 
  104             dblClick.Perform();
  105             Assert.AreEqual("DoubleClicked", toDoubleClick.GetAttribute("value"));
  106         }
  107 
  108         [Test]
  109         public void ShouldAllowContextClick()
  110         {
  111             driver.Url = javascriptPage;
  112 
  113             IWebElement toContextClick = driver.FindElement(By.Id("doubleClickField"));
  114 
  115             Actions actionProvider = new Actions(driver);
  116             IAction contextClick = actionProvider.ContextClick(toContextClick).Build();
  117 
  118             contextClick.Perform();
  119             Assert.AreEqual("ContextClicked", toContextClick.GetAttribute("value"));
  120         }
  121 
  122         [Test]
  123         [IgnoreBrowser(Browser.Remote, "API not implemented in driver")]
  124         public void ShouldAllowMoveAndClick()
  125         {
  126             driver.Url = javascriptPage;
  127 
  128             IWebElement toClick = driver.FindElement(By.Id("clickField"));
  129 
  130             Actions actionProvider = new Actions(driver);
  131             IAction contextClick = actionProvider.MoveToElement(toClick).Click().Build();
  132 
  133             contextClick.Perform();
  134             Assert.AreEqual("Clicked", toClick.GetAttribute("value"), "Value should change to Clicked.");
  135         }
  136 
  137         [Test]
  138         public void ShouldNotMoveToANullLocator()
  139         {
  140             driver.Url = javascriptPage;
  141 
  142             Assert.That(() => new Actions(driver).MoveToElement(null).Perform(), Throws.InstanceOf<ArgumentException>());
  143         }
  144 
  145         [Test]
  146         [IgnoreBrowser(Browser.Chrome, "Drivers correctly click at current mouse position without another move, preserving mouse position.")]
  147         [IgnoreBrowser(Browser.Edge, "Drivers correctly click at current mouse position without another move, preserving mouse position.")]
  148         [IgnoreBrowser(Browser.Firefox, "Drivers correctly click at current mouse position without another move, preserving mouse position.")]
  149         [IgnoreBrowser(Browser.IE, "Drivers correctly click at current mouse position without another move, preserving mouse position.")]
  150         [IgnoreBrowser(Browser.Safari, "Drivers correctly click at current mouse position without another move, preserving mouse position.")]
  151         public void MousePositionIsNotPreservedInActionsChain()
  152         {
  153             driver.Url = javascriptPage;
  154             IWebElement toMoveTo = driver.FindElement(By.Id("clickField"));
  155 
  156             new Actions(driver).MoveToElement(toMoveTo).Perform();
  157             Assert.That(() => new Actions(driver).Click().Perform(), Throws.InstanceOf<WebDriverException>());
  158         }
  159 
  160         [Test]
  161         [IgnoreBrowser(Browser.All, "Behaviour not finalized yet regarding linked images.")]
  162         public void MovingIntoAnImageEnclosedInALink()
  163         {
  164             driver.Url = linkedImage;
  165 
  166             // Note: For some reason, the Accessibility API in Firefox will not be available before we
  167             // click on something. As a work-around, click on a different element just to get going.
  168             driver.FindElement(By.Id("linkToAnchorOnThisPage")).Click();
  169 
  170             IWebElement linkElement = driver.FindElement(By.Id("linkWithEnclosedImage"));
  171 
  172             // Image is 644 x 41 - move towards the end.
  173             // Note: The width of the link element itself is correct - 644 pixels. However,
  174             // the height is 17 pixels and the rectangle containing it is *underneath* the image.
  175             // For this reason, this action will fail.
  176             new Actions(driver).MoveToElement(linkElement, 500, 30).Click().Perform();
  177 
  178             WaitFor(TitleToBe("We Arrive Here"), "Title was not expected value");
  179         }
  180 
  181         [Test]
  182         [IgnoreBrowser(Browser.Chrome, "Moving outside of view port throws exception in spec-compliant driver")]
  183         [IgnoreBrowser(Browser.Edge, "Moving outside of view port throws exception in spec-compliant driver")]
  184         [IgnoreBrowser(Browser.Firefox, "Moving outside of view port throws exception in spec-compliant driver")]
  185         [IgnoreBrowser(Browser.IE, "Moving outside of view port throws exception in spec-compliant driver")]
  186         [IgnoreBrowser(Browser.Safari, "Moving outside of view port throws exception in spec-compliant driver")]
  187         public void MovingMouseBackAndForthPastViewPort()
  188         {
  189             driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("veryLargeCanvas.html");
  190 
  191             IWebElement firstTarget = driver.FindElement(By.Id("r1"));
  192             new Actions(driver).MoveToElement(firstTarget).Click().Perform();
  193 
  194             IWebElement resultArea = driver.FindElement(By.Id("result"));
  195             String expectedEvents = "First";
  196             WaitFor(ElementTextToEqual(resultArea, expectedEvents), "Element text did not equal " + expectedEvents);
  197 
  198             // Move to element with id 'r2', at (2500, 50) to (2580, 100)
  199             new Actions(driver).MoveByOffset(2540 - 150, 75 - 125).Click().Perform();
  200             expectedEvents += " Second";
  201             WaitFor(ElementTextToEqual(resultArea, expectedEvents), "Element text did not equal " + expectedEvents);
  202 
  203             // Move to element with id 'r3' at (60, 1500) to (140, 1550)
  204             new Actions(driver).MoveByOffset(100 - 2540, 1525 - 75).Click().Perform();
  205             expectedEvents += " Third";
  206             WaitFor(ElementTextToEqual(resultArea, expectedEvents), "Element text did not equal " + expectedEvents);
  207 
  208             // Move to element with id 'r4' at (220,180) to (320, 230)
  209             new Actions(driver).MoveByOffset(270 - 100, 205 - 1525).Click().Perform();
  210             expectedEvents += " Fourth";
  211             WaitFor(ElementTextToEqual(resultArea, expectedEvents), "Element text did not equal " + expectedEvents);
  212         }
  213 
  214         [Test]
  215         public void ShouldClickElementInIFrame()
  216         {
  217             driver.Url = clicksPage;
  218             try
  219             {
  220                 driver.SwitchTo().Frame("source");
  221                 IWebElement element = driver.FindElement(By.Id("otherframe"));
  222                 new Actions(driver).MoveToElement(element).Click().Perform();
  223                 driver.SwitchTo().DefaultContent().SwitchTo().Frame("target");
  224                 WaitFor(() => { return driver.FindElement(By.Id("span")).Text == "An inline element"; }, "Could not find element with text 'An inline element'");
  225             }
  226             finally
  227             {
  228                 driver.SwitchTo().DefaultContent();
  229             }
  230         }
  231 
  232         [Test]
  233         public void ShouldAllowUsersToHoverOverElements()
  234         {
  235             driver.Url = javascriptPage;
  236 
  237             IWebElement element = driver.FindElement(By.Id("menu1"));
  238             if (!Platform.CurrentPlatform.IsPlatformType(PlatformType.Windows))
  239             {
  240                 Assert.Ignore("Skipping test: Simulating hover needs native events");
  241             }
  242 
  243             IWebElement item = driver.FindElement(By.Id("item1"));
  244             Assert.AreEqual("", item.Text);
  245 
  246             ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.background = 'green'", element);
  247             Actions actionBuilder = new Actions(driver);
  248             actionBuilder.MoveToElement(element).Perform();
  249 
  250             item = driver.FindElement(By.Id("item1"));
  251             Assert.AreEqual("Item 1", item.Text);
  252         }
  253 
  254         [Test]
  255         public void HoverPersists()
  256         {
  257             driver.Url = javascriptPage;
  258             // Move to a different element to make sure the mouse is not over the
  259             // element with id 'item1' (from a previous test).
  260             new Actions(driver).MoveToElement(driver.FindElement(By.Id("dynamo"))).Perform();
  261 
  262             IWebElement element = driver.FindElement(By.Id("menu1"));
  263 
  264             IWebElement item = driver.FindElement(By.Id("item1"));
  265             Assert.AreEqual(string.Empty, item.Text);
  266 
  267             ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.background = 'green'", element);
  268             new Actions(driver).MoveToElement(element).Perform();
  269 
  270             // Intentionally wait to make sure hover persists.
  271             System.Threading.Thread.Sleep(2000);
  272 
  273             WaitFor(ElementTextToNotEqual(item, ""), "Element text was empty after timeout");
  274 
  275             Assert.AreEqual("Item 1", item.Text);
  276         }
  277 
  278         [Test]
  279         public void MovingMouseByRelativeOffset()
  280         {
  281             driver.Url = mouseTrackerPage;
  282 
  283             IWebElement trackerDiv = driver.FindElement(By.Id("mousetracker"));
  284             new Actions(driver).MoveToElement(trackerDiv).Build().Perform();
  285 
  286             IWebElement reporter = driver.FindElement(By.Id("status"));
  287 
  288             WaitFor(FuzzyMatchingOfCoordinates(reporter, 50, 200), "Coordinate matching was not within tolerance");
  289 
  290             new Actions(driver).MoveByOffset(10, 20).Build().Perform();
  291 
  292             WaitFor(FuzzyMatchingOfCoordinates(reporter, 60, 220), "Coordinate matching was not within tolerance");
  293         }
  294 
  295         [Test]
  296         public void MovingMouseToRelativeElementOffset()
  297         {
  298             driver.Url = mouseTrackerPage;
  299 
  300             IWebElement trackerDiv = driver.FindElement(By.Id("mousetracker"));
  301             Size size = trackerDiv.Size;
  302 
  303             new Actions(driver).MoveToElement(trackerDiv, 95 - size.Width / 2, 195 - size.Height / 2).Build().Perform();
  304 
  305             IWebElement reporter = driver.FindElement(By.Id("status"));
  306 
  307             WaitFor(FuzzyMatchingOfCoordinates(reporter, 95, 195), "Coordinate matching was not within tolerance");
  308         }
  309 
  310         [Test]
  311         public void MovingMouseToRelativeZeroElementOffset()
  312         {
  313             driver.Url = mouseTrackerPage;
  314 
  315             IWebElement trackerDiv = driver.FindElement(By.Id("mousetracker"));
  316             Size size = trackerDiv.Size;
  317 
  318             new Actions(driver).MoveToElement(trackerDiv, -size.Width / 2, -size.Height / 2).Perform();
  319 
  320             IWebElement reporter = driver.FindElement(By.Id("status"));
  321 
  322             WaitFor(FuzzyMatchingOfCoordinates(reporter, 0, 0), "Coordinate matching was not within tolerance");
  323         }
  324 
  325         [Test]
  326         [NeedsFreshDriver(IsCreatedBeforeTest = true)]
  327         public void MoveRelativeToBody()
  328         {
  329             driver.Url = mouseTrackerPage;
  330 
  331             new Actions(driver).MoveByOffset(50, 100).Build().Perform();
  332 
  333             IWebElement reporter = driver.FindElement(By.Id("status"));
  334 
  335             WaitFor(FuzzyMatchingOfCoordinates(reporter, 40, 20), "Coordinate matching was not within tolerance");
  336         }
  337 
  338         [Test]
  339         public void MoveMouseByOffsetOverAndOutOfAnElement()
  340         {
  341             driver.Url = mouseOverPage;
  342 
  343             IWebElement greenbox = driver.FindElement(By.Id("greenbox"));
  344             IWebElement redbox = driver.FindElement(By.Id("redbox"));
  345             Point greenboxPosition = greenbox.Location;
  346             Point redboxPosition = redbox.Location;
  347             int shiftX = redboxPosition.X - greenboxPosition.X;
  348             int shiftY = redboxPosition.Y - greenboxPosition.Y;
  349 
  350             Size greenBoxSize = greenbox.Size;
  351             int xOffset = 2 - greenBoxSize.Width / 2;
  352             int yOffset = 2 - greenBoxSize.Height / 2;
  353 
  354             new Actions(driver).MoveToElement(greenbox, xOffset, yOffset).Perform();
  355             WaitFor(ElementColorToBe(redbox, Color.Green), "element color was not green");
  356 
  357             new Actions(driver).MoveToElement(greenbox, xOffset, yOffset).MoveByOffset(shiftX, shiftY).Perform();
  358             WaitFor(ElementColorToBe(redbox, Color.Red), "element color was not red");
  359 
  360             new Actions(driver).MoveToElement(greenbox, xOffset, yOffset).MoveByOffset(shiftX, shiftY).MoveByOffset(-shiftX, -shiftY).Perform();
  361             WaitFor(ElementColorToBe(redbox, Color.Green), "element color was not red");
  362         }
  363 
  364         [Test]
  365         public void CanMouseOverAndOutOfAnElement()
  366         {
  367             driver.Url = mouseOverPage;
  368 
  369             IWebElement greenbox = driver.FindElement(By.Id("greenbox"));
  370             IWebElement redbox = driver.FindElement(By.Id("redbox"));
  371             Size greenSize = greenbox.Size;
  372             Size redSize = redbox.Size;
  373 
  374             new Actions(driver).MoveToElement(greenbox, 1 - greenSize.Width / 2, 1 - greenSize.Height / 2).Perform();
  375             Assert.That(redbox.GetCssValue("background-color"), Is.EqualTo("rgba(0, 128, 0, 1)").Or.EqualTo("rgb(0, 128, 0)"));
  376 
  377             new Actions(driver).MoveToElement(redbox).Perform();
  378             Assert.That(redbox.GetCssValue("background-color"), Is.EqualTo("rgba(255, 0, 0, 1)").Or.EqualTo("rgb(255, 0, 0)"));
  379 
  380             new Actions(driver).MoveToElement(redbox, redSize.Width / 2 + 2, redSize.Height / 2 + 2).Perform();
  381             Assert.That(redbox.GetCssValue("background-color"), Is.EqualTo("rgba(0, 128, 0, 1)").Or.EqualTo("rgb(0, 128, 0)"));
  382         }
  383 
  384         private Func<bool> FuzzyMatchingOfCoordinates(IWebElement element, int x, int y)
  385         {
  386             return () =>
  387             {
  388                 return FuzzyPositionMatching(x, y, element.Text);
  389             };
  390         }
  391 
  392         private bool FuzzyPositionMatching(int expectedX, int expectedY, String locationTuple)
  393         {
  394             string[] splitString = locationTuple.Split(',');
  395             int gotX = Convert.ToInt16(Math.Round(Convert.ToDouble(splitString[0].Trim())));
  396             int gotY = Convert.ToInt16(Math.Round(Convert.ToDouble(splitString[1].Trim())));
  397 
  398             // Everything within 5 pixels range is OK
  399             const int ALLOWED_DEVIATION = 5;
  400             return Math.Abs(expectedX - gotX) < ALLOWED_DEVIATION && Math.Abs(expectedY - gotY) < ALLOWED_DEVIATION;
  401         }
  402 
  403         private void PerformDragAndDropWithMouse()
  404         {
  405             driver.Url = draggableLists;
  406 
  407             IWebElement dragReporter = driver.FindElement(By.Id("dragging_reports"));
  408 
  409             IWebElement toDrag = driver.FindElement(By.Id("rightitem-3"));
  410             IWebElement dragInto = driver.FindElement(By.Id("sortable1"));
  411 
  412             IAction holdItem = new Actions(driver).ClickAndHold(toDrag).Build();
  413 
  414             IAction moveToSpecificItem = new Actions(driver).MoveToElement(driver.FindElement(By.Id("leftitem-4"))).Build();
  415 
  416             IAction moveToOtherList = new Actions(driver).MoveToElement(dragInto).Build();
  417 
  418             IAction drop = new Actions(driver).Release(dragInto).Build();
  419 
  420             Assert.AreEqual("Nothing happened.", dragReporter.Text);
  421 
  422             holdItem.Perform();
  423             moveToSpecificItem.Perform();
  424             moveToOtherList.Perform();
  425 
  426             Assert.That(dragReporter.Text, Does.Match("Nothing happened\\. (?:DragOut *)+"));
  427             drop.Perform();
  428         }
  429 
  430         private bool IsElementAvailable(IWebDriver driver, By locator)
  431         {
  432             try
  433             {
  434                 driver.FindElement(locator);
  435                 return true;
  436             }
  437             catch (NoSuchElementException)
  438             {
  439                 return false;
  440             }
  441         }
  442 
  443         private Func<bool> TitleToBe(string desiredTitle)
  444         {
  445             return () => driver.Title == desiredTitle;
  446         }
  447 
  448         private Func<bool> ElementTextToEqual(IWebElement element, string text)
  449         {
  450             return () => element.Text == text;
  451         }
  452 
  453         private Func<bool> ElementTextToNotEqual(IWebElement element, string text)
  454         {
  455             return () => element.Text != text;
  456         }
  457 
  458         private Func<bool> ElementColorToBe(IWebElement element, Color color)
  459         {
  460             return () =>
  461             {
  462                 string rgb = string.Format("rgb({0}, {1}, {2})", color.R, color.G, color.B);
  463                 string rgba = string.Format("rgba({0}, {1}, {2}, 1)", color.R, color.G, color.B);
  464                 string value = element.GetCssValue("background-color");
  465                 return value == rgb || value == rgba;
  466             };
  467         }
  468     }
  469 }