在Swing中,我有一个JButton,它在点击时执行以下测试脚本:
@RunWith(Parameterized.class)
public class ExampleTest{
private static csvTools csvTools = new csvTools();
private static WebDriver driver = CreateWebDriver.getDriver("chrome");
public static URLS urls;
// ..
// Data variables
private String fullName;
// ..
public ExampleTest(Map<String, String> testDataRow) {
this.fullName = testDataRow.get("Full name");
//..
}
@Rule
public TestName currentlyRunningTest = new TestName();
@Rule
public final ErrorCollector collector = new ErrorCollector();
@Parameterized.Parameters
public static Collection<Map<String, String>> testData() throws Exception {
return csvTools.getTests("ExampleTestData.csv");
}
@BeforeClass
public static void openBrowser() throws Exception {
page = new BasePage(driver);
userInformation = new UserInformation();
loginPage = new LoginPage(driver);
registrationPage = new RegistrationPage(driver);
evidenceCollector = new resources.EvidenceCollector(ExampleTest.class.getName());
}
@Before
public void setUp() throws Exception {
csvTools.saveData(currentlyRunningTest, "Fail/Pass", "");
evidenceCollector.newTestCase();
}
@Test
public void ExampleTest() throws Exception {
try {
driver.get(URLS.LOGINPAGEURL);
driver.findElement(By.id("d"));
//..
driver.get("examplesite");
page.screenShot(driver);
}
@After
public void tearDown() throws Exception {
evidenceCollector.moveScreenshotsAndTestData("ExampleTestData.csv");
}
@AfterClass
public static void closeBrowser() {
driver.quit();
}
}
我遇到的问题是,它不会让我在同一个Swing GUI实例中执行相同的脚本两次以上。我已经确定这一行是罪魁祸首:
private static WebDriver driver = CreateWebDriver.getDriver("chrome");
我通过评论线和移动来确定这一点:
WebDriver driver = CreateWebDriver.getDriver("chrome");
进入测试本身,然后可以在同一个Swing GUI实例中多次执行测试。但是,由于chrome驱动程序不再存储为全局变量,因此我无法在测试之外访问它,例如@BeforeClass和@After
这是GUI代码:
JButton exTest1 = new JButton("Run ExampleTest");
exTest1.setLocation(290, 70);
exTest1.setSize(120, 30);
buttonPanel.add(exTest1);
exTest1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (exTest1.isEnabled()) {
executor.execute(new Runnable() { // This is how we run stuff in background. You can use lambdas instead of Runnables.
public void run() {
JUnitCore junit = new JUnitCore();
final Result result = junit.run(ExampleTest.class);
SwingUtilities.invokeLater(new Runnable() { // Now we go back to the GUI thread
public void run() {
errorMessageDisplay(result);
}
});
}
});
}
}});
答案 0 :(得分:2)
如何在@BeforeClass
中初始化驱动程序?
@RunWith(Parameterized.class)
public class ExampleTest{
private static csvTools csvTools = new csvTools();
private static WebDriver driver;
// ...
@BeforeClass
public static void openBrowser() throws Exception {
driver = CreateWebDriver.getDriver("chrome");
page = new BasePage(driver);
// ...
}
// ...
}