Automation Using Selenium Webdriver

Friday, 28 October 2016

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.

How to pass two strings (Username and Password) one by one in the two different text fields (Username and Password) in webdriver (Java)

I am having two different Strings by name username and password. And i want to pass these string values one by one (ONLY one value from username and one value from password) into username and password text fields. I tried in two different ways but not sure where i am doing mistake. Below are my codes and O/P result. Expected: Two combinations (1st: @ and @, 2nd: test and test).
Expected O/P: username password username password
String[] username={"@", "test"};
String[] password= {"@", "test"};
1st method:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings {
public static void main(String[] args) {
String[] username={"@", "test"};
String[] password= {"@", "test"};
WebDriver d =new FirefoxDriver();
d.get("http://newtours.demoaut.com/mercurysignon.php");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for(String j:username){
d.findElement(By.name("userName")).clear();
d.findElement(By.name("userName")).sendKeys(j);
System.out.println("userName");
for(int i =0; i<password.length; i++){
d.findElement(By.name("password")).clear();
d.findElement(By.name("password")).sendKeys(password[i]);
System.out.println("password");
}
}
d.findElement(By.name("login")).click();
}
}
O/P: userName password password userName password password
2nd method which i tried:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TwoStrings {
public static void main(String[] args) {
String[] username={"@", "test"};
String[] password= {"@", "test"};
WebDriver d =new FirefoxDriver();
d.get("http://newtours.demoaut.com/mercurysignon.php");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for(String j:username){
for(String k:password){
d.findElement(By.name("userName")).clear();
d.findElement(By.name("userName")).sendKeys(j);
System.out.println("userName");
d.findElement(By.name("password")).clear();
d.findElement(By.name("password")).sendKeys(k);
System.out.println("password");
}
}
d.findElement(By.name("login")).click();
}
}
O/P: userName password userName password userName password userName password