Friday, May 15, 2020

Liferay Arquillian Extension : Functional Testing

Hi,

In this post I am going to discuss about how to use arquillian extension to perform functional testing on liferay plugins...

The following sections details the softwares used...

Prerequisites
Name Version Download Page Direct Link
Java 1.8 Java Download Page N/A
Mysql 8.0.20 N/A N/A
Liferay Portal 7.3.0 - CE Lifeay Portal Download Page Click to Download Directly
IntelliJ IDE 2019.3.5 N/A N/A

Before you proceed incorporating the functional testing for your plugins, I would recommend and it is must to go through an other post of mine for the configurations. The link to the post is given below....

Liferay Arquillian Extension : Integration Testing

Updating build.gradle of the plugin
Open up the build.gradle of the plugin and append the following in dependency closure...

testIntegrationCompile group: 'org.jboss.arquillian.graphene', name: 'graphene-webdriver', version: '2.3.2'

once done the complete build.gradle of the plugin should look something like the one given below..


Updating view.jsp...
Open view.jsp,  which you can find under the folder src/main/resources/META_INF/resources and paste the following contents in it...

<%@ include file="/init.jsp" %>

<div id="01">
    <liferay-ui:message key="helloworld.caption"/>
</div>

Creating Functional Test Class...
Create a test class called HelloWorldFunctionalTest.java under src/testIntegration/java folder and update the file with the following content...

import com.helloworld.test.HelloWorldTestHelper;
import com.liferay.arquillian.portal.annotation.PortalURL;
import com.liferay.portal.kernel.exception.PortalException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@RunAsClient
@RunWith(Arquillian.class)
public class HelloWorldFunctionalTest{

    @Deployment
    public static JavaArchive create() throws Exception {
        final File tempDir = HelloWorldTestHelper.createTempDir();

        String gradlew = "../../gradlew";

        String osName = System.getProperty("os.name", "");
        if (osName.toLowerCase().contains("windows")) {
            gradlew = "../../gradlew.bat";
        }

        final ProcessBuilder processBuilder = new ProcessBuilder(
                gradlew, "jar", "-Pdir=" + tempDir.getAbsolutePath());
        final Process process = processBuilder.start();
        process.waitFor();

        Path jarFilePath = Paths.get("build/libs/com.helloworld-1.0.0.jar");
        Files.copy(jarFilePath, new FileOutputStream(
                tempDir.getAbsolutePath() +
                        "/com.helloworld-1.0.0.jar"));

        final File jarFile = new File(
                tempDir.getAbsolutePath() +
                        "/com.helloworld-1.0.0.jar");

        return ShrinkWrap.createFromZipFile(JavaArchive.class, jarFile);
    }

    @Test
    public void testGreetTheWorld() throws IOException, PortalException {
        _browser.get(_portalURL.toExternalForm());
        Assert.assertEquals("Hello from HelloWorld!", _result.getText());
    }

    @FindBy(id = "01")
    private WebElement _result;

    @PortalURL("com_helloworld_HelloWorldPortlet")
    private URL _portalURL;

    @Drone
    private WebDriver _browser;

}

Let us discuss some of the key points from the code given above....
1. _browser: Arquillian annotation @Drone sets this field as a Selenium WebDriver (browser)
2. _portalURL: Liferay Arquillian annotation @PortalURL assigns the portlet’s URL to this field.
the string parameter that the annotation @PortalURL takes is the id of the portlet HelloWorldPortlet
3. _result : JavaScript selectors and Selenium annotation @FindBy to map the field.

With that the configuration and the code part of the functional testing is completed....

Now run the gradle command testIntegration to have the results....

Once the command execution is completed, you would the test results something like the one in the image given below...



This concludes the functional testing of liferay plugins...

Hope you enjoyed it.

Thanks.

No comments:

Post a Comment

Addressing Modes of 8080 / 8085 MicroProcessor

Hi,   In this post I am going to talk about different addressing modes of 8080/8085...without much talking let us get into the topic.   Ther...