Automation Using Selenium Webdriver

Friday, 28 October 2016

How To Handle Unexpected Alerts In Selenium WebDriver

I have one example where alert Is displaying when loading software web page. So we can check for alert Inside try catch block after page load as shown In bellow given example. After loading page,
It will check for alert. If alert Is there on the page then It will dismiss It else It will go to catch block and print message as shown In bellow given example.

Run bellow given example In eclipse with testng and observe the result.



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class unexpected_alert {
 WebDriver driver;

 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();  
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  driver.get("http://any website alert");
 }

 @AfterTest
 public void tearDown() throws Exception {
  driver.quit();
 }

 @Test
 public void Text() throws InterruptedException {
  //To handle unexpected alert on page load.
  try{
   driver.switchTo().alert().dismiss();
  }catch(Exception e){
   System.out.println("unexpected alert not present");
  }

  driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("fname");
  driver.findElement(By.xpath("//input[@name='lname']")).sendKeys("lname");
  driver.findElement(By.xpath("//input[@type='submit']")).click();
 }
}

Above given webdriver code Is just for example. It Is just explaining the way of using try catch block to handle unexpected alert.
You can use such try catch block In that area where you are facing unexpected alerts very frequently.

Datepicker Using Selenium Webdriver

Many applications are using jQuery Date pickers for selecting date.So selecting date picker using selenium is a not a difficult task.

In this post, I will explain how can we select date from a Date picker using  selenium webdriver.
Please find the below sample code for the same.


Sample code:
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class HandleDatePicker {
  private WebDriver driver;
  private String baseUrl;


  @BeforeClass
  public void setUp() throws Exception {
 System.setProperty("webdriver.chrome.driver", "D:/Sudharsan/Official/Selenium  jars/chromedriver.exe");
 driver = new ChromeDriver();
    baseUrl = "http://jqueryui.com/datepicker/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
  }

  @Test
  public void testUntitled() throws Exception {
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.get(baseUrl + "/or");
    driver.findElement(By.cssSelector("span.form-hint")).click();
    driver.findElement(By.id("txtUsername")).clear();
    driver.findElement(By.id("txtUsername")).sendKeys("Admin");
    driver.findElement(By.id("txtPassword")).clear();
    driver.findElement(By.id("txtPassword")).sendKeys("admin");
    driver.findElement(By.id("btnLogin")).click();
    Thread.sleep(5000);
    driver.findElement(By.xpath("//b[contains(.,'Leave')]")).click();

    //selecting date with different formats you can give with any one following
    selectDate("23 Jun 1991");

    //selectDate("23-06-1991");
    //selectDate("23/Jun/1991");
    //selectDate("23/06/1991");
    //selectDate("23 Jun 1991");

  }

  //Reusable Method for Selecting Date
  public void selectDate(String format){
   driver.findElement(By.className("ui-datepicker-trigger")).click();

   //identifying format
   String date[] = null;
   if(format.contains("-")){
     date =format.split("-");
   }
   else if(format.contains("/")){
      date =format.split("/");
   }
   else if(format.contains(" ")){
      date =format.split(" ");
   }
   //Splitting data
   String day=date[0];
   String month=date[1];
   String year=date[2];

   //Selecting data based on format
   if(month.length()==2){
    //selecting month if you are giving input format as dd-mm-yyyy
    new Select(driver.findElement(By.className("ui-datepicker-month"))).selectByIndex(Integer.parseInt(month)-1);
   }
   else if(month.length()!=2){
  //selecting month if you are giving input format as dd-mmm-yyyy
    new Select(driver.findElement(By.className("ui-datepicker-month"))).selectByVisibleText(month);
   }
   //selecting year
   new Select(driver.findElement(By.xpath("//select[@class='ui-datepicker-year']"))).selectByVisibleText(year);

    //click on day
    driver.findElement(By.linkText(day)).click();
     }
}

Thursday, 27 October 2016

Decide What Test Cases to Automate

Decide What Test Cases to Automate

It is impossible to automate all testing, so it is important to determine what test cases should be automated first.
The benefit of automated testing is linked to how many times a given test can be repeated. Tests that are only performed a few times are better left for manual testing. Good test cases for automation are ones that are run frequently and require large amounts of data to perform the same action.
You can get the most benefit out of your automated testing efforts by automating:
  • Repetitive tests that run for multiple builds.
  • Tests that tend to cause human error.
  • Tests that require multiple data sets.
  • Frequently used functionality that introduces high risk conditions.
  • Tests that are impossible to perform manually.
  • Tests that run on several different hardware or software platforms and configurations.
  • Tests that take a lot of effort and time when manual testing.
Success in test automation requires careful planning and design work. Start out by creating an automation plan. This allows you to identify the initial set of tests to automate, and serve as a guide for future tests. First, you should define your goal for automated testing and determine which types of tests to automate. There are a few different types of testing, and each has its place in the testing process. For instance, unit testing is used to test a small part of the intended application. To test a certain piece of the application’s UI, you would use functional or GUI testing.
After determining your goal and which types of tests to automate, you should decide what actions your automated tests will perform. Don’t just create test steps that test various aspects of the application’s behavior at one time. Large, complex automated tests are difficult to edit and debug. It is best to divide your tests into several logical, smaller tests. It makes your test environment more coherent and manageable and allows you to share test code, test data and processes. You will get more opportunities to update your automated tests just by adding small tests that address new functionality. Test the functionality of your application as you add it, rather than waiting until the whole feature is implemented.
When creating tests, try to keep them small and focused on one objective. For example, separate tests for read-only versus read/write tests. This allows you to use these individual tests repeatedly without including them in every automated test.
Once you create several simple automated tests, you can group your tests into one, larger automated test. You can organize automated tests by the application’s functional area, major/minor division in the application, common functions or a base set of test data. If an automated test refers to other tests, you may need to create a test tree, where you can run tests in a specific order.