lxuboiro
lxuboiro

Reputation: 55

Cannot store WebDriver as global variable and execute a test more than once in the same Swing instance

In Swing, I have a JButton which executes the following test script when clicked on:

@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();
}

}

The issue I have is, it will not let me execute the same script more than twice in the same Swing GUI instance. I have identified that this line, is the culprit:

private static WebDriver driver = CreateWebDriver.getDriver("chrome");

I determied this by commenting the line out and moving:

WebDriver driver = CreateWebDriver.getDriver("chrome");

into the test itself, where it is then possible to execute the test more than once in the same Swing GUI instance. However, as the the chrome driver is no longer stored as a global variable I cannot access it outside of the test, such as as @BeforeClass and @After

Here is the GUI Code:

    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);
                            }
                        });
                    }
                });
        }
        }});

Upvotes: 1

Views: 50

Answers (1)

Tamas Rev
Tamas Rev

Reputation: 7166

What about initializing the driver in @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);
        // ...
    }
    // ...
}

Upvotes: 2

Related Questions