在C# Selenium中实现Page-Object模型 – PageFactory

huangapple go评论103阅读模式
英文:

Implement Page-Object models in C# Selenium - PageFactory

问题

  1. 我对测试自动化非常新手。
  2. 1. 我使用的是 Visual Studio 2022,版本 17.6.2
  3. 2. 我安装了 NuGet 包:Selenium.WebDriverSelenium.SupportSelenium.Chrome.WebDriverGherkinNUnitNUnit3TestAdapterxunitBoDiSpecFlow.Internal.Json
  4. 我尝试使用在网上找到的示例来实现 POM 模型测试。
  5. **POM:**
  6. ```csharp
  7. // 代码部分不翻译

测试:

  1. // 代码部分不翻译

然而,POM 指示出现了错误:

在命名空间 'OpenQA.Selenium.Support' 中找不到 'PageObjects' 类型或命名空间。

当前上下文中不存在 'PageFactory' 的名称。

等等...

请参见下面的截图:

在C# Selenium中实现Page-Object模型 – PageFactory

using OpenQA.Selenium.Support.PageObjects;

我是否正确理解 PageFactory 现在已被弃用?
这是原因吗?
我可以使用什么替代?
有人能给我一个链接或建议吗?

  1. <details>
  2. <summary>英文:</summary>
  3. I am very new to the test automation.
  4. 1. I run Visual Studio 2022, Version 17.6.2
  5. 2. I have NuGet packages: Selenium.WebDriver, Selenium.Support, Selenium.Chrome.WebDriver,
  6. Gherkin, NUnit, NUnit3TestAdapter, xunit, BoDi, SpecFlow.Internal.Json
  7. I try to implement the POM model testing using an example I found on the web.
  8. **POM:**
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using OpenQA.Selenium.Support.PageObjects;
  15. using OpenQA.Selenium;
  16. namespace POMExample.PageObjects
  17. {
  18. internal class HomePage
  19. {
  20. private IWebDriver driver;
  21. public HomePage(IWebDriver driver)
  22. {
  23. this.driver = driver;
  24. PageFactory.InitElements(driver, this);
  25. }
  26. [FindsBy(How = How.CssSelector, Using = &quot;.fusion-main-menu a[href*=&#39;about&#39;]&quot;)]
  27. private IWebElement about;
  28. [FindsBy(How = How.ClassName, Using = &quot;fusion-main-menu-icon&quot;)]
  29. private IWebElement searchIcon;
  30. public void goToPage()
  31. {
  32. driver.Navigate().GoToUrl(&quot;http://www.swtestacademy.com&quot;);
  33. }
  34. public AboutPage goToAboutPage()
  35. {
  36. about.Click();
  37. return new AboutPage(driver);
  38. }
  39. }
  40. }
  41. **Test**
  42. using NUnit.Framework;
  43. using OpenQA.Selenium;
  44. using OpenQA.Selenium.Chrome;
  45. using POMExample.PageObjects;
  46. using System;
  47. using System.Collections.Generic;
  48. using System.Linq;
  49. using System.Text;
  50. using System.Threading.Tasks;
  51. namespace POMExample
  52. {
  53. public class TestClass
  54. {
  55. private IWebDriver driver;
  56. [SetUp]
  57. public void SetUp()
  58. {
  59. driver = new ChromeDriver();
  60. driver.Manage().Window.Maximize();
  61. }
  62. [Test]
  63. public void SearchTextFromAboutPage()
  64. {
  65. HomePage home = new HomePage(driver);
  66. home.goToPage();
  67. AboutPage about = home.goToAboutPage();
  68. ResultPage result = about.search(&quot;selenium c#&quot;);
  69. result.clickOnFirstArticle();
  70. }
  71. [TearDown]
  72. public void TearDown()
  73. {
  74. driver.Close();
  75. }
  76. }
  77. }
  78. However POM Indicates errors:
  79. The type or namespace name &#39;PageObjects&#39; does not exist in the namespace &#39;OpenQA.Selenium.Support&#39;
  80. The name &#39;PageFactory&#39; does not exist in the current context
  81. etc...
  82. see the screenshot below:
  83. [![enter image description here][1]][1]
  84. using OpenQA.Selenium.Support.PageObjects;
  85. Do I understand it right PageFactory is deprecated now ?
  86. Is it the reason?
  87. What can I use instead?
  88. Can somebody give me a link, advice, please
  89. [1]: https://i.stack.imgur.com/qKoes.png
  90. </details>
  91. # 答案1
  92. **得分**: 3
  93. 你是正确的 - Selenium的"PageFactory"从版本3.11开始已经被弃用:
  94. > https://ultimateqa.com/selenium-3-11-released/
  95. >
  96. > 在C#中,从版本3.11开始,PageFactory已经被弃用。这实际上是一个很好的变化,因为它防止了使用Selenium贡献者不推荐的类。此外,不使用PageFactory将防止用户遇到许多在使用简单的运行时元素定位时不会出现的奇怪元素异常。
  97. 链接继续:
  98. > 这绝不意味着你应该停止使用页面对象模式。
  99. > 这是一个完全独立的概念。您可以在不使用PageFactory.cs的情况下使用页面对象模式。后者只是一个与设计模式无关的Selenium实现。
  100. 最后:
  101. > 使用[验收测试驱动自动化](https://ultimateqa.com/acceptance-test-driven-automation/)非常简单。以下是一个符合[页面对象最佳实践](https://techbeacon.com/app-dev-testing/6-rules-high-quality-page-object-patterns)的类的示例:
  102. >
  103. > public class ProductsPage : BasePage
  104. > {
  105. > private readonly string _pageUrlPart;
  106. >
  107. > public ProductsPage(IWebDriver driver) : base(driver)
  108. > {
  109. > _pageUrlPart = "inventory.html";
  110. > }
  111. >
  112. > // 可以使用显式等待通过ExpectedConditions来定位元素
  113. > public bool IsLoaded => Wait.UntilIsDisplayedById("inventory_filter_container");
  114. >
  115. > // 外部测试API无法访问元素
  116. > private IWebElement LogoutLink => _driver.FindElement(By.Id("logout_sidebar_link"));
  117. >
  118. > // 也可以在不使用ExpectedConditions的情况下定位元素
  119. > private IWebElement HamburgerElement => _driver.FindElement(By.ClassName("bm-burger-button"));
  120. >
  121. > public int ProductCount =>
  122. > _driver.FindElements(By.ClassName("inventory_item")).Count;
  123. >
  124. > // 我们使用组合来在另一个页面对象中包含一个页面对象
  125. > public CartComponent Cart => new CartComponent(_driver);
  126. >
  127. > public void Logout()
  128. > {
  129. > HamburgerElement.Click();
  130. > LogoutLink.Click();
  131. > }
  132. > }
  133. <details>
  134. <summary>英文:</summary>
  135. You&#39;re correct - Selenium &quot;PageFactory&quot; is deprecated as of v3.11:
  136. &gt; https://ultimateqa.com/selenium-3-11-released/
  137. &gt;
  138. &gt; PageFactory in C# is deprecated as of version 3.11. This is actually a
  139. &gt; great change because it prevents usage of a class that is not
  140. &gt; recommended by the Selenium contributors. Furthermore, not using
  141. &gt; PageFactory will prevent users from many weird element exceptions that
  142. &gt; aren’t experienced when using a simple runtime element locator.
  143. The link continues:
  144. &gt; This in no way means that you should stop using Page Objects Pattern.
  145. &gt; This is totally a separate concept. You can use Page Object Pattern
  146. &gt; without the PageFactory.cs. The latter is just a Selenium
  147. &gt; implementation that has no relation with a design pattern.
  148. Finally:
  149. &gt; Avoiding PageFactory is very simple using [Acceptance Test Driven
  150. &gt; Automation](https://ultimateqa.com/acceptance-test-driven-automation/).
  151. &gt; Here’s an example of a class that meets all [Page Object best
  152. &gt; practices](https://techbeacon.com/app-dev-testing/6-rules-high-quality-page-object-patterns):
  153. &gt;
  154. &gt; public class ProductsPage : BasePage
  155. &gt; {
  156. &gt; private readonly string _pageUrlPart;
  157. &gt;
  158. &gt; public ProductsPage(IWebDriver driver) : base(driver)
  159. &gt; {
  160. &gt; _pageUrlPart = &quot;inventory.html&quot;;
  161. &gt; }
  162. &gt;
  163. &gt; // An element can be located using ExpectedConditions through an explicit wait
  164. &gt; public bool IsLoaded =&gt; Wait.UntilIsDisplayedById(&quot;inventory_filter_container&quot;);
  165. &gt;
  166. &gt; //elements are not accessible for the external test API
  167. &gt; private IWebElement LogoutLink =&gt; _driver.FindElement(By.Id(&quot;logout_sidebar_link&quot;));
  168. &gt;
  169. &gt; // An element can also be located without ExpectedConditions
  170. &gt; private IWebElement HamburgerElement =&gt; _driver.FindElement(By.ClassName(&quot;bm-burger-button&quot;));
  171. &gt;
  172. &gt; public int ProductCount =&gt;
  173. &gt; _driver.FindElements(By.ClassName(&quot;inventory_item&quot;)).Count;
  174. &gt;
  175. &gt; //We are using Composition to have one page object living in another page object
  176. &gt; public CartComponent Cart =&gt; new CartComponent(_driver);
  177. &gt;
  178. &gt; public void Logout()
  179. &gt; {
  180. &gt; HamburgerElement.Click();
  181. &gt; LogoutLink.Click();
  182. &gt; }
  183. &gt; }
  184. </details>

huangapple
  • 本文由 发表于 2023年7月24日 00:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定