Tuesday 21 June 2016

Block Push Notification on Chrome in Selenium Webdriver


Many of you might already know that Chrome browser supports push notification  almost on all platforms from version 42  and many sites including Facebook and Google+ have already started delivering push notification for end users. So when it comes to automating such sites/apps first thing that you notice is browser asking permission to deliver push notification.


Block Push Notification on Chrome in Selenium Webdriver
Facebook asking for permission to show push notification

Problem Statement:-


As you can observe in above image that it would be impossible to perform immediate next action in browser after login as it’s been blacked-out. Chrome driver will wait for sometime (implicit wait) and then try to perform next step in browser, whatever it may be, it will fail and if you missed the step in execution which might cause failure of next step and result would be failed test/test suite.

Also push notification badges are not getting displayed as part of web document, so clicking on those button displayed is not possible through selenium.



Workaround:-

    Simple thing to do, is find body element of page [which exist for all the pages :-) ], and perform double click on it. But as mentioned, it would be just workaround.



Better Approach:-

    What you would have done if you have to do same thing again and again manually in this case? Consider testing ten different app and login scenarios for them. Any sane person would have searched for how to do the same manually.
Manually disable/block Push Notification in Chrome
Manually disable/block Push Notification in Chrome



Whatever you would have done manually, would it be possible to set this up while invoking chromedriver instance? Yes, This is where chromedriver Capabilities and ChromeOptions comes into the picture.
Almost for each user customizable setting there is ChromeOption available. You can check all options here. Now to prevent or block push notifications on Chrome we can use disable-notifications option.

This is all for now.
Cheers!!

Friday 17 June 2016

Selenium as a Windows Service

I have been playing with selenium grid from past few days and it becomes very tedious to me to setup the grid by firing commands in command prompt [I am mostly windows 7 user]. So I thought of running selenium grid as service and for that I have used NSSM - the Non-Sucking Service Manager.

To set up grid as service, obviously you need-
  1. Selenium-server-standalone-2.53.0.jar file (should work with other version but I have used this)

Selenium Grid Hub setup:-

    You need to have following files in folder ready to use before you start with this setup. My folder setup is like-

    To set up hub, we use following command in the command prompt.

java -jar selenium-server-standalone-2.53.0.jar -role hub


Check whether grid hub is up and running as shown in below image-


Now if you accidentally close your command prompt running, your hub will go down which used happen a lot to me as I was not very keen user of command prompt. By setting up this as service, anyone can get rid of orphan looking command prompt. Now close the command prompt and start with actaul setup of service.

Set up same selenium grid hub as service

NSSM



Detail use of every tab in NSSM is mentioned here. Verify that proper entries are made in the registry for same.


You can monitor SeleniumHub service running now in Services.


Here is your selenium grid hub working as service.


On Grid adding chrome-driver node -



java -jar selenium-server-standalone-2.53.0.jar -role node -hub http://192.168.0.174:4444/grid/register/ -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=1 -Dwebdriver.chrome.driver=C:\Grid\chromedriver.exe  

Execute above command in command prompt. You need to use your hub IP instead of 192.168.0.174 which is IP of my hub. 


Observe your selenium grid console now, you can see one chrome node is added over there.


Set up same chrome node as service:-

Close the previously running chrome node in command prompt. I’m passing below command as parameter while creating new service as ‘ChromeNode’ -
-jar selenium-server-standalone-2.53.0.jar -role node -hub http://192.168.0.174:4444/grid/register/ -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=1 -Dwebdriver.chrome.driver=C:\Grid\chromedriver.exe




 You need to use your hub IP instead of 192.168.0.174 which is IP of my hub. 




Verify that proper entries are made in the registry for same.


You can monitor ChromeNode service running now in Services.



Here is your whole selenium grid setup running as service.



This is all for now in selenium testing tools
Cheers!!

Sunday 12 June 2016

MongoDBClient for Java Application

Singleton MongoDBClient for Java Application

Reason to recommend not to close MongoDB driver connection:-

The MongoDB Java driver provides a MongoClient class for the connection of your Java application with the MongoDB database.

           The mongodb java client maintains (example below) a connection pool, which is relatively expensive to set up [even in faster network it takes 100 of milliseconds, apart from that each driver connection would run in separate thread and it will consume memory], so one should reuse the MongoClient instance across the lifetime.
            A Connection Pool is a cache of database connections maintained by the driver so that connections can be reused when new connections to the database are required. To reduce the number of connection pools created by your application, it is recommend to call MongoClient.connect once.

So it becomes obvious to create one instance of the MongoClient and to reuse it in every request of your application and not to close it.

MongoDBServices wrapper class:-

  1. I have used singleton design pattern in java by using enum.
  2. I kept separate properties file which has parameters to connect with MongoDB server.
  3. Declaration and Initialization of MongoClient.
  4. Use method for database operation.

Singleton pattern in Java by using enum


Parameters to connect with MongoDB server

Parameters to connect with MongoDB server
Properties file holding details for connection.

Declaration and Initialization of MongoClient

Declaration and Initialization of MongoClient
Declaration and Initialization of MongoClient

Use method for database operation

