我正在将Selenium与Page Object Pattern一起使用。我对创建页面对象类的对象有疑问。
哪个选项更好:
@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中创建一个对象是否有禁忌?
答案 0 :(得分:0)
我不知道同意是什么,但是@BeforeTest批注像您一样正确使用。它将在每次单独测试之前初始化您的loginPage对象。
(我假设您使用的是TestNG)
以我的经验,您的第一种方法更好,因为它也减少了重复代码的数量。参见DRY
答案 1 :(得分:0)
在我看来,我认为您在这里劈头发。对我来说,我更喜欢创建一个新的对象每个测试,因为它提供了“干净的”运行,即,我不会在新测试中重复使用同一实例。为了更加清晰/透明,我也每次都清除浏览器上的缓存。
在每次测试中,我都会这样做:
[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";
}