2

I have 4 test scripts for my application. In every scripts, I am using data driven framework to test with multiple values. So each time in each script, I initialize the necessary variables which are almost common in all scripts. Is there any possible to way reuse these variables or constants in java or selenium??

below is my code

static String excel,sheet;

    @BeforeClass
        public void start(){
            driver.findElement(By.id(""));
        }

        @DataProvider
        public static Object[][] chartDetails() throws IOException{
            excel = Init.readConfig("xls");
            sheet = Init.readConfig("sheetName");
            data = ReadData.getData(excel, sheet);
            return data;    
        }

    @Test(dataProvider="chartDetails")
    public static void generateCharts(String standName,String termName) {
        driver.findElement(By.id("script goes here"));
       Assert.assertEquals("actual","expected");
    }

In above code, the declaration part will be common in all modules. so i want to reuse the same variables.

1
  • @SajanChandran added Commented Oct 13, 2015 at 9:48

1 Answer 1

2

Are these test scripts implemented as Java classes? If so, there are two common ways to do this.

Create a superclass that each of the 4 tests scripts inherit from which defines the shared variables and shared logic to populate them.

public BaseTest {
    protected sharedVariable1;
    protected sharedVariable2;
    protected sharedVariable3;

    @Before
    public void sharedInit() {
        sharedVariable1 = //..
        sharedVariable2 = //..
        sharedVariable3 = //..
    }
}

public Test1 extends BaseTest {
    @Before
    public void specificInit() {
    }
}

Or, create a separate class which only holds the shared variables and shared logic and use this class in each of the 4 test scripts.

public Shared {
    private sharedVariable1;
    private sharedVariable2;
    private sharedVariable3;

    public Shared() {
        sharedVariable1 = //..
        sharedVariable2 = //..
        sharedVariable3 = //..
    }
}

public Test1 {
    private Shared shared;

    @Before
    public void init() {
        shared = new Shared();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@sh0ruguru nice answer..I think 2nd choice will be better. I also think the 2nd solution before posting question.
@sh0ruguru wat about the first one?? could n't understand lil bit
JUnit will execute any @Before methods in base classes. Sticking a @Before method in a superclass is a good way to make that method run in all base classes. Also, JUnit guarantees to run @Before methods in base classes first, so the shared initialization will be done before the specific initialization.
@sh0rugur thanks a lot...so creating baseclass and initialising all common variables in that class will be a good solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.