Friday 5 August 2016

Why Cucumber ?

It's an open debate for all the Automation Test Engineers. Few like it, few hate it.

For some it beautifully defines your scripts but for some it can be just another burden that adds to the list of tools.

The truth lies behind both the thoughts. For first point, cucumber helps in abstraction of code and plain english sentences & for the second point, its just doing what other tools do without adding another layer on their code.

Cucumber actually helps product owners & business analysts to identify the coverage of test scripts. Also , it helps in enhancing test coverage by people who don't have the knowledge of the code.

If you see the reporting of cucumber its so easy to read & understand it. Any person who doesn't know the code of automation script can also understand what all steps have been passed & failed.

Is it only useful in organisation of Business Analysts or direct clients ?

I think when I write my code with plain readable English sentences, its make it more objective. As a coder, If I just write the code somewhere the 'why' part is lost in all those lines of code. But if we write feature files, it will be somewhere in the back of mind the objective of that code.

Also, even if there is no direct client but there are developers or managers who want to see our report or tests. Is it meaningful or interesting for them to read each line of code to understand the motive of script. In this case, these feature files will be the best way to make a hold of it.

Also the benefit is to collate requirements, manual tests & automation tests at one place. And tools like cucumber helps in achieving this

Page Factory for Selenium POM Frameworks

What is Page Factory (PF) ?

Page Factory is an inbuilt optimized page object model concept for Selenium. Page factory Class is derived from Page Object Pattern. It can be used in any kind of framework like Data driven, modular & keyword.

Advantages of PF

1. It finds WebElement every time it comes. Hence, no StaleElementExceptions should come.
2. Also we should not see NoSuchElementExceptions.
3. Helps in separating Page Object Repository & Test Methods.

How to Use PF ?

- @FindBy annotation is used to find WebElement.
- initElements method is used to initialise web elements.
-@FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as attributes.

Example for using Page Factory :

Page Factory class would look like this :


Also you need to initiate this PF class in the constructor of your page object class, as follows :



PF actually keeps all the elements in cache which helps in easy & fast access to web elements hence it increases the speed of automation scripts as well.




Monday 29 September 2014

Creating Log file Using Log4j in WebDriver Framework

Tired of sysouts in your scripts !! Well, it makes your Test Scripts execution hell slow.
But you don't even want to omit sysouts from your classes.

Here, log4j (Apache Logging Services) provides you logging library for Java.

Steps to create a Logging Utility in your existing framework :

Step 1 :

Install log4j jar from here and put in lib folder of your project.

Step 2 :

Create a "log4j.properties" file in your config folder.

And write the following code in it :


# Log levels
log4j.rootLogger=INFO,CONSOLE,R

# Appender Configuration
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# Pattern to output the caller's file name and line number
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling File Appender
log4j.appender.R=org.apache.log4j.RollingFileAppender

# Path and file name to store the log file
log4j.appender.R.File=../logs/LogFile.log
log4j.appender.R.MaxFileSize=1000KB

# Number of backup files
log4j.appender.R.MaxBackupIndex=2

# Layout for Rolling File Appender
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c - %p - %m%n


For all the detailed configuration explanation of log 4j, refer this.

Here,

log4j.appender.R.File=../logs/LogFile.log
log4j.appender.R.MaxFileSize=1000KB

is the location where log file would be created and the maximum size of that log file.

Step 3 :

Now in your test script class, and call the getLogger of Logger class that log4j jar provides.
Here is the sample code :

