Sunday, August 23, 2015

Handle Download popup dialog box using selenium Webdriver




In this post we are going to handle the "Download popup dialog box" . when we are downloading files from Webpages.

Usually when we are downloading files from webpages. we get a popup like below.


We can't handle this popup using Selenium Webdriver.

To solve this issue in selenium we need to use FireFoxprofile to disable the popup dialog window.



In this Example:
  1. Setting Perference in the FireFoxProfile to disable the popup
   String downloadPath = "C:\\mydownload\\";
             
              FirefoxProfile myprofile=new FirefoxProfile();
              myprofile.setPreference("browser.download.folderList", 2);
              myprofile.setPreference("browser.download.manager.showWhenStarting", false);
              myprofile.setPreference("browser.download.dir", downloadPath);
              myprofile.setPreference("browser.helperApps.neverAsk.openFile","application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
              myprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
              myprofile.setPreference("browser.helperApps.alwaysAsk.force", false);
              myprofile.setPreference("browser.download.manager.showAlertOnComplete", false);
              myprofile.setPreference("browser.download.manager.closeWhenDone", false);


   2. Set the Profile in FirefoxDriver.

     driver=new FirefoxDriver(myprofile);

    
     3. Navigate to firefox official webpage to download the setup

    driver.get("https://www.mozilla.org/en-US/firefox/new/");
              Thread.sleep(5000);
              driver.findElement(By.xpath("//*[@id='download-button-desktop-release']/ul/li[1]/a/span")).click();





Below See the full Source Code




Sourcecode:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class DownloadProfile {

       public static void main(String[] args) throws InterruptedException {
              // TODO Auto-generated method stub
              WebDriver driver;
              String downloadPath = "C:\\mydownload\\";
             
              FirefoxProfile myprofile=new FirefoxProfile();
              myprofile.setPreference("browser.download.folderList", 2);
              myprofile.setPreference("browser.download.manager.showWhenStarting", false);
              myprofile.setPreference("browser.download.dir", downloadPath);
              myprofile.setPreference("browser.helperApps.neverAsk.openFile","application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
              myprofile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
              myprofile.setPreference("browser.helperApps.alwaysAsk.force", false);
              myprofile.setPreference("browser.download.manager.showAlertOnComplete", false);
              myprofile.setPreference("browser.download.manager.closeWhenDone", false);
             
             
              driver=new FirefoxDriver(myprofile);
              driver.get("https://www.mozilla.org/en-US/firefox/new/");
              Thread.sleep(5000);
              driver.findElement(By.xpath("//*[@id='download-button-desktop-release']/ul/li[1]/a/span")).click();
             
              System.out.println("Download done without popup");

       }

}




Run the project, Notice that Download Starts without popup the dialog window.





Thats it,

Thanks, Have a great day !!.

Tuesday, August 18, 2015

SET THE BROWSER LANGUAGE USING SELENIUM WEBDRIVER (Localization)







In this post we are going to see how to set browser language using selenium webdriver. To test the Localization Testing.
  1. Localization:(sometimes shortened to "l10n") is the process of adapting a product or service to a particular language, culture, and desired local "look-and-feel."
To achieve this we need to use FirefoxProfile class.


In this program

  1. Create object to FirefoxProfile Class
     FirefoxProfile profile=new FirefoxProfile();


   
      2. Set Language use setPreference method.

      profile.setPreference("intl.accept_languages","de");


   3Create Firefox Driver and Pass the profile Object

      WebDriver driver;

   driver=new FirefoxDriver(profile);
   driver.get("http://www.google.com");





Source Code




import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class Languagetest {

       public static void main(String[] args) {
              // TODO Auto-generated method stub
             
              FirefoxProfile profile=new FirefoxProfile();
              //Setting the browser language to de:germany
              profile.setPreference("intl.accept_languages","de");
              WebDriver driver;
              driver=new FirefoxDriver(profile);
              driver.get("http://www.google.com");

       }

}


Run the Program:


ScreenShot:







Your browser language set to German Language.


Thanks

Have a great day!!

Monday, August 17, 2015

Capture Screen Shot on Failure Test cases using TestNG.








Hi guys in this post we are going to see, how to capture ScreenShot on Failure Test cases using TestNG.

Taking Screenshot on the failure is very important to understand the Bug clearly to the manual Testers and developers.

Scenarios for Testcase Failures:


  1. Unable to find elements in the Webpages
  2. Timeout to finding Webelements.
  3. Assertion Failure
  4. Application Error 

In this post we are going to see how this going to work in action.

In this Example we are going to forcefully fail the testcase  by Asserting the title.(Giving the wrong Title)


Steps:

  1. We are creating a new class to capture ScreenShots alone.

import java.io.File;
import java.io.IOException;

import javax.print.attribute.standard.MediaSize.Other;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;


public class CaptureScreenshot {
      
       public static void takescreenshot(WebDriver driver,String Screenshotname) throws IOException
       {
              TakesScreenshot takescreenshot=(TakesScreenshot)driver;
              File source=takescreenshot.getScreenshotAs(OutputType.FILE);
              FileUtils.copyFile(source, new File("./Screenshots/"+Screenshotname+".png"));
              System.out.println("Screenshot Taken Successfully!!!!");
             
       }

}


This class will just capture the screenshots by calling this Method in Test class

CaptureScreenshot.takescreenshot(driver, "Titlefailed");




      2. Create a new Class for the Test.

      3. On BeforeTest annotation Write code to  Open Browser and Navigate to http:\\automationplace.blogspot.com



@BeforeTest
       public void Setup()
       {
              driver=new FirefoxDriver();
              driver.manage().window().maximize();
              driver.get("http:\\automationplace.blogspot.com");
             
              //Make testcase fail by checking the Title
             
       }


       4. On the @Test annotation, Just verify the title (Here am forcefully failing the Testcase by giving wrong title on Expected String in Assert.equal method)


String ExpectedTitle="Automation";

 forcefully failing the Testcase by giving wrong title on the ExpectedTitle.

@Test
       public void verifytitle()
       {
              String ActualTitle=driver.getTitle();
              System.out.println(ActualTitle);
              //Removed "Place" string from Title to fail testcase
              Assert.assertEquals(ActualTitle, ExpectedTitle);
       }


     5. Capture ScreenShot on the @AfterMethod,

     Note :

1. We will use ITestResult Interface which will provide us the test case execution status and test case name.
2.@AfterMethod is another annotation of TestNG which will execute after evert test execution whether test case pass or fail @AfterMethod will always execute.

@AfterMethod
       public void TearDown(ITestResult result) throws IOException
       {
              //If the Testcase fail then only it enters to if condition block
             
              //.getStatus will return Test "Pass" or "Fail"
             
              System.out.println("Testcase status is"+result.getStatus());
              System.out.println("Iresult status is"+result.FAILURE);
             
              if(result.FAILURE == result.getStatus())
              {
                     //Now we need to capture Screenshot
                     //use CaptureScreenshot Class to Take Screenshot
                    
                     CaptureScreenshot.takescreenshot(driver, "Titlefailed");
                    
              }
             
             
              driver.quit();
       }


1.If the Testcase fail then only it enters to if condition block

2.getStatus will return Test "Pass" or "Fail"




It will Store ScreenShot on the Project folder like this








Source Code: 


Class to take ScreenShot:  CreateScreenShot.java

import java.io.File;
import java.io.IOException;

import javax.print.attribute.standard.MediaSize.Other;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;


public class CaptureScreenshot {
      
       public static void takescreenshot(WebDriver driver,String Screenshotname) throws IOException
       {
              TakesScreenshot takescreenshot=(TakesScreenshot)driver;
              File source=takescreenshot.getScreenshotAs(OutputType.FILE);
              FileUtils.copyFile(source, new File("./Screenshots/"+Screenshotname+".png"));
              System.out.println("Screenshot Taken Successfully!!!!");
             
       }

}



Class for the Testcase :FailedTestCase.java

import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


public class FailedTestcase {
      
       WebDriver driver;
       String ExpectedTitle="Automation";
      
       @BeforeTest
       public void Setup()
       {
              driver=new FirefoxDriver();
              driver.manage().window().maximize();
              driver.get("http:\\automationplace.blogspot.com");
             
              //Make testcase fail by checking the Title
             
       }
      
       //Failed Testcase
       @Test
       public void verifytitle()
       {
              String ActualTitle=driver.getTitle();
              System.out.println(ActualTitle);
              //Removed "Place" string from Title to fail testcase
              Assert.assertEquals(ActualTitle, ExpectedTitle);
       }
      
      
       @AfterMethod
       public void TearDown(ITestResult result) throws IOException
       {
              //If the Testcase fail then only it enters to if condition block
             
              //.getStatus will return Test "Pass" or "Fail"
             
              System.out.println("Testcase status is"+result.getStatus());
              System.out.println("Iresult status is"+result.FAILURE);
             
              if(result.FAILURE == result.getStatus())
              {
                     //Now we need to capture Screenshot
                     //use CaptureScreenshot Class to Take Screenshot
                    
                     CaptureScreenshot.takescreenshot(driver, "Titlefailed");
                    
              }
             
             
              driver.quit();
       }
      


}


Run the program .

You find the Screenshot on the project folder after Execution.







Thats it..Have a great day.

Thankyou!!!!