Thursday, 4 June 2015

Maven, TestNG and Selenium

Setup:



  1. Eclipse: Download Eclipse from "https://eclipse.org/downloads/"


  1. goto "Help" and click on Eclipse Marketplace




  1. Serach for Maven and click on install












  1. Under "Help" click on "Install New Software"



  1. Click on Add



  1. Enter Name: TestNG and for Location: http://beust.com/eclipse



  1. Select TestNG and click on Next and complete the installation.


Creating Project in Maven

  1. Click on File then New then Other




  1. New window will open. Expand Maven folder and Click on "Maven Project"










  1. Enter Required Group Id: automationTesting, Artifact Id: com.Automation and then Click on Finish



  1. Create your Test/Class under src/test/java



  1. Your class should look like:




  1. Create testng.xml for Test Suite and Test



  1. Create pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Automation</groupId>
<artifactId>com.project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>com.project</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<sourceDirectory>src/java</sourceDirectory>
<testSourceDirectory>src/test</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>




RUNNING with MAVEN

  1. Right Click on Project then Run As then "Maven build ..."



  1. Define Goals: as "package" and click on "Run"


Challenges in API testing & Best Practices to Overcome API Testing Challenges

   Challenges in API testing:


  1. Selecting and Integrating GUI/API Framework:

    Solution: Always aim to work in the same language in which the application has been built. Also, opt for framework that offers support for GUI tests to allow for better integration and streamlined testing. By adhering to these two guidelines we will improve communication between development and testing teams and expedite release. Define the scope and makeup of the testing framework: Ideally, the selected framework will support GUI tests, as this will allow for easy integration between both GUI and API functions. The ideal framework will also incorporate API libraries, reporting tools, and build management tools
  2. Selecting Data Structure for Handling Requests/Responses:

    Solution: Requests/responses during testing are best handled using data structures, as they provide a firm level of control on the overall flow of our test scenarios. Select test scenarios that mirror actual usage and can be successfully executed within the active time of the API function.
  3. Handling Timeouts and Other Errors During Testing:

    Solution: Timeouts and other various errors may become apparent during execution. These are most effectively dealt with using an exception-handling mechanism.Splitting our test data into dynamic and static states will assist in proper management of the scripts that have been developed. For example, anauthorization token would be a dynamic parameter, whereas a developer key would be a static parameter. Once split, these can be used alongside other test data.
  4. Setting up testing environment for API testing seems little complex, Database and server should be configured as per the application requirements, Once installation is done, API Function should be called to check whether that API is working.
  5. Main challenges in API testing is Parameter Combination, Parameter Selection, and Call Sequencing
  6. There is no GUI available to test the application which makes difficult to give input values
  7. Validating and Verifying the output in different system is little difficult for testers
  8. Parameters selection and categorization required to be known to the testers
  9. Exception handling function needs to be tested
  10. Coding knowledge is necessary for testers

Best Practices to Overcome API Testing Challenges


  1. Test for Expected Usage: It is not feasible to test all possible scenarios, hence testing scenarios should replicate most likely usage. An example scenario would be one that involves interaction between several different modules of the application.
  2. Adopt Appropriate Test Techniques: To ensure efficient code coverage and to maximize the effect of our testing cycle, our API testing team should utilize techniques such as equivalence classes, boundary value analysis, and error guessing. Selected parameters should test functionality. 

Best Practices Selenium using PHPUNIT



Best practices for Maintaining Existing Scripts and New Scripts
    PHPUNIT
  • We will provide reports in XML, XHTML
  • Business Driven Development style readable steps
  • Data Cleanup
  • Page Objects and Step Objects included
  • We will test Controllers, library and model functions
  • Can integrate with Zend framework
  • Parallel Execution
  • We will provide code coverage
  • We will use specific assertions
    POM (Page Object Model) based approach
  • This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements
  • Name of these methods should be given as per the task they are performing i.e., if a loader is waiting for payment gateway to be appear, POM method name can be "make_GoogleSearchDisplay()"




Use Files library for code reuse
  • If the flow of our logic changes we just have only one place to update
  • To test whether our changes worked, we only have to run one of the tests to verify. All other tests use the same code so it should work
  • A lot more expressive as we look at the code. With well named methods, we create a higher level of abstraction that is easier to read and understand
  • If the UI changes (an id of a field), we go to our one method, update the id and we are good to go
  • Flexible and extensible: The possibilities are limitless. At this point we can use conditions, loops, exceptions, you can do your own reporting, etc..


For frequent changes we use Excel sheet or CSV file
We can use excel sheet for storing our test data. It is easy to maintain the data in excel and also easy to modify the test data as per the business requirements.




Use custom exception which can be easily understood
  • Custom exception helps to clearly understand the error. Please find below some key points for using custom exceptions:
  • Add situation specific data to an exception. Ideally this data would help another developer to track down the source of the error.
  • Provides a type safe mechanism for a developer to detect an error condition. 
  • No existing exception adequately described my problem.

    Additional For Existing Scripts
  • Execute existing scripts (Regression Testing) on different environments.
  • Create well structured Test data.
  • Bug reporting for failures.
  • Will not modify developed code.

Coding Standards Automation Testing



Coding Standards are suggestions that will help us to write automation Scripts code using any language. Naming conventions for creating automation Script, objects, variables and procedures. Commenting standard in the scripting, Text formatting and align guidelines for the script.


Naming Conventions: Naming Conventions should be followed at all levels of the project for e.g. Variables, Constants, Objects
  • Standards of Variable Naming

For easy readability purpose use the prefixes like below examples, along with descriptive names for variables in Automation Script code.

Ex:
String --sName
Object --oObjectname
Integer,Byte,Long --nNumber
Array --arrListitems
Boolean --bYes
Single,Double --fPrice
DateTime --dToday
Error --errResponse
Currency --cRupees
  • Standards of Constant Naming

Constant names should be in uppercase with underscores (_) between the words.

Ex:USER_LIST_MAX, NEW_LINE
  • Object Naming Standards

For the objects created in the Automation script, use prefix “o” (small letter o).

Ex:
ADODB.Connection -- oConn
ADODB.Recordset -- oRset
Scripting.xmlobject -- oXmlobj
Scipting.FileSystemObject -- oFsobj
Scipting.TextStream -- oTxt
Scripting.Dictionary -- oDic
Shell.Application -- oWsh
Excel.Application -- oXls


Reusability: Abstraction to be followed for Code Reuse at all possible levels – function library, config file, POM


    POM (Page Object Model) based approach
  • POM approach is to create a separate class file which would find web elements , fill them or verify them. This class can be reused in all the scripts using that element. In future if there is change in the web element , we need to make change in just 1 class file and not 10 different scripts.
  • This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements
  • A lot more expressive as we look at the code. With well named methods, we create a higher level of abstraction that is easier to read and understand
  • If the UI changes (an id of a field), we go to our one method, update the id and we are good to go




For frequent changes we use Excel sheet or CSV file

  • We can use excel sheet for storing our test data. It is easy to maintain the data in excel and also easy to modify the test data as per the business requirements.


Use custom exception which can be easily understood

    Custom exception helps to clearly understand the error. Please find below some key points for using custom exceptions:
  • Add situation specific data to an exception. Ideally this data would help another developer to track down the source of the error.
  • Provides a type safe mechanism for a developer to detect an error condition. 
  • No existing exception adequately described my problem.


    Maintainability and Extensibility:
  • Use of Interface based programming to achieve the maintaince of existing scripts and extension of new ones


Portability.
  • Code often has to run on different hardware or be compiled with different compilers. If it follows a set standard, it will likely be easieror even completely painlessto move it to a different platform.


No hard coding to be used unless absolutely necessary.


Documentation to be taken care at all levels of project.

Best Practices Selenium using TestNG



Best practices for Maintaining Existing Scripts and New Scripts
    Using TestNG
  • It enables user to set execution priorities for the test methods. As per business requirement Test method can be executed.




  • Parallel testing can be done using TestNG
  • Supports annotations for e.g @AfterTest, @BeforeTest, @AfterSuite, BeforeSuite()
  • We can do parameterization using TestNG
  • Readily supports integration with various tools and plug-ins like build tools (Ant, Maven etc.), Integrated Development Environment (Eclipse)
  • Facilitates user with effective means of Report Generation using ReportNG
  • TestNG, by default, generates a different type of report for example an HTML and an XML report.


POM (Page Object Model) based approach
  • This Page class will find the WebElements of that web page and also contains Page methods which perform operations on those WebElements
  • Name of these methods should be given as per the task they are performing i.e., if a loader is waiting for payment gateway to be appear, POM method name can be "make_GoogleSearchDisplay()"




Use Files library for code reuse
  • If the flow of our logic changes we just have only one place to update
  • To test whether our changes worked, we only have to run one of the tests to verify. All other tests use the same code so it should work
  • A lot more expressive as we look at the code. With well named methods, we create a higher level of abstraction that is easier to read and understand
  • If the UI changes (an id of a field), we go to our one method, update the id and we are good to go
  • Flexible and extensible: The possibilities are limitless. At this point we can use conditions, loops, exceptions, you can do your own reporting, etc..


For frequent changes we use Excel sheet or CSV file
We can use excel sheet for storing our test data. It is easy to maintain the data in excel and also easy to modify the test data as per the business requirements.






Use custom exception which can be easily understood
  • Custom exception helps to clearly understand the error. Please find below some key points for using custom exceptions:
  • Add situation specific data to an exception. Ideally this data would help another developer to track down the source of the error.
  • Provides a type safe mechanism for a developer to detect an error condition. 
  • No existing exception adequately described my problem.

    Additional For Existing Scripts
  • Execute existing scripts (Regression Testing) on different environments.
  • Create well structured Test data.
  • Bug reporting for failures.
  • Modify developed code, as coding standards are already specified.

Thursday, 12 March 2015

FILE WRITER CLASS (Core JAVA)

·         This class is used to write characters into a file after writing all the characters stream should be flushed using flush() method. After performing all the write operations the stream should be closed using close() method
NOTE: Generally flush() and close() will be written inside finally block
·         The File Reader class is used to read characters from the file. These characters are read by using read()
·         Read() will read the characters and stores as array of characters

·         Take input from keyboard (resume)

IO PACKAGE (Core JAVA)

·         java provides IO package to write program which can handle file as well as directory of system file
·         File class is used for basic operations. For e.g.,
1.       creating directory or folder
2.       deleting directory or folder
3.       to check existence of directory or folder
4.       to get path of directory or folder
5.       to check permission of directory or folder
·         some other basic classes are also available in IO package such as:
1.       File writer File reader:  It is used to read and write single character internally
2.       Buffer Reader and Buffer Writer: It is used to read and write Buffer of characters
3.       Object Input Stream and Object OutputStraem: It is used to read and write java object and instance
4.       Binary Input Stream and Binary Output Stream: It is used to read and write binary characters and executable files
5.       File Input Stream and File Output Stream: It is used for general purpose and is basically used in selenium
FILE WRITER CLASS
·         This class is used to write characters into a file after writing all the characters stream should be flushed using flush() method. After performing all the write operations the stream should be closed using close() method
NOTE: Generally flush() and close() will be written inside finally block
·         The File Reader class is used to read characters from the file. These characters are read by using read()
·         Read() will read the characters and stores as array of characters

·         Take input from keyboard (resume)