Configure Arquillian With Liferay DXP/7

blog-banner

Testing is necessary to create bug free and stable product. Nowadays, We have many testing tools available in the market such as Selenium, JUnit, Apache JMeter, Mockito and Arquillian.

In this blog, I will explain how to configure Arquillian test tool with Liferay DXP. Arquillian has many features such as.

  • Managing the lifecycle of the container (start/stop)
  • Write a test case to assert the behavior of a CDI bean
  • Execute the Arquillian test case in multiple compatible containers
  • Work outside of a real environment.

Prerequisite  : Basic knowledge of Liferay DXP/7

To configure arquillian with Liferay DXP/7 follow below steps:

Step 1 : JMX Configuration In Liferay DXP/7

  • Install Apache Aries JMX
    • Run  startTestableTomcat  Gradle task in Liferay workspace to install the Apache Aries JMX modules automatically.

Note  : If you are using Liferay CE 7.0 GA4 or earlier version of Liferay DXP Then you need to install following modules manually.

  • "org.apache.aries.jmx:org.apache.aries.jmx:1.1.5"

  • "org.apache.aries:org.apache.aries.util:1.1.3"

  1. For Ubuntu:

    • Set a property in bin/setenv.sh

    		JMX_OPTS="-Dcom.sun.management.jmxremote
    		-Dcom.sun.management.jmxremote.authenticate=false
    		-Dcom.sun.management.jmxremote.port=8099
    		-Dcom.sun.management.jmxremote.ssl=false"
    		
    		CATALINA_OPTS="${CATALINA_OPTS} ${JMX_OPTS}"
    
    
  2. For Windows:
    • Set a property in bin/setenv.bat

    		set "JMX_OPTS=-Dcom.sun.management.jmxremote
    		-Dcom.sun.management.jmxremote.authenticate=false
    		-Dcom.sun.management.jmxremote.port=8099
    		-Dcom.sun.management.jmxremote.ssl=false"
    		
    		set "CATALINA_OPTS=%CATALINA_OPTS% %JMX_OPTS%"
    
    	

Step 2: Add following Property in portal-ext.properties

browser.launcher.url=            #To launching a browser on its own

setup.wizard.enabled=false  #To bypasses launching the Setup Wizard

Step 3: Add browser driver for functional testing

We need to install “ chromedriver ” to configure chrome browser for function testing.

  1. For Ubuntu:

    • To install chromedriver to ubuntu run following command

      sudo apt-get install chromium-chromedriver

  2. For Windows:

Step 4 : Add dependency and deployment task in build.gradle file.

  1. We need to add following dependency to configure Arquillian.

    testIntegrationCompile group: "com.liferay.arquillian", name: "com.liferay.arquillian.arquillian-container-liferay", version: "1.0.6"
    
    testIntegrationCompile group: "junit", name: "junit", version: "4.12"
    
    testIntegrationCompile group: "log4j", name: "log4j", version: "1.2.17"
    
    testIntegrationCompile group: "org.slf4j", name: "slf4j-log4j12", version: "1.7.5"
    
    testIntegrationCompile group: "org.jboss.arquillian.graphene", name: "graphene-webdriver", version: "2.1.0.Final"
    
    testIntegrationCompile group: "org.jboss.arquillian.junit", name: "arquillian-junit-container", version: "1.1.11.Final"
    
    
  2. For assembly .jar archive.

    		jar {
    			if (project.hasProperty('dir')) {
    				destinationDir = file(dir);
    			}
    		}
    
    	
  3. We need to add liferay’s home path in file.

    		liferay {
    			liferayHome='/home/st43/Projects/ST/blog/liferay-ce-portal-7.0-ga4'
    		 }
    
    		

