Selenium WebDriver – Blackbox Automated Testing using Arquillian Framework

I have been using Arquillian Graphene framework for more than a year for automated functional testing using Selenium WebDriver. I absolutely LOVE this framework. It is so easy to use and really helpful to keep your tests neat and clean by maintaining the test config in a separate file, by injecting the resources at run time when it is required etc.

 

What is Arquillian?

Arquillian is an integration/functional testing framework for testing JVM based application. It nicely integrates with other testing frameworks like JUnit/TestNG and helps to manage life cycle of the test and provides many other useful extensions. For functional browser automation, Arquillian provides Drone and Graphene extensions – both are built on top of Selenium WebDriver.

Arquillian is simply an extension to JUnit/TestNG frameworks. So If you already use JUnit and TestNG, you can just inlcude Arquillian w/o affecting your tests. Drone is for WebDriver managementGraphene is a wrapper around WebDriver with very nice utilities. So when you use all these, you do not miss any existing features of Junit/testNG/Selenium/WebDriver. You can add the jars and continue not to use any features of Arquillian Graphene if you do not like. Ie, your old code will still work fine. It does not force you to do anything! But please give sometime and You will love it!! [ It took some time for me 🙂 ]

Initially it was a bit hard for me to understand how it works as it was very confusing and I failed few times in finding all the maven dependencies to make it work. I am going to make it easy for you by showing you a very simple ‘Google Search’ project using Arquillian [the client mode].

 

Test run Modes:

Arquillian can run the tests in below modes.

  • Container
    • runs tests with container, manages life cycle of container including deployment.
    • Useful for developer’s integration tetsing
  • Stand-alone / Client
    • runs tests without container integration, only lifecycle of extensions is managed
    • allows to write tests independently of Arquillian containers and deployment management
    • For automated blackbox functional testing. [This is the mode we would be using for our tests]

Drone:

Drone is an Arquillian extension to manage the life cycle of the browser in the Arquillian tests. It helps to keep all the browser related test configuration in a separate file called ‘arquillian.xml’ outside of the java code. Drone simply takes care of WebDriver instance creation and configuration and then it delegates this session to Graphene.  Check here for more info.

Graphene:

Graphene is designed as set of extensions for Selenium WebDriver that focuses on rapid development and usability in Java environment. It has below nice features.

  • Page Objects and Page Fragments – Grahpene lets you write tests in a consistent level of abstraction using Page Objects and Page Fragments.
    • Better, robust and readable tests.
  • Guards the requests to the server which was raised by the interaction of the user with the browser.
  • Improved waiting API.
  • JQuery selectors as a location strategy.
  • AngularJS support
  • Browser/Driver injection to the test on the fly.
  • Can take screenshots while testing (see screenshooter extension), and together with other useful info generate neat reports (see Arquillian Recorder extension).

Lets create a simple project for WebDriver automation using Arquillian Grpahene.

Maven Dependency:

Include below mvn dependencies to bring the power of Aruqillian graphene into your existing Java-Webdriver automation framework. [I used TestNG extension for Arquillian framework. You can also use the JUnit extension]

Creating a simple Arquillian Project: 

  • Create a maven project in your favorite editor.
  • Include above maven dependencies in the pom file.
  • Add ‘arquillian.xml’ file to keep all your test configuration as shown here.

arq-01

  • We will use Firefox browser in our webdriver tests.
  • Creating a simple Google page object as shown here.

@Location("https://www.google.com")
public class Google {

    @Drone
    private WebDriver driver;

    @FindBy(name = "q")
    private WebElement searchBox;

    @FindBy(name = "btnG")
    private WebElement searchbutton;

    @FindByJQuery(".rc")
    private List <WebElement> results;

    public void searchFor(String searchQuery) {

        //Search
        searchBox.sendKeys(searchQuery);

        //Graphene gurads the request and waits for the response
        Graphene.guardAjax(this.searchbutton).click();

        //Print the result count
        System.out.println("Result count:" + results.size());

    }
}

  • Create a simple TestNG test to test Google search functionality.

@RunAsClient
public class Test1 extends Arquillian{

