【问题标题】:Is it a good practise to initiate one object per all page tests?每个页面测试都启动一个对象是一种好习惯吗?
【发布时间】:2018-11-30 13:46:50
【问题描述】:

我正在使用 Selenium 和页面对象模式。我有一个关于创建页面对象类的对象的问题。

哪个选项更好:

@BeforeTest
public void browser() throws IOException {
    driver = initializeBrowser();
    loginPage = new LoginPage(driver);
}

并像这样使用它:

@Test
public void loginToApp() throws InterruptedException {
    loginPage.clickLoginButton();
    Assert.assertTrue("some assertion");
}

@Test
public void loginToAppUsingLogin() throws IOException {
    loginPage.sendLogin("login");
    loginPage.sendPassword("password");
    loginPage.clickLoginButton();

    Assert.assertTrue("some assertion");
}

或者

 @BeforeTest
 public void browser() throws IOException {
     driver = initializeBrowser();
 }


 @Test
 public void loginToApp() throws InterruptedException {
     loginPage = new LoginPage(driver);
     loginPage.clickLoginButton();
     Assert.assertTrue("some assertion");
 }

 @Test
 public void loginToAppUsingLogin() throws IOException {
     loginPage = new LoginPage(driver);
     loginPage.sendLogin("login");
     loginPage.sendPassword("password");
     loginPage.clickLoginButton();

     Assert.assertTrue("some assertion");
 }

每个测试套件在@BeforeTest 中创建一个对象是否有任何禁忌?

【问题讨论】:

  • 我会说这取决于你正在测试的东西。 LoginPage 是有状态的吗? IE。您在一次测试中与它进行的交互会在另一次测试中搞砸吗?如果是我,我不会以上述任何一种的方式去做;我只是在现场声明它。 private final LoginPage loginPage = new LoginPage(this.driver);.

标签: java selenium selenium-webdriver pageobjects


【解决方案1】:

我不知道同意是什么,但 @BeforeTest 注释像你一样正确使用。它会在每次单独测试之前初始化您的 loginPage 对象。

(我假设你使用 TestNG)

根据我的经验,您的第一种方法更好,因为它还减少了重复代码的数量。见DRY

【讨论】:

  • 但是 BeforeTest 只被调用一次。它的行为不像 @BeforeMethod 注释。所以在第一种情况下,我们只创建一个对象,在第二种情况下,每 1 个测试注释 1 个对象
  • 哦,对不起,我把它和JUnit的@Before注解弄混了。
  • @jamal 那么您可以使用@BeforeMethod 注释,并在方法中包含loginPage = new LoginPage(driver); 语句。
  • 对不起,但这不是我问题的答案。我知道我可以这样使用它。但是,为每个测试套件声明一个页面对象是一种好习惯,还是最好在每个测试中删除 1 个页面对象,或者第三个选项无关紧要
  • 然后查看@user2478398 在原始问题下的评论。我认为他的回答是正确的。答案是,这取决于。如果它有状态,它应该在每次测试之前重置,否则你只能初始化一次
【解决方案2】:

在我看来,我认为你在这里分裂头发。对我来说,我更喜欢每个测试创建一个新对象,因为它提供了一个“干净”的运行,也就是说,我不会为新测试重复使用同一个实例。为了更加清晰/透明,我每次都会清除浏览器上的缓存。

在每个测试中我都会这样做:

[Test, Order(10), Description("Navigate to the 'Dashboard' page, click the 'Open' button and fill out the form that loads.")]
public void NavigateToDashboardAndClickElement()
{
   //  Setup a null instance of IE for use in testing.
   IWebDriver driver = null;
   //  Instantiate the IESetupHelper class.
   IESetupHelper setupIE = new IESetupHelper();
   //  Set the environment variables for IE, and launch the browser.
   setupIE.SetupIEVariables(driver);
}

为了设置浏览器本身,我执行以下操作:

public void SetupIEVariables(IWebDriver driver)
{
   //  Set the options for the driver instance.  In this case, we are ignoring the zoom level of the browswer we are going to use.
   InternetExplorerOptions options = new InternetExplorerOptions { IgnoreZoomLevel = true };
   //  Clear the broswers cache before launching.
   options.EnsureCleanSession = true;
   //  Create a new driver instance.
   driver = new InternetExplorerDriver(@"path to driver here", options);
   //  Set the window size for the driver instance to full screen.
   driver.Manage().Window.Maximize();
   //  Set the URL for the driver instance to go to.
   driver.Url = @"some URL here";
}

【讨论】:

    猜你喜欢
    • 2016-01-03
    • 1970-01-01
    • 2022-10-05
    • 2019-01-23
    • 1970-01-01
    • 2017-08-09
    • 2016-01-03
    • 2012-08-11
    相关资源
    最近更新 更多