Sunday, January 17, 2016

Introducing FluentLenium (1)

I had started new initiative recently - Facebook group Technology Books. Feel free to join if you look for reading recommendations or want to share your thoughts. Because of that I had to accept hundreds joining requests from many people. (Un)fortunately repetitive tasks quickly bore me, so I figured out it's the perfect opportunity to not only automate it, but also to feature it on this blog. Here is my journey, which now becomes our journey.

We are going to use Java and FluentLenium framework which nicely extends famous Selenium WebDriver. This project is being actively developed right know and you should really try it. Naming conventions used in FluentLenium are really intuitive, and writing tests becomes really easy and rewarding. Documentation is quite nice too.

At first we need to clearly define what we want to achieve. The scenario looks like that:
As a logged in Technology Books admin I want to accept all existing joining requests.

So let's get started. At first we need to add Maven dependency for our project. I'm going to use TestNG because of it's powerful annotations:

Having Page Object Pattern in mind we need to code the following things:
1. Make FacebookLogOutPage class
2. Create logic for non-version-tracked file (which will contain Facebook login and password)
3. Write authentication method with @BeforeMethod annotation
4. Make FacebookApproveGroupRequestPage class
5. Write accept request methods with @Test annotation

Coding begins here!

1. If you are familiar with Selenium WebDriver you'll be surprised to realise that FleuntLenium allows CSS Selectors only. I prefer them over XPath anyway, because they are faster and more readable. If you like identifying elements via id just add # sign, and via class add . sign. Using developer tools console makes element identification very easy. See example screenshot below.

So FacebookLoggedOutPage.class may look like this:

2. Now we need a way to store our user credentials locally. In order to achieve that we need file. Obviously I didn't share my credentials here

And LoadProperties method which loads them to our code:

3. We are ready for authentication method which will login our admin to Facebook. Note how intuitive the syntax is and how easy the code looks

4. Ok, we are already logged in so we need to go for GroupApprovePage now...

5. ...And Accept all requestors. Here is how it looks like finally:

Please take a closer look how strong FluentLenium is when it comes to dynamic waiting. This is one of the most important element when it comes to test automation projects. There is a lot of bad information about how we should avoid timeout exceptions on public forums and blogs (even on stack). Please get familiar with Chapter 4 of Mastering Selenium WebDriver if have problems with them. Googling may lead you to adopt bad practices. 

Code from steps 1-5 is available on my GitHub.

Bonus content:

a) I'm really happy after being featured as new face of polish testing by Huge motivation boost to keep up blogging work.

b) You may have noticed (1) in title. I'll continue FluentLenium / test automation series in future. I'll discuss why this test isn't exactly perfect. Or maybe do you know already? Please let me know in comment section.

c) Main image credit:

d) I have second blog with book reviews. Enjoy