Testing Your Alfresco Add-ons Michael Suzuki Software Engineer
Testing Your Alfresco Add-ons Introduction Automation Tools Best Practice Setting Up With Maven Demo Were going to cover testing: The tools that are available The patterns And how to do it.
Michael Suzuki @suzukimichael github.com/michaelsuzukisagi
Testing your add-on’s gives you the confidence that they are behaving correctly. What is this talk about.
Why Testing Makes Sense It builds Confidence that the systems is behaving as it should. Validates no regression bugs have been introduced Helps find problems early Shorter feedback loops when refactoring We don’t wait till the qa phase to find problems.
Test Types Unit Tests Integration Tests Functional Tests Unit tests,tests the unit. Integration test ensures services are talking to each other. Functional that the software meets the requirements.
The Right Balance
Value Of Automation Detecting difference Pins the functionality Avoid wasted effort Less duplication
Testing Your Alfresco Add-ons Automation Tools
WebDriver An open source browser automation API. Supports different OS, languages and browsers.
WebDriver Code { //Start fire fox WebDriver driver = new FireFoxDriver(); //Navigate to share driver.navigate(http://localhost:8080/share); }
Locator Strategies Id Name Tag name Class name CSS selector Xpath Link Text Partial Link Text Talk about locators: By id,css,xpath, tag name.
Locator Strategies In Action { //By Id WebElement username = driver.findElement(By.id(“iusername”)); //By Name WebElement password = driver.findElement(By.name(“password”)); //By Tag Name WebElement btn= driver.findElement(By.tagName(“button”)); //By CSS selector username = driver.findElement(By.cssSelector(“input#iusername”)); //By class name WebElement btn =driver.findElement(By.className(“button”)); //By xpath btn = driver.findElement(By.cssSelector(“//button)); }
Interacting With Elements { //Find Element By Id WebElement element = driver.findElement(By.id(“foo”)); //check if visible element.isDisplayed(); //type in admin element.sendKeys(“admin”); //clear input field element.clear(); //click action element.click(); //get text element.getText(); }
Testing Your Alfresco Add-ons Best Practice
Always Use Page Object Pattern Render Pattern Why cause it makes hard to maintain test code
Page Object “A page object wraps an HTML page, or fragment, with an application-specific API, allowing you to manipulate page elements without digging around in the HTML” Martin Fowler
Login Page Object public class LoginPage { private static By USERNAME_INPUT = By.id(“username"); private static By PASSWORD_INPUT = By.id(“password"); private static By SUBMIT_BUTTON = By.id(“submit"); public LoginPage(WebDriver driver) {} public void login(String username, String password) // Find By Id WebElement username = driver.findElement(USERNAME_INPUT); username.sendKeys(username); WebElement password = driver.findElement(PASSWORD_INPUT); password.sendKeys(password); driver.findElement(SUBMIT_BUTTON).click(); }
Why You Should Use Page Objects Encourages re-use of the code Makes tests more readable Encapsulates mechanical details of the page Easier to maintain Less duplication
Render Pattern The logic which determines if a page has rendered, by checking that all the required web elements of the page, are visible before it can be used.
Render Method In Action public LoginPage render(RenderTime timer) { while (true) timer.start(); try if(driver.find(USERNAME_INPUT).isDisplayed() && driver.find(PASSWORD_INPUT).isDisplayed() && driver.find(SUBMIT_BUTTON).isDisplayed() break; } catch (NoSuchElementException nse){} finally timer.end(); return this; Addresses intermitted test failures and Thread Sleep. Loops to find input fields and submit button, if not found the timer eventually throws a PageRenderTimeException
Share PO Project Aims to mimic user interaction on Alfresco Share. A library that is simple, reliable and reusable. The project started in 2012 and is now part of Alfresco core. Currently used by Functional test (aka QA share) and Benchmark
WebDrone Project A Selenium WebDriver wrapper with added functionality. Provides additional tools: Selenium grid Image recognition Language support Currently used by Functional test (aka QA share) and Benchmark
Things To Avoid WebDriver code in test code Assertions in page objects Thread sleep Why cause it makes hard to maintain test code
Testing Your Alfresco Add-ons Demo
Build Add-ons with Maven Setup Spring Loaded export SPRING_LOADER=/usr/local/lib/spring-loaded/springloaded-1.2.0.RELEASE.jar Setup Maven options with Spring Loaded export MAVEN_OPTS="-XX:MaxPermSize=512m -Xmx1024m -javaagent:$SPRING_LOADER -noverify" Run AMP with Maven Create a directory mvn archetype:generate -DarchetypeCatalog=https://artifacts.alfresco.com/nexus/content/groups/public/archetype-catalog.xml -Dfilter=org.alfresco.maven.archetype: Type 1 to build amp Type option 5 to get latest archtype Type a groupid org.alfresco.summit Type aritfact id, the project name: demo If out of memory type MAVEN_OPTS='-Xms256m -XX:MaxPermSize=1024m -Xmx1024m’ Run alfresco mvn integration-test -Pamp-to-war To run share, cd into directory and type mvn integration-test -Pamp-to-war -Dmaven.tomcat.port=8081 mvn integration-test -Pamp-to-war
Thank you
Additional Info Share Page Object WebDrone Demo Selenium WebDriver https://github.com/Alfresco/community-edition/tree/master/projects/share-po WebDrone https://svn.alfresco.com/repos/alfresco-enterprise/benchmark/webdrone Demo https://github.com/michaelsuzukisagi/summit2014.git Selenium WebDriver http://docs.seleniumhq.org/docs/03_webdriver.jsp https://code.google.com/p/selenium/wiki/PageObjects Alfresco Maven http://ecmarchitect.com/alfresco-developer-series-tutorials/maven-sdk/tutorial/tutorial.html http://www.youtube.com/watch?v=Tu641nRGbtQ Spring Loaded https://github.com/spring-projects/spring-loaded