	  @Test(dataProvider = Arquillian.ARQUILLIAN_DATA_PROVIDER)
	  public void googleSearchTest(@InitialPage Google GooglePage) {
		 GooglePage.searchFor("Arquillian Graphene"); 
  }
}


  • If we run this test now, We can see TestNG doing the following.
    • automatically launches the firefox browser
    • access the Google site
    • searches for ‘Arquillian Graphene’
    • prints the search result count

Arquillian Graphene Features:

    • Driver Injection: If you notice the above Google page object / TestNG test class, We have not created any instance of the WebDriver.
      • This is because, Arquillian maintains all the test configuration in a simple file called ‘arquillian.xml’ where we mention we will invoke Firefox browser.
      • Now simply change the browser property to phantomjs to run this test on PhantomJS without touching Java code.
      • Check here for more information.
    • Request Guards:
      • Graphene provides a nice set of APIs to handle the HTTP/AJAX requests browser makes to the server.
      • We do not need to use any implicit/explicit WebDriver wait statements. Graphene takes care of those things for us.
      • Check here for more information.
    • JQuery Selectors:
      • Graphene also provides a JQuery location strategy to find the elements on browser.
      • My example above was very simple. To understand this better, include below code in the google page object.
      • Without JQuery selectors, If we need only the visible links, then we need to get all links and iterate all links objects and find only visible links.
      • With JQuery selectors, Grphene makes it very easy for us to find only visible elements. It improves the overall performance of your tests drastically.
 
   @FindByJQuery("a") 
   private List<WebElement> allLinks; 
   
   @FindByJQuery("a:visible")
    private List<WebElement> allVisibleLinks; 
  • Page Injection:
    • Like WebDriver/Browser injection, Graphene also injects the page objects on the fly.
    • You do not need to create an instance of the Page classes yourself in the code. Graphene takes care of those things for us.
    • Consider below example to see how Graphene makes the test readable and elegant.
    • Graphene ensures that Page objects are injected only when it is invoked in your tests. So All the elements of the page class will be available for you when you need it.
      • Grphene injects an instance of Order page into OrderPage variable only when this statement gets executed. OrderPage.enterPaymentInformation() in the below code.
      • There is no ‘new’ keyword anywhere in the script which makes the script looks neat 🙂

@RunAsClient
public class NewTest extends Arquillian {
	
	@Page
	Login LoginPage;
	
	@Page
	Search SearchPage;
	
	@Page
	Order OrderPage;
	
	@Test
	public void f() {  
		LoginPage.LoginAsValidUser();
		SearchPage.searchForProduct();
		OrderPage.enterPaymentInformation();
		OrderPage.confirmOrder();  
	}
}

  • Page Fragments:
    • Like Page Objects, Graphene lets you encapsulate certain elements & its behavior / decouple a HTML structure from the Page Object.
  • Javascript Interface:
    • Graphene lets you inject a custom Javascript code on the DOM and directly access the JavaScript object like a Java object in the code.
  • AngularJS application automation:
    • Protractor framework is not the only solution for AngularJS applications. Arquillian has a nice extension for finding angular elements.
    • Check here for more information.
  • SauceLabs / BrowserStack support:
    • Arquillian has an extension for BrowserStack. That is, the above script we have created can be simply run on any mobile device / platform / browser just by updating the arquillian.xml by including the browserstack account details.
    • I was just blown away when I was able to run all my tests w/o any issues in BrowserStack w/o touching the code.
    • Check here for more information.
  • Custom extension:
    • You can also implement your custom extension and include it in the framework.

Summary:

We saw the basic features of Arquillian Graphene framework and how it makes our test more readable with its browser and page objects injection on the fly and handling the HTTP/AJAX requests etc.

To know more check more posts under ‘WebDriver‘ category of this site.

Ex: How can we create a page object which requires less maintenance?

Check here for the answer : http://www.testautomationguru.com/arquillian-graphene-page-fragments/

 

Happy testing 🙂

Share This:

Categories: Arquillian, Articles, Best Practices, Framework, Selenium

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *