"Fossies" - the Fresh Open Source Software Archive

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

    1 using NUnit.Framework;
    2 using OpenQA.Selenium.Environment;
    3 using System;
    4 using System.Drawing;
    5 using System.Runtime.InteropServices;
    6 
    7 namespace OpenQA.Selenium.Interactions
    8 {
    9     [TestFixture]
   10     public class BasicKeyboardInterfaceTest : DriverTestFixture
   11     {
   12         [SetUp]
   13         public void Setup()
   14         {
   15             //new Actions(driver).SendKeys(Keys.Null).Perform();
   16             IActionExecutor actionExecutor = driver as IActionExecutor;
   17             if (actionExecutor != null)
   18             {
   19                 actionExecutor.ResetInputState();
   20             }
   21         }
   22 
   23         [TearDown]
   24         public void ReleaseModifierKeys()
   25         {
   26             //new Actions(driver).SendKeys(Keys.Null).Perform();
   27             IActionExecutor actionExecutor = driver as IActionExecutor;
   28             if (actionExecutor != null)
   29             {
   30                 actionExecutor.ResetInputState();
   31             }
   32         }
   33 
   34         [Test]
   35         [IgnoreBrowser(Browser.Remote, "API not implemented in driver")]
   36         public void ShouldAllowBasicKeyboardInput()
   37         {
   38             driver.Url = javascriptPage;
   39 
   40             IWebElement keyReporter = driver.FindElement(By.Id("keyReporter"));
   41 
   42             // Scroll the element into view before attempting any actions on it.
   43             ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView();", keyReporter);
   44 
   45             Actions actionProvider = new Actions(driver);
   46             IAction sendLowercase = actionProvider.SendKeys(keyReporter, "abc def").Build();
   47 
   48             sendLowercase.Perform();
   49 
   50             Assert.AreEqual("abc def", keyReporter.GetAttribute("value"));
   51 
   52         }
   53 
   54         [Test]
   55         [IgnoreBrowser(Browser.Remote, "API not implemented in driver")]
   56         public void ShouldAllowSendingKeyDownOnly()
   57         {
   58             driver.Url = javascriptPage;
   59 
   60             IWebElement keysEventInput = driver.FindElement(By.Id("theworks"));
   61 
   62             // Scroll the element into view before attempting any actions on it.
   63             ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView();", keysEventInput);
   64 
   65             Actions actionProvider = new Actions(driver);
   66 
   67             IAction pressShift = actionProvider.KeyDown(keysEventInput, Keys.Shift).Build();
   68             pressShift.Perform();
   69 
   70             IWebElement keyLoggingElement = driver.FindElement(By.Id("result"));
   71             string logText = keyLoggingElement.Text;
   72 
   73             IAction releaseShift = actionProvider.KeyDown(keysEventInput, Keys.Shift).Build();
   74             releaseShift.Perform();
   75 
   76             Assert.That(logText, Does.EndWith("keydown"));
   77         }
   78 
   79         [Test]
   80         [IgnoreBrowser(Browser.Remote, "API not implemented in driver")]
   81         public void ShouldAllowSendingKeyUp()
   82         {
   83             driver.Url = javascriptPage;
   84             IWebElement keysEventInput = driver.FindElement(By.Id("theworks"));
   85 
   86             // Scroll the element into view before attempting any actions on it.
   87             ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView();", keysEventInput);
   88 
   89             IAction pressShift = new Actions(driver).KeyDown(keysEventInput, Keys.Shift).Build();
   90             pressShift.Perform();
   91 
   92             IWebElement keyLoggingElement = driver.FindElement(By.Id("result"));
   93 
   94             string eventsText = keyLoggingElement.Text;
   95             Assert.That(keyLoggingElement.Text, Does.EndWith("keydown"));
   96 
   97             IAction releaseShift = new Actions(driver).KeyUp(keysEventInput, Keys.Shift).Build();
   98 
   99             releaseShift.Perform();
  100 
  101             eventsText = keyLoggingElement.Text;
  102             Assert.That(keyLoggingElement.Text, Does.EndWith("keyup"));
  103         }
  104 
  105         [Test]
  106         [IgnoreBrowser(Browser.IE, "Keypress and Keyup are getting switched")]
  107         [IgnoreBrowser(Browser.Remote, "API not implemented in driver")]
  108         public void ShouldAllowSendingKeysWithShiftPressed()
  109         {
  110             driver.Url = javascriptPage;
  111 
  112             IWebElement keysEventInput = driver.FindElement(By.Id("theworks"));
  113 
  114             keysEventInput.Click();
  115 
  116             IAction pressShift = new Actions(driver).KeyDown(Keys.Shift).Build();
  117             pressShift.Perform();
  118 
  119             IAction sendLowercase = new Actions(driver).SendKeys("ab").Build();
  120             sendLowercase.Perform();
  121 
  122             IAction releaseShift = new Actions(driver).KeyUp(Keys.Shift).Build();
  123             releaseShift.Perform();
  124 
  125             AssertThatFormEventsFiredAreExactly("focus keydown keydown keypress keyup keydown keypress keyup keyup");
  126 
  127             Assert.AreEqual("AB", keysEventInput.GetAttribute("value"));
  128         }
  129 
  130         [Test]
  131         [IgnoreBrowser(Browser.Remote, "API not implemented in driver")]
  132         public void ShouldAllowSendingKeysToActiveElement()
  133         {
  134             driver.Url = bodyTypingPage;
  135 
  136             Actions actionProvider = new Actions(driver);
  137             IAction someKeys = actionProvider.SendKeys("ab").Build();
  138             someKeys.Perform();
  139 
  140             AssertThatBodyEventsFiredAreExactly("keypress keypress");
  141             IWebElement formLoggingElement = driver.FindElement(By.Id("result"));
  142             AssertThatFormEventsFiredAreExactly(string.Empty);
  143         }
  144 
  145         [Test]
  146         public void ThrowsIllegalArgumentExceptionWithNullKeys()
  147         {
  148             driver.Url = javascriptPage;
  149             Assert.That(() => driver.FindElement(By.Id("keyReporter")).SendKeys(null), Throws.InstanceOf<ArgumentNullException>());
  150         }
  151 
  152         [Test]
  153         public void CanGenerateKeyboardShortcuts()
  154         {
  155             driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("keyboard_shortcut.html");
  156 
  157             IWebElement body = driver.FindElement(By.XPath("//body"));
  158             AssertBackgroundColor(body, Color.White);
  159 
  160             new Actions(driver).KeyDown(Keys.Shift).SendKeys("1").KeyUp(Keys.Shift).Perform();
  161             AssertBackgroundColor(body, Color.Green);
  162 
  163             new Actions(driver).KeyDown(Keys.Alt).SendKeys("1").KeyUp(Keys.Alt).Perform();
  164             AssertBackgroundColor(body, Color.LightBlue);
  165 
  166             new Actions(driver)
  167                 .KeyDown(Keys.Shift).KeyDown(Keys.Alt)
  168                 .SendKeys("1")
  169                 .KeyUp(Keys.Shift).KeyUp(Keys.Alt)
  170                 .Perform();
  171             AssertBackgroundColor(body, Color.Silver);
  172         }
  173 
  174         [Test]
  175         public void SelectionSelectBySymbol()
  176         {
  177             driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("single_text_input.html");
  178 
  179             IWebElement input = driver.FindElement(By.Id("textInput"));
  180 
  181             new Actions(driver).Click(input).SendKeys("abc def").Perform();
  182 
  183             WaitFor(() => input.GetAttribute("value") == "abc def", "did not send initial keys");
  184 
  185             if (!TestUtilities.IsInternetExplorer(driver))
  186             {
  187                 // When using drivers other than the IE, the click in
  188                 // the below action sequence may fall inside the double-
  189                 // click threshold (the IE driver has guards to prevent
  190                 // inadvertent double-clicks with multiple actions calls),
  191                 // so we call the "release actions" end point before
  192                 // doing the second action.
  193                 IActionExecutor executor = driver as IActionExecutor;
  194                 if (executor != null)
  195                 {
  196                     executor.ResetInputState();
  197                 }
  198             }
  199 
  200             new Actions(driver).Click(input)
  201                 .KeyDown(Keys.Shift)
  202                 .SendKeys(Keys.Left)
  203                 .SendKeys(Keys.Left)
  204                 .KeyUp(Keys.Shift)
  205                 .SendKeys(Keys.Delete)
  206                 .Perform();
  207 
  208             Assert.That(input.GetAttribute("value"), Is.EqualTo("abc d"));
  209         }
  210 
  211         [Test]
  212         public void SelectionSelectByWord()
  213         {
  214             string controlModifier = Keys.Control;
  215             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  216             {
  217                 controlModifier = Keys.Alt;
  218             }
  219 
  220             driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("single_text_input.html");
  221 
  222             IWebElement input = driver.FindElement(By.Id("textInput"));
  223 
  224             new Actions(driver).Click(input).SendKeys("abc def").Perform();
  225 
  226             WaitFor(() => input.GetAttribute("value") == "abc def", "did not send initial keys");
  227 
  228             if (!TestUtilities.IsInternetExplorer(driver))
  229             {
  230                 // When using drivers other than the IE, the click in
  231                 // the below action sequence may fall inside the double-
  232                 // click threshold (the IE driver has guards to prevent
  233                 // inadvertent double-clicks with multiple actions calls),
  234                 // so we call the "release actions" end point before
  235                 // doing the second action.
  236                 IActionExecutor executor = driver as IActionExecutor;
  237                 if (executor != null)
  238                 {
  239                     executor.ResetInputState();
  240                 }
  241             }
  242 
  243             new Actions(driver).Click(input)
  244                 .KeyDown(Keys.Shift)
  245                 .KeyDown(controlModifier)
  246                 .SendKeys(Keys.Left)
  247                 .KeyUp(controlModifier)
  248                 .KeyUp(Keys.Shift)
  249                 .SendKeys(Keys.Delete)
  250                 .Perform();
  251 
  252             WaitFor(() => input.GetAttribute("value") == "abc ", "did not send editing keys");
  253         }
  254 
  255         [Test]
  256         public void SelectionSelectAll()
  257         {
  258             string controlModifier = Keys.Control;
  259             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  260             {
  261                 controlModifier = Keys.Command;
  262             }
  263 
  264             driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("single_text_input.html");
  265 
  266             IWebElement input = driver.FindElement(By.Id("textInput"));
  267 
  268             new Actions(driver).Click(input).SendKeys("abc def").Perform();
  269 
  270             WaitFor(() => input.GetAttribute("value") == "abc def", "did not send initial keys");
  271 
  272             new Actions(driver).Click(input)
  273                 .KeyDown(controlModifier)
  274                 .SendKeys("a")
  275                 .KeyUp(controlModifier)
  276                 .SendKeys(Keys.Delete)
  277                 .Perform();
  278 
  279             Assert.That(input.GetAttribute("value"), Is.EqualTo(string.Empty));
  280         }
  281 
  282         //------------------------------------------------------------------
  283         // Tests below here are not included in the Java test suite
  284         //------------------------------------------------------------------
  285         [Test]
  286         [IgnoreBrowser(Browser.Remote, "API not implemented in driver")]
  287         public void ShouldAllowSendingKeysWithLeftShiftPressed()
  288         {
  289             driver.Url = javascriptPage;
  290 
  291             IWebElement keysEventInput = driver.FindElement(By.Id("theworks"));
  292 
  293             keysEventInput.Click();
  294 
  295             IAction pressShift = new Actions(driver).KeyDown(Keys.LeftShift).Build();
  296             pressShift.Perform();
  297 
  298             IAction sendLowercase = new Actions(driver).SendKeys("ab").Build();
  299             sendLowercase.Perform();
  300 
  301             IAction releaseShift = new Actions(driver).KeyUp(Keys.LeftShift).Build();
  302             releaseShift.Perform();
  303 
  304             AssertThatFormEventsFiredAreExactly("focus keydown keydown keypress keyup keydown keypress keyup keyup");
  305 
  306             Assert.AreEqual("AB", keysEventInput.GetAttribute("value"));
  307         }
  308 
  309         private void AssertThatFormEventsFiredAreExactly(string message, string expected)
  310         {
  311             Assert.AreEqual(expected, driver.FindElement(By.Id("result")).Text.Trim(), message);
  312         }
  313 
  314         private void AssertThatFormEventsFiredAreExactly(string expected)
  315         {
  316             AssertThatFormEventsFiredAreExactly(string.Empty, expected);
  317         }
  318 
  319         private void AssertThatBodyEventsFiredAreExactly(string expected)
  320         {
  321             Assert.AreEqual(expected, driver.FindElement(By.Id("body_result")).Text.Trim());
  322         }
  323 
  324         private Func<bool> BackgroundColorToChangeFrom(IWebElement element, Color currentColor)
  325         {
  326             return () =>
  327             {
  328                 string hexValue = string.Format("#{0:x2}{1:x2}{2:x2}", currentColor.R, currentColor.G, currentColor.B);
  329                 string rgbValue = string.Format("rgb({0}, {1}, {2})", currentColor.R, currentColor.G, currentColor.B);
  330                 string rgbaValue = string.Format("rgba({0}, {1}, {2}, 1)", currentColor.R, currentColor.G, currentColor.B);
  331                 string actual = element.GetCssValue("background-color");
  332                 return actual != hexValue && actual != rgbValue && actual != rgbaValue;
  333             };
  334         }
  335 
  336         private void AssertBackgroundColor(IWebElement el, Color expected)
  337         {
  338             string hexValue = string.Format("#{0:x2}{1:x2}{2:x2}", expected.R, expected.G, expected.B);
  339             string rgbValue = string.Format("rgb({0}, {1}, {2})", expected.R, expected.G, expected.B);
  340             string rgbaValue = string.Format("rgba({0}, {1}, {2}, 1)", expected.R, expected.G, expected.B);
  341             string actual = el.GetCssValue("background-color");
  342             Assert.That(actual, Is.EqualTo(hexValue).Or.EqualTo(rgbValue).Or.EqualTo(rgbaValue));
  343         }
  344     }
  345 }