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 user.properties 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 user.properties 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 testerzy.pl. 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: www.testautomationfx.com

d) I have second blog with book reviews. Enjoy awesome-bookreviews.blogspot.com

4 comments:

  1. So, just wondering - what does one gain from sacrificing the xpath locators (and other syntactic stuff such as By.id that can be done with css)? is it only a manner of readability? I feel as if i'm missing something (also missing - where is the part where I attach the webdriver to this framework?

    ReplyDelete
    Replies
    1. Thanks for your question.

      Actually, inability to locate elements by xpath is no longer the issue for FluentLenium. As I said project is actively developed and support for that has been added in latests releases. I'm not sure why was it omitted at the first place, probably the author used css selectors only (same as me btw.).

      Regarding your second question. This is Selenium extension which overrides default driver. Check code and look for this method (FluentAdapter class):

      /**
      * Override this method to change the driver
      *
      * @return returns WebDriver which is set to FirefoxDriver by default - can be overwritten
      */
      public WebDriver getDefaultDriver() {
      return new FirefoxDriver();
      }

      Delete
  2. Ouh! I didn't know about fluentlenium, I particularly like awaiting for conditions. Looks neat :-)

    BTW, What's your opinion on including assertions in PageObjects?

    ReplyDelete
    Replies
    1. Yes, FluentLenium is awesome :)

      Theoretically assertions should be done in tests, but strict following of 'best practices' may make your job harder, not easier.

      Exceptions are allowed. Simplicity should me our main priority :)

      Delete