Guest User

Untitled

a guest
Nov 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace OpenQA.Selenium.Support.UI
  4. {
  5. public class Select
  6. {
  7.  
  8. private readonly IWebElement element;
  9.  
  10. public bool Multiple { get; private set; }
  11.  
  12. /// <summary>
  13. /// Create a new instance of a Select object.
  14. /// </summary>
  15. /// <param name="element">The element to be wrapped</param>
  16. public Select(IWebElement element)
  17. {
  18. if (element.TagName ==null || element.TagName.ToLower() != "select")
  19. throw new UnexpectedTagNameException("select", element.TagName);
  20.  
  21. this.element = element;
  22.  
  23. //let check if it's a multiple
  24. string attribute = element.GetAttribute("multiple");
  25. Multiple = attribute != null && attribute.ToLower().Equals("multiple");
  26. }
  27.  
  28. /// <summary>
  29. /// Fetch the lsit of options for the Check.
  30. /// </summary>
  31. /// <returns>List of <see cref="IWebElement"/>.</returns>
  32. public IList<IWebElement> GetOptions()
  33. {
  34. return element.FindElements(By.TagName("option"));
  35. }
  36.  
  37. /// <summary>
  38. /// Select the option by the text displayed.
  39. /// </summary>
  40. /// <param name="text">The text of the option to be selected.</param>
  41. public void SelectByText(string text)
  42. {
  43. foreach (IWebElement option in GetOptions())
  44. {
  45. if (option.Text == text)
  46. {
  47. option.Select();
  48. if(!Multiple) return;
  49. }
  50. }
  51. }
  52.  
  53. /// <summary>
  54. /// Select an option by the value.
  55. /// </summary>
  56. /// <param name="value">The value of the option to be selected.</param>
  57. public void SelectByValue(string value)
  58. {
  59. foreach (IWebElement option in GetOptions())
  60. {
  61. if (option.Value == value)
  62. {
  63. option.Select();
  64. if (!Multiple) return;
  65. }
  66. }
  67. }
  68.  
  69. /// <summary>
  70. /// Select the option by the index.
  71. /// </summary>
  72. /// <param name="index">The index of the option to be selected.</param>
  73. public void SelectByIndex(int index)
  74. {
  75. foreach (IWebElement option in GetOptions())
  76. {
  77. if (option.GetAttribute("index").Equals(index.ToString()))
  78. {
  79. option.Select();
  80. if (!Multiple) return;
  81. }
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// Deselect the option by the text displayed.
  87. /// </summary>
  88. /// <param name="text">The text of the option to be deselected.</param>
  89. public void DeselectByText(string text)
  90. {
  91. foreach (IWebElement option in GetOptions())
  92. {
  93. if (option.Text.Equals(text))
  94. {
  95. if (option.Selected) option.Toggle();
  96. if (!Multiple) return;
  97. }
  98. }
  99. }
  100.  
  101. /// <summary>
  102. /// Deselect the option by the value.
  103. /// </summary>
  104. /// <param name="value">The value of the option to deselect.</param>
  105. public void DeselectByValue(string value)
  106. {
  107. foreach (IWebElement option in GetOptions())
  108. {
  109. if (option.Value.Equals(value))
  110. {
  111. if (option.Selected) option.Toggle();
  112. if (!Multiple) return;
  113. }
  114. }
  115. }
  116.  
  117. /// <summary>
  118. /// Deselect the option by the index.
  119. /// </summary>
  120. /// <param name="index">The index of the option to deselect.</param>
  121. public void DeselectByIndex(int index)
  122. {
  123. foreach (IWebElement option in GetOptions())
  124. {
  125. if (option.GetAttribute("index").Equals(index.ToString()))
  126. {
  127. if (option.Selected) option.Toggle();
  128. if (!Multiple) return;
  129. }
  130. }
  131. }
  132.  
  133. /// <summary>
  134. /// Get the selected item.
  135. /// </summary>
  136. /// <returns>The <see cref="IWebElement"/> that is selected.</returns>
  137. /// <remarks>If more than one item is selected this will return the first item.</remarks>
  138. public IWebElement GetSelected()
  139. {
  140. foreach (IWebElement option in GetOptions())
  141. {
  142. if (option.Selected) return option;
  143. }
  144.  
  145. throw new NoSuchElementException("No options are selected");
  146. }
  147.  
  148. /// <summary>
  149. /// Get the all the selected options within the select element.
  150. /// </summary>
  151. /// <returns>A list of <see cref="IWebElement"/>.</returns>
  152. public IList<IWebElement> GetAllSelectedOption()
  153. {
  154. List<IWebElement> returnValue = new List<IWebElement>();
  155. foreach (IWebElement option in GetOptions())
  156. {
  157. if (option.Selected) returnValue.Add(option);
  158. }
  159.  
  160. return returnValue;
  161. }
  162.  
  163. }
  164. }
Add Comment
Please, Sign In to add comment