Step 5: Add Arquillian.xml file

  1. For Ubuntu :

    <?xml version="1.0" encoding="UTF-8"?>
    		<arquillian xmlns="http://jboss.org/schema/arquillian"
    				xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    				xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    		
    			<extension qualifier="webdriver">
    				<property name="browser">chrome</property>
    			</extension>
    		
    			<extension qualifier="graphene">
    				<property name="url">http://localhost:8080</property>
    			</extension>
    		
    			<engine>
    				<property name="deploymentExportPath">build/deployments</property>
    			</engine>
    		</arquillian>
    		
    
    	
  2. For Windows:

    		<?xml version="1.0" encoding="UTF-8"?>
    		<arquillian xmlns="http://jboss.org/schema/arquillian"
    				xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    				xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
    				
    				<extension qualifier="webdriver">
    					<property name="browser">chrome</property>
    					<property name="chrome.binary">C:\Program Files (x86)\Google\Chrome\Application\chrome.exe</property>
    					<property name="chromeDriverBinary">E:\project\chromedriver.exe</property>
    				</extension>
    				
    				<extension qualifier="graphene">
    					<property name="url">http://localhost:8080</property>
    				</extension>
    				
    				<engine>
    					<property name="deploymentExportPath">build/deployments</property>
    				</engine>
    		</arquillian>
    		
    
    	

Step 6: Write Test Cases

  1. For Integral test cases

    Here, We have provided simple test case for multiplication

    		@RunWith(Arquillian.class)
    		public class ArquillianIntegrationTest {
    		
    			@Deployment
    			public static JavaArchive create() throws Exception {
    				final File tempDir = Files.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();
    		
    				final File jarFile = new File(tempDir.getAbsolutePath() +"/com.st.arquillian-1.0.0.jar");
    		
    				return ShrinkWrap.createFromZipFile(JavaArchive.class, jarFile);
    			}
    		
    			@Test
    			public void testAdd() throws IOException, PortalException {
    				final long result = _sampleService.mul(5, 3);
    				Assert.assertEquals(15, result);
    			}
    			
    			@Inject
    			private ArquillianExampleService _sampleService;
    		}
    
    	
  2. For Functional test cases

    Here, We have provided functional test cases for multiplication functionality and elements

    		@RunAsClient
    		@RunWith(Arquillian.class)
    		public class ArquillianFunctionalTest {
    		
    			@Deployment
    			public static JavaArchive create() throws Exception {
    				final File tempDir = Files.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();
    		
    				final File jarFile = new File(tempDir.getAbsolutePath() +"/com.st.arquillian-1.0.0.jar");
    		
    				return ShrinkWrap.createFromZipFile(JavaArchive.class, jarFile);
    			}
    		
    			@Test
    			public void testAdd()throws InterruptedException, IOException, PortalException {
    		
    				_browser.get(_portlerURL.toExternalForm());
    		
    				_firstParameter.clear();
    				_firstParameter.sendKeys("6");
    		
    				_secondParameter.clear();
    				_secondParameter.sendKeys("4");
    		
    				_mul.click();
    		
    				Thread.sleep(5000);
    				Assert.assertEquals("24", _result.getText());
    			}
    		
    			@Test
    			public void testInstallPortlet() throws IOException, PortalException {
    				_browser.get(_portlerURL.toExternalForm());
    		
    				final String bodyText = _browser.getPageSource();
    		
    				Assert.assertTrue("The portlet is not well deployed",bodyText.contains("Sample Portlet is working!"));
    			}
    		
    			@FindBy(css = "button[type=submit]")
    			private WebElement _mul;
    		
    			@Drone
    			private WebDriver _browser;
    		
    			@FindBy(css = "input[id$='firstParameter']")
    			private WebElement _firstParameter;
    		
    			@PortalURL("arquillian_example_portlet")
    			private URL _portlerURL;
    		
    			@FindBy(css = "span[class='result']")
    			private WebElement _result;
    		
    			@FindBy(css = "input[id$='secondParameter']")
    			private WebElement _secondParameter;
    		
    		}
    
    	

Here we have used arquillian annotation such as

  • @Deployment : Methods should be considered as deployment producers.
  • @RunAsClient : To run the test method as a client by arquillian.
  • @FindBy : To Find web page elements, We can find by css, id, class and xPath.
  • @Drone : Used to inject Selenium webdriver
  • @PortalURL : To inject portlet URL.

Step 7: Run following commands to test application

  1. For Ubuntu : “./gradlew testIntegration”
  2. For Windows : ”gradlew testIntegration”

That’s it.

Contact us

For Your Business Requirements

Text to Identify Refresh CAPTCHA
Background Image Close Button

2 - 4 October 2024

Hall: 10, Booth: #B8 Brussels, Belgium