我使用的是带有页面对象模式的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中创建一个对象是否有禁忌症?
我不知道同意是什么,但@BeforeTest注释和您一样使用正确。它在每次单独测试之前初始化loginPage对象。
(我假设你使用TestNG(
根据我的经验,您的第一种方法更好,因为它还减少了重复代码的数量。参见DRY
在我看来,我认为你在这里吹毛求疵。对我来说,我更喜欢为每个测试创建一个新对象,因为它提供了一个"干净"的运行,也就是说,我不会为新测试重新使用同一个实例。为了更加清晰/透明,我每次都会清除浏览器上的缓存。
在每次测试中,我都会这样做:
[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";
}