public class OpenGoogle
 { 

  private static Logger logger=Logger.getLogger("OpenGoogle.class"); // write this line 

  public void testOpenGoogle() throws InterruptedException{

   driver.get("https://www.google.co.in/");
   assertTrue(driver.getTitle().contains("Google"));
   logger.info("You have opened Google.com"); // use info for printing in log file

  }

Create the logger class object and call the logger which writes in the log file.

Now, replace all the "sysout" statements in your script with "logger.info" or any other log 4j configuration according to your requirement.

Doing this, all your sysouts which used to slow down the test execution will reduce and also you will be able to see the full log file once you complete your execution.

And you are done with adding Logging Services in your framework !

Happy Logging !

Customized Explicit Waits in WebDriver

When is the need for Customized Explicit Waits ?

There are situations at times when we need to explicitly write a custom condition which are not present in ExpectedCondition class of WebDriverWait class.

Example of a Customized Explicit Wait Condition:

Lets take an example where we need to wait for a URL to change,

So we will do Anonymous class Instantiation of Expected Condition. In these types, wait is applied on Boolean Condition. Here we have our own Boolean Condition on which wait needs to be applied.

Here is the code for it:


//customized Explicit Wait Condition
  ExpectedCondition e = new ExpectedCondition<Boolean>() {
           public Boolean apply(WebDriver d) {
            String previousURL = driver.getCurrentUrl();
            logger.info("Waiting...");
             return (d.getCurrentUrl() != previousURL);
           }
         };

In this example,
it will wait for sometime and then again check until a URL change is there.

So, this is how customized explicit waits works.

Say Hello to 'Agile Testing'

Agile and Software testing go hand in hand

Agile has revolutionized the IT world. Agile is not only a process but a methodology. It's about understanding the human behavior and changing the mindset as per agile principles and values.

Scrum is the most common methodology which is being followed in majorly all the organizations now a days. Scrum is a time-boxed process which motivates the team members to complete the stories in that time frame and increases the productivity of the Agile Teams.

The key values of Agile Testing are :

"The whole Team concept i.e. the whole Team is responsible for the Quality Work (including Developers and Testers)"

 Agile Testing involves following :


  • Get the insights of the Business Requirement in Planning meeting while start of the Sprint together with the whole Team.
  • testing will be an on - going process throughout the Sprint as soon as developers gives a working code.
  • Acceptance Criteria will be added in the story after the Planning only.
  • The developers have the responsibility to unit test their code and after Unit Test only it should be ready for Testing.
  • Testing should involve Manual as well as Automated Test Cases.

Agile testing involves open communication with the developers, business analysts and other stake holders. 


Be more communicative and open to new ideas.

Happy Agile Testing !!


Sunday 28 September 2014

Difference between Implicit Waits and Explicit Waits

Implicit Waits :

Set implicit wait once and it apply for whole life of WebDriver object.
eg.

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);



Explicit Waits : 

- Custom code for a particular element to wait for particular time of period before executing next steps in your test.
-  Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this.

eg.
//explicit wait for search field
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("search")));

This is the basic difference between Explicit and Implicit Waits with an example.

Explicit Waits are good to use as it waits for a particular web page element on which you can apply wait.

Sending e-mail notification using TestNG , ANT and WebDriver

Sending e-mail notifications for daily report of our Test Suite Execution has become an important business requirements in majorly all the on-going projects.

Email notification can be send easily without any typical Java code but via TestNG and WebDriver only.

We will send the notification using one of the features of TestNG and the code for that is :


<!-- @ Purpose: This function is for sending an auto email after sanity & regression
   run is complete. -->

<target name="send-email">
<mail
    tolist="abc@xyz.com"
    cclist="pqr@tuv.com"
    from="efg@hij.com" subject="Sanity & Regression Suite Automation Report"
    mailhost="smtp.gmail.com" mailport="465" ssl="true"
    user="efg@hij.com" password="*****">
   
    <message>Hi,

 PFA the HTML Report of Automation Sanity & Regression Suite Execution.

 Environment : ${baseURL}

 Regards,
 Automation Team

 *** This is an automatically generated email, please do not reply to this email id. ***
    </message>
<attachments>
 <fileset dir="${outputDir}">
      <include name="index.html" />
      <include name="emailable-report.html" />
 </fileset>
</attachments>
</mail>

<tstamp>
   <format property="send.time" pattern="MM/dd/yyyy hh:mm aa" unit="hour" />
</tstamp>

<echo message="Mail successfully sent at ${send.time}" />
</target>


Create a new target for sending e-mail in your build.xml and call that target name to send notification.

Also you need Java Mail API Jar. Download it from here and Activation Jar from here. Put these jars in the lib folder of ANT.

The Email send notification is configured using TestNG, ANT and WebDriver.

Happy E-mailing..!