Tuesday, June 3, 2014

Page Object Model (POM) with realtime project.

POM- Page Object Model.
This is also a kind of framework.
Framework is a set of guidelines like coding standards , test-data handling , object repository treatment etc.

Note- There is nothing like any specific framework. It is just like we are giving the name to framework on the basis what all things/approaches we have used in framework.
If you are driving some data from excel/database then that framework you can call it as data driven framework.
the below framework is also a data driven framework/ hybrid framework/ pom framework/ method driven framework/ function driven / module driven.
Main advantage of POM-
1- avoid to write the duplicate locators for same WebElement which is the big issue in other frameworks.
2- Maintenance of the test script which becomes very easy.
3- improves readability etc.


Important points about POM which we need to do-

1) In POM, create a class for each and every web page.
2) In those classes declare all the WebElements as private variable.
3) Write the public methods to perform operation on that page.
4) Rest all points mentioned in script as people won't like to read long history so lets start practical things.

Here We are going to test login functionality of linkedin.com

Manual testcases for linkedin login scenario which need to automate

1) Create a java project.
2) Create a folder with name Jars and copy all the jar files(downloaded selenium jar files from here also download apache jars files from here) inside the folder. Then add all the jar files to your project.
3) Add TestNG library.
4) Now create packages and corresponding classes under that.
     a) com.LinkedIn.utilLibrary->  Inside this package create Generic class which will have generic methods means the methods which we can use in other projects as well like read and write data from excel file etc.
     b) com.LinkedIn.objectPage -> Inside this create all the web page classes and BasePage class.
                                                           In BasePage class will declare all the WebElements which are same in all the Web pages like header level WebElements and Footer etc and also declare common methods.
     c) com.LinkedIn.testScripts -> Here write the script of the test cases.
5) Create a excel file (BrowserAndURL.xlsx) which will have input feed data.
6) Now right click on your project and navigate to TestNG and Convert to TestNG.
7) Execute the testng.xml
8) That's all about this POM basics.

Project Structure-


LinkedIN_Project_By_POM
        src
            com.LinkedIn.utilLibrary -
                  GenericUtilLibrary
            com.LinkedIn.objectPage
                  BasePage
                  LoginPage
            com.LinkedIn.testScripts
                  AtLoginValid
                  AtLoginInvalidPassword
                  AtLoginInvalidBlank
                  AtLoginInvalidBlankPassword
                  AtLoginValidBack
                  SuperTestNG

This is how your project will look like after all set up done as above.


Output Console-

Exp msg 'Hmm, that's not the right password. Please try again or request a new one.'  matched with act msg 'Hmm, that's not the right password. Please try again or request a new one.'.
Exp title 'false' matched with act title 'false'.
Exp title 'Welcome! | LinkedIn' matched with act title 'Welcome! | LinkedIn'.
Exp msg 'false'  matched with act msg 'false'.
Exp title 'World's Largest Professional Network | LinkedIn' matched with act title 'World's Largest Professional Network | LinkedIn'.

===============================================
Suite
Total tests run: 5, Failures: 0, Skips: 0
===============================================




16 comments:

  1. Could you please let me know what can i need add to "Add TestNG library."? . I have added TestNG.jar to "TestNG" folder , but i got "SuperTestNG cannot be resolved to a type" error.

    ReplyDelete
    Replies
    1. Please find the solution here-

      http://selenium-makeiteasy.blogspot.in/2014/06/how-to-add-testng-to-project.html

      Delete
  2. I am also facing the same problem "SuperTestNG cannot be resolved to a type" and driver cannot be resolved to a variable. I have already added the testNG project library but still error persisted.
    I guess we should use BasePage instead of SuperTestNG

    ReplyDelete
    Replies
    1. No Amit , That is correct only. You need to extend the SuperTestNG class only. Please check again. May be you have done some wrong setup.

      Delete
  3. Thank you Sanjay for sharing this project. It really helped. Is it possible to have a similar project which shows the usage of Keyword driven framework as well?
    Thanks in advance.

    ReplyDelete
  4. hi i am new to selenium automation.can u explain me abt testng framework design in detail.

    ReplyDelete
  5. pls explain in detail abt testng

    ReplyDelete
  6. Please follow this link to learn more about testng in details http://selenium-makeiteasy.blogspot.in/search/label/TestNG

    ReplyDelete
  7. Clear explanation. Can u explain Jenkins and how to use them

    ReplyDelete
  8. Please find the jenkins details here-
    http://selenium-makeiteasy.blogspot.in/search/label/Jenkins

    ReplyDelete
  9. Please suggest any solution of following scenario :

    I need to execute all testcases first on one browser such as firefox then after completion of all testcases executed on firefox , again execution of testcases should be started automatically on another browser like google chrome... .


    This is not parallel execution case.

    ReplyDelete
  10. you can add the same test case two times in testng and pass the parameter from testng itself for different browser.
    Please refer this link how to pass the parameter from testng.xml_
    http://selenium-makeiteasy.blogspot.in/search/label/%40Parameters

    ReplyDelete
  11. @ Sanjay - lot of good information provided .. thanks a ton.

    ReplyDelete
  12. Hi sanjay,
    how to manage explicit wait in test cases? Can i add common explicit wait function in BasePage and re-use it in all test cases?

    ReplyDelete
  13. how to explain POM framework in selenium with diagram to interviewer ?

    ReplyDelete