How to test file upload screen in Spring + Selenium

When I write a test code with selenium for the upload screen created with Springboot, The file selection dialog could not be operated and the test could not be performed, so the method to solve it is described below.

Realization method

Since it is difficult to operate <input type ='file'> with selenium, The file is automatically set for the file attribute on the server side (Java side).

Implementation point

  1. Add extension points for parameters received from the screen in ControllerAdviceCustom class
  2. Add a process to create a MockFileEditor class to generate and set a MockMultipartFile
  3. Set to pass the MockFileEditor class if the type is MultipartFile for the extension point of ControllerAdviceCustom class at the time of test execution.

Now you don't have to worry about the operation of <input type ='file'> on selenium side!

ControllerAdviceCustom.java


@ControllerAdvice
public class ControllerAdviceCustom {
    @InitBinder
    public void initBinderMock(WebDataBinder dataBinder) {
        //Mock extension point (implementation uses MockUp on the test side)
    }

    //Omitted below
}

MockFileEditor.java


public class MockFileEditor extends PropertyEditorSupport {
    @Override
    public void setValue(Object obj) {
        //Generate and reflect MockMultipartFile for parameter (Multipart)
        String textFile = "dummy text file";
        MultipartFile mockFile = new MockMultipartFile("file", textFile.getBytes());
        super.setValue(mockFile);
    }
}

SeleniumTest.java


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SeleniumTest {

    @Before
    public void setup() {
        //Implemented mock extension points for ControllerAdviceCustom class
        new MockUp<ControllerAdviceCustom>() {
            @Mock
            @InitBinder
            public void initBinderMock(WebDataBinder dataBinder) {
                //Set to pass the MockFileEditor class if the type is MultipartFile
                dataBinder.registerCustomEditor(MultipartFile.class, new MockFileEditor());
            }
        };
    }

    @Test
public void Testing screens including file uploads() {
        //Omitted below
    }
}

Supplement

It seems that it is possible to operate <input type ='file'> even with selenium. However, it is doubtful whether that method is possible for all browsers, and there is a possibility that it will not be usable due to browser version upgrade (security measures), so I chose this implementation method.

Recommended Posts

How to test file upload screen in Spring + Selenium
How to bind to property file in Spring Boot
How to use Lombok in Spring
How to unit test Spring AOP
How to include Spring Tool in Eclipse 4.6.3?
How to add jar file in ScalaIDE
How to load a Spring upload file and view its contents
How to implement UI automated test using image comparison in Selenium
How to split Spring Boot message file
How to achieve file upload with Feign
How to realize huge file upload with Rest Template of Spring
How to deal with the event that Committee :: InvalidRequest occurs in committee during Rspec file upload test
Summary of how to select elements in Selenium
JUnit 5: How to write test cases in enum
How to add a classpath in Spring Boot
Add screen recording capabilities to Selenium test cases
How to define multiple orm.xml in Spring4, JPA2.1
[Spring Boot] How to refer to the property file
How to set environment variables in the properties file of Spring boot application
I tried to implement file upload with Spring MVC
How to send value in HTML without screen transition
How to write a unit test for Spring Boot 2
How to create a Spring Boot project in IntelliJ
How to use CommandLineRunner in Spring Batch of Spring Boot
How to convert a file to a byte array in Java
How to debug the generated jar file in Eclipse
How to use In-Memory Job repository in Spring Batch
File upload with Spring Boot
How to correctly check the local HTML file in the browser
How to read your own YAML file (*****. Yml) in Java
Change driver to Rack :: Test if driver wants to POST in Selenium test
How to change application.properties settings at boot time in Spring boot
How to call and use API in Java (Spring Boot)
[Rails5] japanMap link How to write parameters in js.erb file
[Xcode] How to arrange Xcode and Simulator screens in full screen
Set up Multipart Resolver to allow file uploads in Spring
How to control transactions in Spring Boot without using @Transactional
How to find May'n in XPath
How to hide scrollbars in WebView
How to run JUnit in Eclipse
How to iterate infinitely in Ruby
How to use Spring Data JDBC
[Rails] How to write in Japanese
How to run Ant in Gradle
Write test code in Spring Boot
[Rails] How to prevent screen transition
How to make a jar file with no dependencies in Maven
How to master programming in 3 months
[How to install Spring Data Jpa]
How to set Spring Boot + PostgreSQL
How to learn JAVA in 7 days
How to convert erb file to haml
How to get parameters in Spark
How to automatically operate a screen created in Java on Windows
How to install Bootstrap in Ruby
How to set and use profile in annotation-based Configuration in Spring framework
Log output to file in Java
How to use InjectorHolder in OpenAM
How to make a splash screen
How to use ModelMapper (Spring boot)
How to introduce jQuery in Rails 6