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 management. Graphene 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.
- 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 🙂
very cool!
may i request, do you have any plan you could probably write tutorial for serenity-bdd?
many thanks
This is new for me, Superb,
Hi vlns,
Thanks for that great article ! I have one question : How could we implement Arquillian with Selenium 3 ?
I search for keyword “Selenium Arquillian” but got least resources. In Arquillian homepage, I can not find article related to this topic. So when I run your code, Eclipse always show searchFor(null) and I doubt it can not instantiate the driver.
Many thanks for your help.
BR.
Selenium 3 is basically a jar file. Arquillian has been working fine with selenium 3 version for long time.
Dear vlns,
After trying and failing many times :
– I can make it run in Chrome. However, Eclipse returns: org.jboss.arquillian.graphene.request.RequestGuardException: Request type ‘XHR’ was expected, but type ‘HTTP’ was done instead.
I think it is due to my Selenium, 3.8.0, which is not supported yet by Arquillian.
For Firefox, I can make it opens the browser, but I got this issue:
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
My geckodriver is found inside project, in drivers folder, and in arquillian.xml, I already mentioned in arquillian.xml file:
firefox
./drivers/geckodriver
So I had no clue about it.
=> Seems like Arquillian does not support completely Selenium 3.
But anyway, thanks for great article.
Please understand that it is nothing to do with selenium. You are using wrong request guards. You should use guardHTTP instead of guardAjax in your case. Basically the error says, it was expecting an ajax request. but page submission was done instead.
Can you share pom file? Please do not paste it here. Arquillian directly pulls selenium jars, all the drivers etc. You do not have to mention anything explicitly. I guess, it is something to do with your pom file.
Dear vlns,
Instead of using Graphene.guardAjax(this.searchbutton).click() , if I use Graphene.guardHttp(this.searchbutton).click(); then it passes. So I guess Google changes something.
Chrome in my lap is Chrome 65 and Firefox 53.
I am still investigating about Firefox and I will update the result to you. FYi, you could check my POM which is uploaded in here : https://github.com/hienhoangminh/arquillian-demo
BR.
Hi.
I can’t seem to execute tests even though I’ve copied all of your provided sample. This is my error:
https://imgur.com/a/xvFo6Lg . Can you help me?
Regards
I am not sure of your pom file. please check this one here – https://github.com/vinsguru/google-voice-search/blob/master/pom.xml
Hi Vins,
Even cloning that repository, I can’t properly run the tests. The error is shown here:
https://github.com/vinsguru/google-voice-search
Hi Vins,
Even cloning that repository, I can’t properly run the tests. The error is shown here:
https://imgur.com/a/SjVXHue
Hi Vins
Thanks for the Wonderful article.Your blog contains many useful information needed for the proper design of test automation framework.Keep up the fantastic service you provide for the automation community.
I have couple of questions.
1) Can Arquillian be successfully integrated with BDD Cucumber framework coupled with Test NG.Are there any additional libraries to be built to make it
for Cucumber ?
2) Does it support parallel testing by default because Graphene stores WebDriver instance in the thread-local context or any modifications required in
the configuration ?
Thanks
Yes, Arquillian does support BDD & parallel testing via Junit. We need to add few more dependencies.
Ok Thanks.So,the support for BDD with TestNG exists as well ?
Yes. I think testNG does not support parallel testing.