"Fossies" - the Fresh Open Source Software Archive

Member "selenium-selenium-4.8.1/dotnet/test/support/UI/SelectBrowserTests.cs" (17 Feb 2023, 14940 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 "SelectBrowserTests.cs": 4.5.0_vs_4.6.0.

    1 using System;
    2 using System.Collections.Generic;
    3 using NUnit.Framework;
    4 using OpenQA.Selenium.Environment;
    5 
    6 namespace OpenQA.Selenium.Support.UI
    7 {
    8     [TestFixture]
    9     public class SelectBrowserTests : DriverTestFixture
   10     {
   11         [OneTimeSetUp]
   12         public void RunBeforeAnyTest()
   13         {
   14             EnvironmentManager.Instance.WebServer.Start();
   15         }
   16 
   17         [OneTimeTearDown]
   18         public void RunAfterAnyTests()
   19         {
   20             EnvironmentManager.Instance.CloseCurrentDriver();
   21             EnvironmentManager.Instance.WebServer.Stop();
   22         }
   23 
   24         [SetUp]
   25         public void Setup()
   26         {
   27             driver.Url = formsPage;
   28         }
   29 
   30         [Test]
   31         public void ShouldThrowAnExceptionIfTheElementIsNotASelectElement()
   32         {
   33             IWebElement element = driver.FindElement(By.Name("checky"));
   34             Assert.Throws<UnexpectedTagNameException>(() => { SelectElement elementWrapper = new SelectElement(element); });
   35         }
   36 
   37         [Test]
   38         public void ShouldIndicateThatASelectCanSupportMultipleOptions()
   39         {
   40             IWebElement element = driver.FindElement(By.Name("multi"));
   41             SelectElement elementWrapper = new SelectElement(element);
   42             Assert.IsTrue(elementWrapper.IsMultiple);
   43         }
   44 
   45         [Test]
   46         public void ShouldIndicateThatASelectCanSupportMultipleOptionsWithEmptyMultipleAttribute()
   47         {
   48             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
   49             SelectElement elementWrapper = new SelectElement(element);
   50             Assert.IsTrue(elementWrapper.IsMultiple);
   51         }
   52 
   53         [Test]
   54         public void ShouldIndicateThatASelectCanSupportMultipleOptionsWithTrueMultipleAttribute()
   55         {
   56             IWebElement element = driver.FindElement(By.Name("multi_true"));
   57             SelectElement elementWrapper = new SelectElement(element);
   58             Assert.IsTrue(elementWrapper.IsMultiple);
   59         }
   60 
   61         [Test]
   62         public void ShouldNotIndicateThatANormalSelectSupportsMulitpleOptions()
   63         {
   64             IWebElement element = driver.FindElement(By.Name("selectomatic"));
   65             SelectElement elementWrapper = new SelectElement(element);
   66             Assert.IsFalse(elementWrapper.IsMultiple);
   67         }
   68 
   69         [Test]
   70         public void ShouldIndicateThatASelectCanSupportMultipleOptionsWithFalseMultipleAttribute()
   71         {
   72             IWebElement element = driver.FindElement(By.Name("multi_false"));
   73             SelectElement elementWrapper = new SelectElement(element);
   74             Assert.IsTrue(elementWrapper.IsMultiple);
   75         }
   76 
   77         [Test]
   78         public void ShouldReturnAllOptionsWhenAsked()
   79         {
   80             IWebElement element = driver.FindElement(By.Name("selectomatic"));
   81             SelectElement elementWrapper = new SelectElement(element);
   82             IList<IWebElement> returnedOptions = elementWrapper.Options;
   83 
   84             Assert.AreEqual(4, returnedOptions.Count);
   85 
   86             string one = returnedOptions[0].Text;
   87             Assert.AreEqual("One", one);
   88 
   89             string two = returnedOptions[1].Text;
   90             Assert.AreEqual("Two", two);
   91 
   92             string three = returnedOptions[2].Text;
   93             Assert.AreEqual("Four", three);
   94 
   95             string four = returnedOptions[3].Text;
   96             Assert.AreEqual("Still learning how to count, apparently", four);
   97 
   98         }
   99 
  100         [Test]
  101         public void ShouldReturnOptionWhichIsSelected()
  102         {
  103             IWebElement element = driver.FindElement(By.Name("selectomatic"));
  104             SelectElement elementWrapper = new SelectElement(element);
  105 
  106             IList<IWebElement> returnedOptions = elementWrapper.AllSelectedOptions;
  107 
  108             Assert.AreEqual(1, returnedOptions.Count);
  109 
  110             string one = returnedOptions[0].Text;
  111             Assert.AreEqual("One", one);
  112         }
  113 
  114         [Test]
  115         public void ShouldReturnOptionsWhichAreSelected()
  116         {
  117             IWebElement element = driver.FindElement(By.Name("multi"));
  118             SelectElement elementWrapper = new SelectElement(element);
  119 
  120             IList<IWebElement> returnedOptions = elementWrapper.AllSelectedOptions;
  121 
  122             Assert.AreEqual(2, returnedOptions.Count);
  123 
  124             string one = returnedOptions[0].Text;
  125             Assert.AreEqual("Eggs", one);
  126 
  127             string two = returnedOptions[1].Text;
  128             Assert.AreEqual("Sausages", two);
  129         }
  130 
  131         [Test]
  132         public void ShouldReturnFirstSelectedOption()
  133         {
  134             IWebElement element = driver.FindElement(By.Name("multi"));
  135             SelectElement elementWrapper = new SelectElement(element);
  136 
  137             IWebElement firstSelected = elementWrapper.AllSelectedOptions[0];
  138 
  139             Assert.AreEqual("Eggs", firstSelected.Text);
  140         }
  141 
  142         // [Test]
  143         // [ExpectedException(typeof(NoSuchElementException))]
  144         // The .NET bindings do not have a "FirstSelectedOption" property,
  145         // and no one has asked for it to this point. Given that, this test
  146         // is not a valid test.
  147         public void ShouldThrowANoSuchElementExceptionIfNothingIsSelected()
  148         {
  149             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  150             SelectElement elementWrapper = new SelectElement(element);
  151 
  152             Assert.AreEqual(0, elementWrapper.AllSelectedOptions.Count);
  153         }
  154 
  155         [Test]
  156         public void ShouldAllowOptionsToBeSelectedByVisibleText()
  157         {
  158             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  159             SelectElement elementWrapper = new SelectElement(element);
  160             elementWrapper.SelectByText("select_2");
  161             IWebElement firstSelected = elementWrapper.AllSelectedOptions[0];
  162             Assert.AreEqual("select_2", firstSelected.Text);
  163         }
  164 
  165         [Test]
  166         public void ShouldAllowOptionsToBeSelectedByPartialText()
  167         {
  168             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  169             SelectElement elementWrapper = new SelectElement(element);
  170             elementWrapper.SelectByText("4", true);
  171             IWebElement firstSelected = elementWrapper.AllSelectedOptions[0];
  172             Assert.AreEqual("select_4", firstSelected.Text);
  173         }
  174 
  175         [Test]
  176         public void ShouldThrowExceptionOnSelectByTextExactMatchIfOptionDoesNotExist()
  177         {
  178             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  179             SelectElement elementWrapper = new SelectElement(element);
  180             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByText("4"));
  181         }
  182 
  183         [Test]
  184         public void ShouldNotAllowInvisibleOptionsToBeSelectedByVisibleText()
  185         {
  186             IWebElement element = driver.FindElement(By.Name("invisi_select"));
  187             SelectElement elementWrapper = new SelectElement(element);
  188             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByText("Apples"));
  189         }
  190 
  191         [Test]
  192         public void ShouldThrowExceptionOnSelectByVisibleTextIfOptionDoesNotExist()
  193         {
  194             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  195             SelectElement elementWrapper = new SelectElement(element);
  196             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByText("not there"));
  197         }
  198 
  199         [Test]
  200         public void ShouldThrowExceptionOnSelectByVisibleTextIfOptionDisabled()
  201         {
  202             IWebElement element = driver.FindElement(By.Name("single_disabled"));
  203             SelectElement elementWrapper = new SelectElement(element);
  204             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByText("Disabled"));
  205         }
  206 
  207         [Test]
  208         public void ShouldAllowOptionsToBeSelectedByIndex()
  209         {
  210             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  211             SelectElement elementWrapper = new SelectElement(element);
  212             elementWrapper.SelectByIndex(1);
  213             IWebElement firstSelected = elementWrapper.AllSelectedOptions[0];
  214             Assert.AreEqual("select_2", firstSelected.Text);
  215         }
  216 
  217         [Test]
  218         public void ShouldThrowExceptionOnSelectByIndexIfOptionDoesNotExist()
  219         {
  220             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  221             SelectElement elementWrapper = new SelectElement(element);
  222             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByIndex(10));
  223         }
  224 
  225         [Test]
  226         public void ShouldThrowExceptionOnSelectByIndexIfOptionDisabled()
  227         {
  228             IWebElement element = driver.FindElement(By.Name("single_disabled"));
  229             SelectElement elementWrapper = new SelectElement(element);
  230             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByIndex(1));
  231         }
  232 
  233         [Test]
  234         public void ShouldAllowOptionsToBeSelectedByReturnedValue()
  235         {
  236             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  237             SelectElement elementWrapper = new SelectElement(element);
  238             elementWrapper.SelectByValue("select_2");
  239             IWebElement firstSelected = elementWrapper.AllSelectedOptions[0];
  240             Assert.AreEqual("select_2", firstSelected.Text);
  241         }
  242 
  243         [Test]
  244         public void ShouldThrowExceptionOnSelectByReturnedValueIfOptionDoesNotExist()
  245         {
  246             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  247             SelectElement elementWrapper = new SelectElement(element);
  248             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByValue("not there"));
  249         }
  250 
  251         [Test]
  252         public void ShouldThrowExceptionOnSelectByReturnedValueIfOptionDisabled()
  253         {
  254             IWebElement element = driver.FindElement(By.Name("single_disabled"));
  255             SelectElement elementWrapper = new SelectElement(element);
  256             Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByValue("disabled"));
  257         }
  258 
  259         [Test]
  260         public void ShouldAllowUserToDeselectAllWhenSelectSupportsMultipleSelections()
  261         {
  262             IWebElement element = driver.FindElement(By.Name("multi"));
  263             SelectElement elementWrapper = new SelectElement(element);
  264             elementWrapper.DeselectAll();
  265             IList<IWebElement> returnedOptions = elementWrapper.AllSelectedOptions;
  266 
  267             Assert.AreEqual(0, returnedOptions.Count);
  268         }
  269 
  270         [Test]
  271         public void ShouldNotAllowUserToDeselectAllWhenSelectDoesNotSupportMultipleSelections()
  272         {
  273             IWebElement element = driver.FindElement(By.Name("selectomatic"));
  274             SelectElement elementWrapper = new SelectElement(element);
  275             Assert.Throws<InvalidOperationException>(() => elementWrapper.DeselectAll());
  276         }
  277 
  278         [Test]
  279         public void ShouldAllowUserToDeselectOptionsByVisibleText()
  280         {
  281             IWebElement element = driver.FindElement(By.Name("multi"));
  282             SelectElement elementWrapper = new SelectElement(element);
  283             elementWrapper.DeselectByText("Eggs");
  284             IList<IWebElement> returnedOptions = elementWrapper.AllSelectedOptions;
  285 
  286             Assert.AreEqual(1, returnedOptions.Count);
  287         }
  288 
  289         [Test]
  290 
  291         public void ShouldNotAllowUserToDeselectOptionsByInvisibleText()
  292         {
  293             IWebElement element = driver.FindElement(By.Name("invisi_select"));
  294             SelectElement elementWrapper = new SelectElement(element);
  295             Assert.Throws<NoSuchElementException>(() => elementWrapper.DeselectByText("Apples"));
  296         }
  297 
  298         [Test]
  299         public void ShouldAllowOptionsToBeDeselectedByIndex()
  300         {
  301             IWebElement element = driver.FindElement(By.Name("multi"));
  302             SelectElement elementWrapper = new SelectElement(element);
  303             elementWrapper.DeselectByIndex(0);
  304             IList<IWebElement> returnedOptions = elementWrapper.AllSelectedOptions;
  305 
  306             Assert.AreEqual(1, returnedOptions.Count);
  307         }
  308 
  309         [Test]
  310         public void ShouldAllowOptionsToBeDeselectedByReturnedValue()
  311         {
  312             IWebElement element = driver.FindElement(By.Name("multi"));
  313             SelectElement elementWrapper = new SelectElement(element);
  314             elementWrapper.DeselectByValue("eggs");
  315             IList<IWebElement> returnedOptions = elementWrapper.AllSelectedOptions;
  316 
  317             Assert.AreEqual(1, returnedOptions.Count);
  318         }
  319 
  320         [Test]
  321         public void ShouldThrowExceptionOnDeselectByReturnedValueIfOptionDoesNotExist()
  322         {
  323             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  324             SelectElement elementWrapper = new SelectElement(element);
  325             Assert.Throws<NoSuchElementException>(() => elementWrapper.DeselectByValue("not there"));
  326         }
  327 
  328         [Test]
  329         public void ShouldThrowExceptionOnDeselectByTextIfOptionDoesNotExist()
  330         {
  331             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  332             SelectElement elementWrapper = new SelectElement(element);
  333             Assert.Throws<NoSuchElementException>(() => elementWrapper.DeselectByText("not there"));
  334         }
  335 
  336         [Test]
  337         public void ShouldThrowExceptionOnDeselectByIndexIfOptionDoesNotExist()
  338         {
  339             IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
  340             SelectElement elementWrapper = new SelectElement(element);
  341             Assert.Throws<NoSuchElementException>(() => elementWrapper.DeselectByIndex(10));
  342         }
  343 
  344         [Test]
  345         public void ShouldNotAllowUserToDeselectByTextWhenSelectDoesNotSupportMultipleSelections()
  346         {
  347             IWebElement element = driver.FindElement(By.Name("selectomatic"));
  348             SelectElement elementWrapper = new SelectElement(element);
  349             Assert.Throws<InvalidOperationException>(() => elementWrapper.DeselectByText("Four"));
  350         }
  351 
  352         [Test]
  353         public void ShouldNotAllowUserToDeselectByValueWhenSelectDoesNotSupportMultipleSelections()
  354         {
  355             IWebElement element = driver.FindElement(By.Name("selectomatic"));
  356             SelectElement elementWrapper = new SelectElement(element);
  357             Assert.Throws<InvalidOperationException>(() => elementWrapper.DeselectByValue("two"));
  358         }
  359 
  360         [Test]
  361         public void ShouldNotAllowUserToDeselectByIndexWhenSelectDoesNotSupportMultipleSelections()
  362         {
  363             IWebElement element = driver.FindElement(By.Name("selectomatic"));
  364             SelectElement elementWrapper = new SelectElement(element);
  365             Assert.Throws<InvalidOperationException>(() => elementWrapper.DeselectByIndex(0));
  366         }
  367     }
  368 }