我有一个继承自相同基类的以下selenium测试套件,当我运行整个测试套件时,如何让测试使用相同的Web驱动程序实例?我也希望孤立地运行每个测试。我相信这将大大减少运行套件所需的时间。
此测试从maven运行,而maven又运行每个测试类。
@RunWith(Suite.class)
@SuiteClasses({
AdminPortalTestSuite.class,
DevPortalTestSuite.class,
CommonTestSuite.class
})
public class SeleniumTestSuite {
}
基类所有测试都继承自
@BeforeClass
public static void setUp() throws Exception {
if (null == baseUrl || !baseUrl.startsWith("https://")) {
baseUrl = "https://localhost:8443";
}
if (null == browser || browser.startsWith("${")) {
browser = "firefox";
}
//retrieve properties including locale.
retrieveProperties();
Thread.sleep(4000);
setUpDriver();
}
@After
public void tearDownAfterTest() {
openUrl(LIST_PARTNERS);
adminPortalLogout();
openUrl(DASHBOARD);
developerPortalLogout();
driver.manage().deleteAllCookies();
}
@AfterClass
public static void tearDown() throws Exception {
BaseFunctionalTestCase.driver.quit();
}
测试示例
public class APApplicationFunctionalTestCase extends BaseFunctionalTestCase {
/**
* Test validation when creating a new application.
*/
@Test
public void testApplicationValidation() {
Assume.assumeTrue(preHtml5);
final String partnerName = randomize("partner");
//create partner
createPartnerThroughAP(partnerName);
adminPortalLogin();
openUrl(ADD_APPLICATION + partnerName);
waitForId("applicationView.applicationName");
findById("submit-button").click();
waitForId("submit-button");
//check validation
assertTrue("Failed to validate application name",
isTextPresent(resolveAPMessage("partner", "application.messages",
"NotEmpty.applicationEditView.applicationView.applicationName")));
assertTrue("Failed to validate application username",
isTextPresent(resolveAPMessage("partner", "application.messages",
"NotEmpty.applicationEditView.applicationView.applicationUserName")));
assertTrue("Failed to validate application password",
isTextPresent(resolveAPMessage("partner", "application.messages",
"Password.applicationEditView.applicationView.applicationPassword")));
assertTrue("Failed to validate application password confirmation",
isTextPresent(resolveAPMessage("partner", "application.messages",
"Length.applicationEditView.applicationPasswordConfirmation")));
}
答案 0 :(得分:3)
这就是我做的。在SeleniumTestSuite中,我添加了一个静态WebDriver并在使用@BeforeClass注释的setUp()方法中实例化它。然后,在我的所有selenium测试继承的Base类中,我添加了一个getDriver()方法,它将尝试从SeleniumTestSuite获取静态驱动程序。如果该驱动程序为null,则会实例化并返回一个新驱动程序。因此,当selenium测试类通过套件运行时,他们将使用SeleniumTestSuite中的驱动程序,当它们单独运行时,它们将使用自己的驱动程序。
SeleniumTestSuite:
@RunWith(Suite.class)
@SuiteClasses({
AbcSeleniumTest.class,
XyzSeleniumTest.class
})
public class SeleniumTestSuite {
private static WebDriver driver;
@BeforeClass
public static void setUp() {
driver = new FirefoxDriver();
}
//driver getter/setter
}
BaseSeleniumTest:
public abstract class BaseSeleniumTest {
public WebDriver getDriver() {
WebDriver driver = SeleniumTestSuite.getDriver();
if(driver != null) {
return driver;
}
return new FirefoxDriver();
}
}
AbcSeleniumTest:
public class AbcSeleniumTest extends BaseSeleniumTest {
@Test
public void testAbc() {
WebDriver driver = getDriver();
// test stuff
}
}
答案 1 :(得分:1)
我对JUnit不太满意......看起来你正在尝试这里建议的解决方案: Before and After Suite execution hook in jUnit 4.x
建议您将@BeforeClass移到SeleniumTestSuite类中。