jahiaone 2015 - selenium and digital factory: a not so common implementation of regression tests

21
1 Selenium and Digital Factory: a not so common implementation of regression tests François Pral © 2002 - 2015 Jahia Solutions Group SA

Upload: jahia-solutions-group

Post on 31-Jul-2015

55 views

Category:

Technology


2 download

TRANSCRIPT

1

Selenium and Digital Factory: a not so common implementation of

regression tests

François Pral© 2002 - 2015 Jahia Solutions Group SA

2 © 2002 - 2015 Jahia Solutions Group SA

Summary

What is Selenium? Some history The “Content Objects” approach Module testing with Selenium Questions

3 © 2002 - 2015 Jahia Solutions Group SA

What is selenium?

Software testing tool for web applications Open source Browser based Supports multiple languages (Java, C#,

Groovy…)

http://www.seleniumhq.org/

4 © 2002 - 2015 Jahia Solutions Group SA

Some history

Started developing Selenium tests in 2011 Tests recorded with Selenium IDE Then we created function libraries… … which kept growing and growing

5 © 2002 - 2015 Jahia Solutions Group SA

From procedural programming…

Too many functions… Some for content creation

createUserAccount(...)

Some for content creation and verification createUserAccountAndVerifyCreation(...)

6 © 2002 - 2015 Jahia Solutions Group SA

ExampleString pageTitle = "Test";String richText = "Selenium can be fun";leftPanelFunctions.rightClickOnPage("Home");leftPanelFunctions.createNewPage(pageTitle, "test", "simple");leftPanelFunctions.clickOnPage(pageTitle);richTextFunctions.createRichTextinEmptyArea("maincontent");functions.fillCKEditorForm(richText);functions.saveContent();publication.publishPageAsAdmin(pageTitle);functions.goToLiveMode("demoSite");selenium.click(By.linkText(pageTitle));functions.verifyText(richText);

7 © 2002 - 2015 Jahia Solutions Group SA

to object-oriented programming

Needed to find a new approach to simplify test developments PageObjects “ContentObjects”

8 © 2002 - 2015 Jahia Solutions Group SA

The ContentObjects model

9 © 2002 - 2015 Jahia Solutions Group SA

The idea behind “ContentObjects” Separate the data creation / modification from

the test logic It’s about interacting with Digital Factory Specific verifications are done in the test classes

Avoid Prefer

news.testDeletion(); news.delete();Assert.assertFalse(news.isDisplayed(Mode.EDIT));

10 © 2002 - 2015 Jahia Solutions Group SA

Advantages

The “Content Objects” reuse the functions already developed We do not start from scratch No need to refactor old tests

Simplify the test environment creation Content creation Content manipulation (modification, deletion…)

The tests become more readable

11 © 2002 - 2015 Jahia Solutions Group SA

Example (new implementation)

Site site = new Site("demoSite", "demoSite", "sample-bootstrap-templates");Page page = new Page("Test", "test", "2 columns");BootstrapRichText richText = new BootstrapRichText("Selenium can be fun");

site.getHomePage().createPage(page);page.select();page.addToMainContentArea(richText);page.publish();page.live();Assert.assertTrue(richText.isDisplayed(Mode.LIVE));

12 © 2002 - 2015 Jahia Solutions Group SA

From Digital Factory testing to module testing

Approach adapted to our needs Test Digital Factory features Probably not the best solution for site/module testing

in live mode

PageObjects can be used to test a module Sets the foundations for a potentially growing

selenium project Reusable for your site testing

13 © 2002 - 2015 Jahia Solutions Group SA

Example of module testing

Simple search tests on a site

https://github.com/fpral/SearchSeleniumDemo.git

14 © 2002 - 2015 Jahia Solutions Group SA

Example of module testing

Based on PageObjects:

15 © 2002 - 2015 Jahia Solutions Group SA

Test classpublic class ElasticSearchTests extends TestBase{

private static final String JCR_USER_PASSWORD = "password"; private static final String RICHTEXT = "Selenium demonstration at Jahia One"; @BeforeTest public void openHomePage(){ //update it to go on the home page of the site driver.get("http://localhost:8080"); }

@Test public void permissionTest(){ HomePage home = new HomePage(driver); //Search with jcr1 who has reading permissions on every content home.loginAs("jcr1", JCR_USER_PASSWORD); SearchResultPage searchResultPage = home.search(RICHTEXT); verifyResult(…); searchResultPage.logout(); }}

16 © 2002 - 2015 Jahia Solutions Group SA

Execution results on Bamboo

17 © 2002 - 2015 Jahia Solutions Group SA

Conclusion

18 © 2002 - 2015 Jahia Solutions Group SA

19

From Selenium RC to Webdriver...

Migration from RC to Webdriver in 2013 Switched from JUnit to TestNG

Webdriver supports Selenium RC commands Selenium selenium = new

WebDriverBackedSelenium(driver, baseUrl); http://docs.seleniumhq.org/docs/

appendix_migrating_from_rc_to_webdriver.jsp But not all the time...

20

Migration from RC to Webdriver Extend WebDriverBackedSelenium

Override the basic methods type(), click(), contextMenu(), getAlert(), etc…

@Overridepublic void type(String locator, String text){ if(locator.startsWith("//") ){ type(By.xpath(locator),text); } else if(locator.startsWith("xpath=")){ type(By.xpath(locator.replaceAll("xpath=","")),text); } else if(locator.startsWith("name=")){ type(By.name(locator.replaceAll("name=","")),text); } else if(locator.startsWith("id=")){ type(By.id(locator.replaceAll("id=","")),text); } else if(isElementPresent(By.id(locator))){ type(By.id(locator),text); } else if(isElementPresent(By.name(locator))){ type(By.name(locator),text); } else{ super.type(locator, text); }}

21

Migration from RC to Webdriver Extend WebDriverBackedSelenium

It is our “bot” BotStyleTests design pattern It’s an extension of the “selenium” object from RC Useful in a GWT environment:

public void type(By by, String text){ WebElement elem = driver.findElement(by); try { elem.clear(); elem.sendKeys(text); } catch(org.openqa.selenium.StaleElementReferenceException e){ type(by,text); }}