Skip to content
Advertisement

How to put a method i @beforeTest

A selenium/java rookie here. 🙂 Trying to understand everything about Test annotations and how to use an method (is it called that) in all classes.

I have this class below where i have a metod called in each @Test, but i would like to put as mush as possible in @BforeTest, or do it in another smart way.

Does you have any smart i de how to do this in a smart way? Thanks in advance! Kind regards, Fred

AS IS:

public class xlsxreadtest {

        @BeforeTest
        public void setup() throws Exception {
        }

        @Test (priority = 1)
        public void Read_xslx1() throws IOException {

        FileInputStream fis = new FileInputStream("./files/Test.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0); 
        String text1 = sheet.getRow(2).getCell(1).toString();
        System.out.println(text1);
    }

        @Test (priority = 2)
        public void Read_xslx2() throws IOException {

        FileInputStream fis = new FileInputStream("./files/Test.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0); 
        String text2 = sheet.getRow(3).getCell(1).toString();
        System.out.println(text2);
    }

    @AfterTest
    public void afterTest() {
        System.out.println("AfterTest : Driver close");
        driver.close();
    }
}

Would like something like this, where i only have to call the Excel-method, or any other, only once.

public class xlsxreadtest {

        @BeforeTest
        public void setup() throws Exception {
            FileInputStream fis = new FileInputStream("./files/Test.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet sheet = workbook.getSheetAt(0);
        }

        @Test (priority = 1)
        public void Read_xslx1() throws IOException {
            
        String text1 = sheet.getRow(2).getCell(1).toString();
        System.out.println(text1);

    }

        @Test (priority = 2)
        public void Read_xslx2() throws IOException {
            
        String text2 = sheet.getRow(3).getCell(1).toString();
        System.out.println(text2);
    }

    @AfterTest
    public void afterTest() {
        System.out.println("AfterTest : Driver close");
        driver.close();
    }
}

Advertisement

Answer

You can make your local variable sheet an instance variable:

public class xlsxreadtest {
        XSSFSheet sheet;

        @BeforeTest
        public void setup() throws Exception {
            FileInputStream fis = new FileInputStream("./files/Test.xlsx");
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            sheet = workbook.getSheetAt(0);
        }

After which you can reference it from your test methods:

@Test (priority = 1)
public void Read_xslx1() throws IOException {
    String text1 = sheet.getRow(2).getCell(1).toString();
    System.out.println(text1);
}

@Test (priority = 2)
public void Read_xslx2() throws IOException {
    String text2 = sheet.getRow(3).getCell(1).toString();
    System.out.println(text2);
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement