Saturday, 5 April 2014

Selenium WebDriver : How to TakeScreenshot Of WebElement



Selenium WebDriver : How to TakeScreenshot Of WebElement




Every one know that it is possible to take the screenshot of page using Selenium, but most of the people doesn't that there is a possibility for taking the screen of WebElement also....!, you might have a doubt what is the use of taking the screen shot of the element ?

One of the interesting answer is for comparing the images, Surprised.....! but it is possible compare the images using Selenium WebDriver
Here we go.....:)

                   http://qaautomationworld.blogspot.in/

          Selenium WebDriver : How to TakeScreenshot Of WebElement

1. Taking the screen shot of the page. Every selenium automation engineer know it... :)
WebDriver driver = new FirefoxDriver();
driver.get("http://qaautomationworld.blogspot.in/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\qaautomationworld\\qaautomationworld.png"));

2. Taking the screen shot of particular WebElement

    
  •  Grab full-page screenshot
  •  Get WebElement position
  •  Get WebElement dimension
  •  Crop WebElement image from page screenshot
  • Save WebElement image to disk

 WebDriver driver = new FirefoxDriver();
 driver.get("http://qaautomationworld.blogspot.in/");
 WebElement element = driver.findElement(By.xpath("//img[@alt='Selenium WebDriver Tips']"));
 WrapsDriver wrapsDriver = (WrapsDriver) element;
 //Get the entire Screenshot from the driver of passed WebElement
 File screen = ((TakesScreenshot) wrapsDriver.getWrappedDriver()).
 getScreenshotAs(OutputType.FILE);
 //Create an instance of Buffered Image from captured screenshot
 BufferedImage img = ImageIO.read(screen);
 // Get the Width and Height of the WebElement using
 int width = element.getSize().getWidth();
 int height = element.getSize().getHeight();
 //Create a rectangle using Width and Height
 Rectangle rect = new Rectangle(width, height);
 //Get the Location of WebElement in a Point.
 //This will provide X & Y co-ordinates of the WebElement
 Point p = element.getLocation();
 //Create image by for element using its location and size.
 //This will give image data specific to the WebElement
 BufferedImage dest = img.getSubimage(p.getX(), p.getY(), rect.width,
 rect.height);
 //Write back the image data for element in File object
 ImageIO.write(dest, "png", screen);
 
 FileUtils.copyFile(screen, new File("E:\\qaautomationworld\\qaautomationworld.png"));


Do comment if you have any queries on this post

Note: Reference for this post Selenium Testing Tools Cookbook 

Friday, 14 February 2014

File Downlaoding using Selenium Webdriver

  File Downlaoding using Selenium Webdriver:

Files downloading using WebDriver is possible by using profile set up in FireFox. Following is the sample code for setting preferences , The important aspect to go with downloading is you need to know the MIME type of the file that you wanted to download.

For Example if you wanted to download .txt (i.e text file) file then you need to add text/plain in the browser.helperApps.neverAsk.saveToDisk preference saveToDisk is the profile preference which excludes windows popup of Open with / save .

you can find the MIME types for different file type in the following link

http://www.freeformatter.com/mime-types-list.html

http://www.sitepoint.com/web-foundations/mime-types-complete-list/  

Following is the sample code snippet  for file downloading



       FirefoxProfile profile = new FirefoxProfile();
        String path="d:\\downloads123";
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", path);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword,application/csv,text/csv,image/png ,image/jpeg, application/pdf, text/html,text/plain,application/octet-stream");
       
     
       
        WebDriver driver = new FirefoxDriver(profile);




Please do comment hear if you have any queries on this topic.


Please do comment for any queries

Friday, 7 February 2014

Selenium WebDriver : Implicit Wait Vs Explicit Wait Vs Custom-expected Wait

Using Selnium 2 (i.e WebDriver) we have number of ways for implementing WaitForAnElement, In this post will try to explain them with an example.

WaitForElement with an implicit wait :


