How to do test automation for web UI application designed with React and EMF parsley

Display with code 8.jpg

Introduction

Web UI applications refer to applications accessed through a web browser, and they usually have complex user interfaces and interaction logic. To ensure the functionality, performance, and user experience of web UI applications, test automation is an effective way to perform repetitive testing tasks quickly and provide reliable test results without human intervention. This article will introduce how to test automation for Web UI applications designed with React and EMF parsley, as well as examples using HtmlUnitDriver and java code.

text

overview

React is a JavaScript library for building user interfaces. It can create reusable components and achieve efficient rendering through virtual DOM technology. EMF parsley is a framework based on Eclipse Modeling Framework (EMF), which simplifies the development process of Web UI applications based on Model Driven Development (MDD), and provides rich view and editor components. A web UI application designed using React and EMF parsley has the following characteristics:

  • Componentization : Web UI applications are composed of multiple components, each of which has its own state and logic and can be rendered and updated independently.
  • Data-driven : The data of the Web UI application comes from the back-end model, and interacts with the front-end through RESTful API or WebSocket to realize data synchronization and update.
  • Dynamic : Web UI applications can dynamically change the content and style of the interface according to user operations or data changes, providing rich interactive effects.

highlights

Test automation for web UI applications designed with React and EMF parsley has the following advantages:

  • High coverage : Test automation can cover all functional, performance, and user experience aspects of a Web UI application to detect potential defects and errors.
  • High efficiency : Test automation can complete a large number of test tasks in a short period of time, saving manpower and time costs, and improving development efficiency.
  • High reliability : Test automation can avoid human errors and deviations, provide accurate and consistent test results, and improve software quality.

the case

For test automation of web UI applications designed with React and EMF parsley, we need to use suitable tools and frameworks. This article will take HtmlUnitDriver and java as examples to introduce how to implement a simple test automation script. HtmlUnitDriver is a WebDriver implementation based on HtmlUnit, it can simulate a headless browser (no GUI), and execute JavaScript code. Java is a widely used programming language that can be used in conjunction with HtmlUnitDriver to write test cases and assertions. Here is a sample code:

// 导入相关类
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.junit.Assert;
import org.junit.Test;

// 定义一个测试类
public class WebUITest {
    
    

    // 定义一个测试方法
    @Test
    public void testLogin() {
    
    

        // 创建一个HtmlUnitDriver对象
        HtmlUnitDriver driver = new HtmlUnitDriver();

        // 亿牛云代理 爬虫加强版 代理信息 
        final static String proxyUser = "16YUN";
        final static String proxyPass = "16IP";
        final static String proxyHost = "www.16yun.cn";
        final static String proxyPort = "3111";

        // 设置 爬虫代理 代理服务信息
        driver.setProxy(proxyHost, proxyPort);

        // 设置 爬虫代理 代理验证信息
        driver.setCredentials(new UsernamePasswordCredentials(proxyUser, proxyPass));

        // 访问Web UI应用程序的登录页面
        driver.get("http://example.com/login");

        // 查找用户名输入框并输入用户名
        WebElement username = driver.findElement(By.id("username"));
        username.sendKeys("test");

        // 查找密码输入框并输入密码
        WebElement password = driver.findElement(By.id("password"));
        password.sendKeys("123456");

        // 查找登录按钮并点击
        WebElement login = driver.findElement(By.id("login"));
        login.click();

        // 检查是否跳转到主页面
        Assert.assertEquals("http://example.com/main", driver.getCurrentUrl());

        // 关闭浏览器
        driver.quit();
    }
}

Here is an explanation of the code:

  • Import the org.openqa.selenium.htmlunit.HtmlUnitDriver class, which is the main class of HtmlUnitDriver and provides methods for creating and operating headless browsers.
  • Import the org.openqa.selenium.By class, which is a locator class that provides methods for finding web page elements based on different attributes (such as id, name, class, etc.).
  • Import the org.openqa.selenium.WebElement class, which is a web element class that provides methods for operating (such as input, click, etc.) on web elements (such as input boxes, buttons, etc.).
  • Import the org.junit.Assert class, which is an assertion class that provides methods to verify whether the test results meet expectations (such as assertEquals, assertTrue, etc.).
  • Import the org.junit.Test annotation, which is a marker class, used to mark a method as a test method, so that the test runner can recognize and execute the method.
  • Define a test class named WebUITest to store test methods.
  • Use the @Test annotation to mark a test method called testLogin for testing the login functionality of the Web UI application.
  • Create an HtmlUnitDriver object and assign it to the driver variable to simulate a headless browser.
  • Call the setProxy method of the driver object to set the host name and port number of the proxy server, where the proxy information provided by the user is used.
  • Call the setCredentials method of the driver object to set the username and password of the proxy server. Here, the proxy information provided by the user is used.
  • Call the get method of the driver object to access the login page of the Web UI application. Here, it is assumed that the URL of the login page is http://example.com/login.
  • Call the findElement method of the driver object, and pass in By.id("username") as a parameter, find the username input box according to the id attribute, and return a WebElement object, and assign it to the username variable, which is used to store the username input box element .
  • Call the sendKeys method of the username object, and pass in "test" as a parameter, and enter the username into the username input box. Here, the username is assumed to be test.
  • Call the findElement method of the driver object, and pass in By.id("password") as a parameter, find the password input box according to the id attribute, and return a WebElement object, and assign it to the password variable to store the password input box element.
  • Call the sendKeys method of the password object, and pass in "123456" as a parameter, and enter the password into the password input box. Here, it is assumed that the password is 123456.
  • Call the findElement method of the driver object, and pass in By.id("login") as a parameter, find the login button according to the id attribute, and return a WebElement object, and assign it to the login variable to store the login button element.
  • Call the click method of the login object, click the login button, and trigger the login operation.
  • Call the assertEquals method of the Assert class, and pass in "http://example.com/main" and driver.getCurrentUrl() as parameters to verify whether the current URL is equal to the expected URL. Here, it is assumed that the login is successful and the main page is redirected. Its URL is http://example.com/main. If they are equal, the test passes; if not, the test fails and an exception is thrown.
  • Call the quit method of the driver object to close the headless browser and release resources.

This article describes how to automate the test of a Web UI application designed with React and EMF parsley, as well as an example implemented using HtmlUnitDriver and java code. Test automation is an effective method that can improve the functionality, performance and user experience of Web UI applications, save manpower and time costs, and improve software quality. The Web UI application designed with React and EMF parsley has the characteristics of componentization, data-driven and dynamic. It can use tools and frameworks such as HtmlUnitDriver and java for test automation. I hope this article will be helpful to you.

Guess you like

Origin blog.csdn.net/ip16yun/article/details/131982160