[JAVA] Perform automatic screen tests with Selenide + XlsMapper

Environment and implementation method

IDE:eclipse(Oxygen.3) Language: Java

Until environment construction Create a new Maven project in eclipse. Enter the following code and execute → Maven Install

<dependencies>
  <!-- Selenide -->
  <dependency>
    <groupId>com.codeborne</groupId>
    <artifactId>selenide</artifactId>
    <version>5.9.0</version>
    <scope>test</scope>
  </dependency>

  <!-- JUnit -->
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
  </dependency>

  <!-- XlsMapper(Excel reading) -->
  <dependency>
    <groupId>com.github.mygreen</groupId>
    <artifactId>xlsmapper</artifactId>
    <version>2.1</version>
  </dependency>

Implementation

I will omit the details because I think it is easier to understand what is explained elsewhere. I think XlsMapper is well understood in the official documentation. It was a method I used to do personally, but it was easy to code how to create a Page class and define the actions that can be performed within the Page. By the way, with this method, I feel that it is difficult to implement when the operation that can be done on one screen changes dynamically.

PageBase.java


public class PageBase {
  protected SelenideElement hoge() {
    return $("input[name='hoge']");
  }

  protected SelenideElement fuga() {
    return $("input[name='fuga']");
  }
}

Page.java


public class Page extends PageBase {
  //Set the value to hoge
  public void setHoge(String hoge) {
    hoge().val(hoge);
  }

  //Click fuga
  public void clickFuga(){
    fuga().pressEnter();
  }
}

However, by preparing the Page class in this way, it is possible to implement it by simply calling them in the Logic part.

Logic.java


public class Logic {
  public void doLogic(String hoge){
    Page pg = new Page();
    //Set the value to hoage and click fuga
    pg.setHoge(hoge);
    pg.clickFuga();
    //Take a screenshot
    Selenide.screenshot("hogefuga");
  }
}

Test.java


public class Test {
  @before
  public void setup(){
    //Specify the type of browser to open (IE)
    Configuration.browser = WebDriverRunner.INTERNET_EXPLORER;
    System.setProperty("webdriver.ie.driver", "C:\\eclipse\IEDriver\IEDriverServer.exe");

    //Specify where to save the screenshot
    Configuration.reportsFolder = "/sample/testFolder/";
    //Open the specified site in the browser
    open("https://qiita.com");
  }

  @Test
  public void doTest(){
    Logic logic = new Logic();
    logic.doLogic("sample");
  }

  @after
  public void tearDown(){
    //Close browser
    Selenide.closeWebDriver();
  }
}

This is the implementation of the screen test using Selenide. In the above case, open the site, enter "sample" in the hoge element, click the fuga element, and close the browser. Now use XlsMapper to set the value of "sample" to the pre-filled Excel value. Prepare a separate class for implementing XlsMapper. The implementation method differs depending on whether there are multiple values or only one value. First, if there is only one value, prepare a Data class.

Data.java


@XlsSheet(name="testSheet")
public class Data{
  @XlsLabelledCell(label="hoge" type=LabelledCellType.Right)
  public String hoge;
}

Test.java


@Test
public void doTest(){
  XlsMapper xlsMapper = new XlsMapper();
  Data data = xlsMapper.load(
                        new FileInputStream("testExcel.xlsx"),
                        Data.class);

  Logic logic = new Logic();
  logic.doLogic(data.hoge);
}

By implementing in this way, the Excel value can be retrieved. If there are multiple values, create a Sheet class and a Data class, and implement the Data reading by loading the Sheet class.

Sheet.java


@XlsSheet(name='testSheet')
public class Sheet {
  @XlsHorizontalRecords(tableLabel="Data list", recordClass=Data.class)
  public List<Data> dataList;
}

Data.java


public class Data {
  @XlsColumn(columnName="hoge")
  @XlsTrim
  public String hoge;
}

Test.java


@Test
public void doTest(){
  XlsMapper xlsMapper = new XlsMapper();
  Sheet sheet = xlsMapper.load(
                        new FileInputStream("testExcel.xlsx"),
                        Sheet.class);

  Logic logic = new Logic();
  for(Data data : sheet.dataList){
    logic.doLogic(data.hoge);
  }
}

In this way, the implementation is to rotate Logic by the number of List in the Sheet class. With this implementation method, the specification is to "repeat the same operation a certain number of times on the same screen".

Summary

It is effective when you want to get evidence by hitting keys on one screen many times, but this implementation method is weak in the case of a test where you hit keys once on multiple screens. It feels like. I coded it for ITa, but it was actually used to check the screen display accompanying the DB update, so I think I implemented it very weakly. I haven't brushed up the code and articles so much, but I can make it into a shape for the time being.

Recommended Posts

Perform automatic screen tests with Selenide + XlsMapper
Write tests with Minitest