mardi 1 septembre 2015

How can I avoid annotations executing before every test?

Apologies for any formatting issues or anything else against etiquette on this site, this is my first post after lurking for the last couple of months and everything that I am working with is pretty new to me.

I have recently started to write some selenium tests in Java/Cucumber/JUnit and have reached an issue that I can't work my way around. I know what the problem is but can't figure out how to actually change my tests to remedy it. Here is some of the background info:

feature file example:

Feature: Form Submission functionality 

@Run
Scenario: Submitting the demo form with correct details is succesful
Given I am on the demo page
When I submit the demo form with valid information
Then the thank you page is displayed

StepDefs file example (I have four files like this, testing different parts of the site):

package testFiles.stepDefinitions;

import testFiles.testClasses.formSubmissionFunctionalityTest;
import cucumber.api.java.en.*;
import cucumber.api.java.After;
import cucumber.api.java.Before;

public class formSubmissionFunctionalityStepDefs {

    private formSubmissionFunctionalityTest script = new formSubmissionFunctionalityTest();

    @Before
    public void setUpWebDriver() throws Exception {
        script.setUp();
    }

    @Given("^I am on the demo page$")
    public void i_am_on_the_demo_page() throws Throwable {
        script.goToDemoPage();
    }

    @When("^I submit the demo form with valid information$")
    public void i_submit_the_demo_form_with_valid_information() throws Throwable {
        script.fillSubmitDemoForm();
    }

    @Then("^the thank you page is displayed$")
    public void the_thank_you_page_is_displayed() throws Throwable {
        script.checkThankYouPageTitle();
    }

    @After
    public void tidyUp() {
        script.tearDown();
    }
}

I then also have a formSubmissionFunctionalityTest.java file which contains all of the actual code for methods such as fillSubmitDemoFrom. I also have a setupTest.java file with methods such as tearDown and setUp in.

The problem I am having is that every time I execute a test, four browser sessions are opened rather than the desired single browser. I know that this is because the @Before and @After annotations are executed before each test, rather than before the whole suite. I think that the best solution would be to have a new file with the @Before and @After in, but this is the part that I can't seem to figure out. In each file, script is different, which is where I think the problems come from, but I am not entirely sure.

Does anyone know of a way I can restructure my tests so that they all share the same @Before and @After methods, without causing multiple browser sessions to open? Thank you in advance

Aucun commentaire:

Enregistrer un commentaire