Friday, 29 August 2014

Downloading a file using webdriver

Download a file using web driver :

    It is always a challenge to work with open source tools,as we do not get support from the author of the application and will have lot of limitations,one of the limitation of the web driver is that,it cannot interact with other than web applications,in such cases ,saving a file to your required location need to interact with Firefox popups to pick the location,but selenium(web driver) cannot support to do it

Solution : We can over come this problem using a simple trick (i.e) changing the browser settings ,when ever you download a file from browser(here it is firefox ),it will give you the popup with the options  "Open with" or "Save as ",this popup can be turned off using the mime types. See the below screenshot



    
If we have the above settings,then Firefox will not gives you the popup ,But question here,where this file can be saved? ,answer will be Firefox will save the file in a folder path given in your Firefox settings i.e 

browser.download.dir=<your downlod location>
browser.download.folderList=2;
browser.helperApps.neverAsk.saveToDisk=application/zip

We can achieve these settings with the web driver using the Firefox profile concept,below is the example which will download the selenium client libraries using the web driver:

 
Note : need to change the mime types for the type of the file  you are downloading.I have used the mime type for .zip file,that is 

    Profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");


Happy Automation!!!


 


Tuesday, 25 March 2014

Getting Started with Selenium GRID

Getting Started with Selenium GRID

This post covers very basics of selenium Grid,I will cover adavnced concepts of grid in upcoming posts.

Selenium-Grid allows you run your tests on different machines against different browsers in parallel.it have 2 basic components

1.       HUB:

Hub is a key component in selenium grid, it takes care of following things

·         It receives a test to be executed along with information on which browser and ‘platform’ (i.e. WINDOWS, LINUX, etc.) where the test should be run.


2.       NODE:
It is another component of selenium grid, which can be used by the HUB to pass the tests to execute,   A node supposed to be  register to the grid with the settings like browser, browser version,OS,OS version….etc.


Pre requisites:

·         Java
·         Eclipse with TestNG Plugin

INSTALLATION:


 Install the latest version of selenium server jar file from http://docs.seleniumhq.org/download/

Starting a HUB:


It is always a best practice a HUB first then Nodes, follow the below steps to start a hub

1.       Open command prompt
2.       Go to the folder where your Selenium server.jar file resides
3.       Run the following command :

4.       If your grid successfully started, then you would be watching the following message in command prompt


You can verify your grid/Hub configuration by hitting the following URL on any available browser:
             http://localhost:4444/grid/console

Screenshot:
                                              


Starting NODE:


 Ideally a HUB can be one, but you can start more than node to register with the hub, But we have to make sure that each node should use different ports, follow the below steps to start the node
1.       Open command prompt
2.       Go to the folder where your Selenium server.jar file resides
3.       Run the following command :
     


4.       If your Node successfully registered, then you would be watching the following message in command prompt
 

      You can verify node registration  by hitting the following URL/refreshing the URL:
             http://localhost:4444/grid/console
     Expected Screenshot:

          


Creating Scripts:


To execute scripts with GRID, it is suggested that, write your scripts using the TestNG framework, follow the below steps to create sample scripts
1.       Create a sample project in eclipse

2.       Add a package(testscripts)  to src

3.       Create simple TestNG Test case with the below code :

package testscripts;

import static org.junit.Assert.fail;

import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class NewTest4 {
  private WebDriver driver;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();

  @BeforeTest
  public void setUp() throws Exception {
              DesiredCapabilities capabilities = new DesiredCapabilities();
              capabilities.setBrowserName("firefox");
              capabilities.setPlatform(Platform.WINDOWS);           
              driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),capabilities);
           baseUrl = "http://way2automationtesting.blogspot.in/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testNew1() throws Exception {
         driver.get(baseUrl + "/");
         driver.findElement(By.cssSelector("img")).click();
  }

  @AfterTest
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
}


4.       Make the copy of the same code with different names such as NewTest2,NewTest3….etc
                          

5.       Now create a Suite.XML in your project base path with the following content:   


<?xml version="1.0" encoding="UTF-8"?>
<suite name="Test Suite" verbose="1" parallel="true" thread-count="2">
       <test name="Test">
               <packages>
                <package name="testscripts" />
   </packages>
       </test>
</suite>



Executing Scripts:


1.       Right click on your Suite.XML file
2.       Select the options as highlighted in below screen shot


Now, your scripts will be executed parallel on 3 Firefox browsers.




Wednesday, 5 February 2014

Parameterizing Web Driver test with Random value

Generating Random String :

    As a automation tester,we can come across the various steps to automate,I have come across the following situation:

Requirements :
  1. Open our application URL (http://www.someurl.com)
  2. Click on Register -> It open Registration Page
  3. Fill all the details such as  First Name,Last Name,User Name, Email.....etc
  4. Click on Submit the details
  5. Verify user created successfully
 NOTE : I want to create 100 users 

Partial Solution 1 :


  1. Create a Register method and parameter it as shown in below pic
     2.   Parameter the test  using the external data sheet (TestData.xls),This execl should have all the 100
            user's First Name,Last Name,User Name Details


Limitation of Solution 1:

     This approach will work pretty good at first iteration,But when i execute the same script multiple times ,I will get the error message as : 
  This error occurs,because we have used the excel data for previous iteration


Best Solution : 

     After trying all the possibilities,we have decided to go with the Random Generator,It gives you one unique random string each time it is executed

   Steps to use : 

  1.        Download the TestUtils.jar from     https://drive.google.com/file/d/0B9ZTXXKTZAYVUGZ5d0loWUNhMlk/edit?usp=sharing
  2.    Add it to your Class path
   3.    Import the following class in your Test.java
                Import com.webdriver.framework.*;
   4.    Use any one of the code shown below :

5.  Finally my code is looks like below : 







Appreciate your comments and suggestions :

NOTE:Thanks to the author of this method and my mentor  :Mr.Pankaj Kumar,