Database operation using singleton method
Database operation using singleton method


Resources:-

http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java

http://stackoverflow.com/questions/26914320/closing-mongodb-java-connection

Sunday 5 June 2016

Page Object Model


           Once you start with automation, a word you will constantly get bombarded with is  ‘Framework' which is a very generic term.  Wiki defines a Test  Automation Framework as an integrated system that sets the rules of automation of a specific product. This system integrates the function libraries, test data sources, object details and various reusable modules. The article has also listed various approaches for designing test automation framework, sadly people categorize them as automation frameworks without understanding that it is just approach to design test automation. Approach of automation framework that one should choose/take must be depends upon the need of AUT.
          Similarly, many folks misunderstood or I am not sure why they call Page Object as framework. It is NOTa framework, it is a Design Pattern which has many advantages. Though there are many resources available on internet on how to implement page object model, very few of them mentions below rules.

Basic rules for getting the desired maintainability of your test code :-
1.      Page objects themselves should never make verifications or assertions. This is part of your test and should always be within the test’s code, never in an page object. The page object will contain the representation of the page, and the services the page provides via methods but no code related to what is being tested should be within the page object.
2.      There is one, single, verification which can, and should, be within the page object and that is to verify that the page, and possibly critical elements on the page, were loaded correctly. This verification should be done while instantiating the page object.
3.      A page object does not necessarily need to represent an entire page. The Page Object design pattern could be used to represent components on a page. If a page in the AUT has multiple components, it may improve maintainability if there is a separate page object for each component.
4.      If you have WebDriver APIs in your test methods, You're Doing It Wrong. [This is what point 1 is all about. ] -- Simon Stewart.


Ref:- http://www.seleniumhq.org/docs/06_test_design_considerations.jsp#page-object-design-pattern



Saturday 9 January 2016

Sunday 8 November 2015

Discussions, Meetings, Emails & One-on-one

I have been thinking lately, how much average person in IT industry spent time in meetings /discussions in his/her entire career. Below tweet from friend leads to the inception of this post



I decided to break down the post in few questions which would help me to conclude by end of the  post
  1. Why we need meetings/discussions?
  2. What are we doing wrong about the meetings/discussions?
  3. What are possible alternatives to  meetings/discussions? - Emails & One-on-one
  4. What agile has to say about meetings/ discussions?
  5. How can you make meetings/ discussions fruitful?
Let's discuss this question one by one-


1. Why we need meetings/discussions?
       Below are the reasons I can think of why discussions/meetings are called for, in general:-
  • To bring every team member on the same page about a current status of Project.
  • To discuss and define the roadmap.
  • To have everyone's opinion & discuss possible options on particular problem
  • Discuss loopholes in the current processes.
And this list can go on and on ...but in general, these reasons are enough to have an idea what I'm talking about.


2. What are we doing wrong about the meetings/discussions?
        Do you found yourself in a situation where you wonder 'what you are doing in the meeting?'
        Do you hear voices in your head screaming 'what's wrong with these people, why are we wasting so much time?'

Take a deep breath, you are not alone. Here is the list of things that makes a meeting a wrong meeting.


  • Meeting/Discussion without an agenda - If you don't define the purpose of your meet, it's most certainly be unfruitful.
  • 11th-hour invite for a meeting - People leaving their desks with something in a mind and have no clear idea about the purpose of a meeting, It's going to be a disaster for them.
  • Accepting invitation without thinking - 'Do I really need to attend this meeting?
  • Sending a meeting request to everyone in a team instead of required stakeholders. - If you schedule a meeting for an hour for a team of eight members (1 * 8 =8hrs ), do you think it would be that productive as work of the single person in a whole day?
                        And that person can be anybody - a fresher who recently on boarded your team or principal architect of the project.


3.What are the possible alternatives to meetings/discussions? -     Emails & One-on-one



I was wondering isn't it entirely possible to remove discussions & meeting from software professional's calendar. So before actually writing this post, I started Q & A on WhatsApp with few intelligent minds. And below are few awesome replies. 

- "Email cannot always convey emotions or urgency,  even if a message is conveyed."
- "Language itself is ambiguous, body language & emotions helps while conveying matter."
-  "It is possible to convey priority over an email but its most likely impossible to convey severity."
- "Mail, Skype can help in communication but in person, face to face meetings / discussion helps to create bonding essential for a team."

All above point seems to be valid to me and yes at some point we must need to have meetings/discussions. We should set some rules/protocol/standards when to have a meetings/discussions, email & one-on-one.


4.What agile has to say about meetings/ discussions?

    Manifesto for Agile Software Development states that importance must be given to Individuals and interactions over processes and tools.

source :- LeadingAgile


Though Scrum, which is an agile software development model, have regular meetings like Stand-up meeting, Sprint review meeting & Retrospective meeting, according to manifesto one-on-one & discussion meeting should get preference over email.


5. How can you make meetings/discussions fruitful?
    
I would be doing a separate post on this ...




PS: - These are funny replies that I have received while doing WhatsApp Q&A, Hope you will enjoy it.











Copyrights held by Amol Chavan. Powered by Blogger.