The Selenium WebDriver provides an implicit wait for synchronizing tests. When an implicit
wait is implemented, if WebDriver cannot find an element in the Document Object
Model (DOM), it will wait for a defined amount of time for the element to appear in the DOM.

Once set, the implicit wait is set for the life of the WebDriver object's instance. However, an
implicit wait may slow down your tests when an application responds normally, as it will wait
for each element appearing in the DOM and increase the overall execution time.

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

Until the end of a test or an implicit wait is set back to 0, every time an element is searched
using the findElement() method, the test will wait for 10 seconds for an element to appear.


WaitForElement with an explicit wait:


The Selenium WebDriver also provides an explicit wait for synchronizing , which provides
a better control when compared with an implicit wait. Unlike an implicit wait, you can write
custom code or conditions for wait before proceeding further in the code.

An explicit wait can only be implemented in cases where synchronization is needed and the
rest of the script is working fine.

The Selenium WebDriver provides WebDriverWait and ExpectedCondition classes for
implementing an explicit wait.

The ExpectedCondition class provides a set of predefined conditions to wait before
proceeding further in the code. The following are some common conditions
that we frequently come across when automating web browsers supported by the
ExpectedCondition class:

An element is visible and enabled          elementToBeClickable(By locator)              
An element is selected                          elementToBeSelected(WebElement element)
Presence of an element                         presenceOfElementLocated(By locator)        
Title                                                    titleContains(java.lang.String title)                  

WebDriverWait wait = new WebDriverWait(driver, 10);
 Next, ExpectedCondition is passed to the wait.until() method as follows:
 wait.until(ExpectedConditions.titleContains("selenium"));

The WebDriverWait object will call the ExpectedCondition class object every 500
milliseconds until it returns successfully.

WaitForElement with custom-expected conditions :


The Selenium WebDriver also provides a way to build custom-expected conditions along with
common conditions using the ExpectedCondition class.

The Selenium WebDriver provides the ability to implement the custom ExpectedCondition
class along with the WebDriverWait class for creating a custom-wait condition, as needed
by a test. In this example, created a custom condition, which returns a WebElement
object once the inner findElement() method locates the element within a specified
timeout as follows:

WebElement message = (new WebDriverWait(driver, 5))
.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("qaautomationworld"));
}});


Other popular posts:




Wednesday, 5 February 2014

Installing m2eclipse – Maven Plugin for Eclipse

In this tutorial, we will see how to install m2eclipse plugin which provides Maven integration in Eclipse IDE.


Before installing eclipse plugin in ellipse. its better to install maven in your machine  look at this post Maven installation

Environment Used

  • Java SE 6
  • Eclipse Juno for Java EE Developers  IDE
  • Maven Eclipse Plugin – m2eclipse version 1.2


    Before going forward make sure you have set the JDK path as JAVA_HOME in your environmental variables.

 



Online Installation (From Update Site) of Maven Plugin m2eclipse:

Step 1:

Installing m2eclipse is fairly simple. Start Eclipse then go to:
Help -> Install New Software…
Copy this link http://download.eclipse.org/technology/m2e/releases for the latest Stable Release into Eclipse and hit Enter.


When the site loads, select the features to install, or click the Select All button. For our requirement select “Maven Integration for Eclipse” as shown above.
Checking [x] Contact all update sites during install to find required software might take sometime and this is optional.

Step 2:

  • Click Next to view Installation Details.
  • Click Next to agree the license terms, and click Finish.

Step 3:

If you get any warning message when installing, click OK to continue.

This will take few minutes to install the Maven plugin and once done restart the Eclipse.


Creating New Maven Project in Eclipse


After installing the Maven plugin for Eclipse, you can check if the installation is successful by creating a new Maven project.
You should see a Maven folder in the New project wizard as shown below



Now you have successfully installed “Maven plugin – m2eclipse” in Eclipse IDE.


Other popular posts:


How to install Maven on Windows

All possible ways for handling DragAndDrop using Selenium

All Possible ways for switching frames in Selenium

Automating tables tags using Selenium

Selenium Webdriver Integration with JBheave (BDD)