September 2, 2016

What is Cucumber?

Cucumber Introduction:

Before we need to discuss about CUCUMBER, we've to know about BDD.

What is BDD?

Behavior-driven development (BDD) is a software development methodology in which an application is specified and designed by describing how its behavior should appear to an outside observer and is extension of Test Driven Development.
BDD is used to test the system rather than testing the particular piece of code.

Behavior Driven Development is extension of Test Driven Development and it is used to test the system rather than testing the particular piece of code.

What is CUCUMBER?

Cucumber is a tool based on Behavior Driven Development (BDD) framework which is used to write acceptance tests for web application. It allows automation of functional validation in easily readable and understandable format (like plain English) to Business Analysts, Developers, Testers, etc.

Cucumber feature files can serve as a good document for all. Initially Cucumber was implemented in Ruby and then extended to Java framework. Both the tools support native JUnit.

Here in this blog, I'm providing some basic information about the Cucumber with Selenium Combination and it supports all the languages supported by Selenium to develope the scripts.

Impotent Information:

Required Jars: These cucmber jar are used to create feature file, interact with the selenium server

  1. Cucumber-Core.jar
  2. Cucumber-html.jar
  3. Cucumber-java.jar
  4. Cucumber-junit.jar
  5. Cucumber-jvm-deps.jar
  6. gherkin.jar
  7. import cucumber.api.junit.Cucumber;

Links:

September 1, 2016

Synchronization Techniques

Why are waits required in selenium?

In web automation waits are required as certain elements get loaded on the page, so after triggering an event a page may get loaded successfully but some of its element may still not get loaded. This causes elementNotFound exception.

In such cases we will be using Thread.sleep() i.e. a static wait that will halt the test execution for some specified time and then perform the next step. As Thread.sleep() will wait for the specified time no matter if the elements gets visible before the specified amount of time. So, using the Thread.sleep() method is never advisable for UI automation.

To avoid this, selenium provides different types of waits , in most of the cases we will use Implicit, explicit waits and pageLoadTimeout.

Implicit Waits –

An implicit wait when used is set to the WebDriver instance and is applied to all the web elements. In implicit wait the webdriver polls the DOM to check the availability of the webElement and waits till the maximum time specified before throwing NoSuchElementException.

Syntax:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(<interger to represent the time>, TimeUnit.<UNIT>);
 - where UNIT might be SECONDS, MINUTES, HOURS, DAYS etc.,
 - TimeUnit is available from java.util.concurrent Package.

Example:
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

In the above example the value 10 specified in implicit wait method is the maximum time in seconds till which webDriver will wait before throwing NoSuchElementException while locating a webElement.

Explicit Waits –

Unlike implicit waits (which is applied on WebDriver instance without any condition), the explicit waits are applied to each and every webElement. In explicit wait certain conditions are defined for which the webDriver instance waits before locating webElements or performing actions on them. Some of the most common conditions specified in explicit waits are-
visibilityOfElementLocated, elementToBeClickable, presenceOfElementLocated etc.

Syntax:
WebDriverWait wait = new WebDriverWait(driver, <Time is Seocnds>);
wait.until(ExpectedConditions.<condition>(ElementLocator));

Example:
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(ElementLocator));

Here the webDriver instance will wait until the condition specified is met i.e. the visibility Of Element located by the Locator with the maximum wait time of 30 seconds after which if the condition is still not met than it will throw NoSuchElementException exception.


FluentWait Command :

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such asNoSuchElementExceptions when searching for an element on the page.

 Wait wait = new FluentWait(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
     .ignoring(NoSuchElementException.class); 

PageLoadTimeout Command:

Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
driver.manage().timeouts().pageLoadTimeout(100, SECONDS);

SetScriptTimeout Command :

Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.
driver.manage().timeouts().setScriptTimeout(100,SECONDS);

August 19, 2016

Selenium Web Driver

Selenium WebDriver is an interface and it is a web automation framework that allows you to execute your tests against different browsers.
We've the following browser support
  • Firefox
  • Internet Explorer
  • Safari
  • Google Chrome
  • Opera Browser
  • HtmlUntilDriver (No User Interface)
Using WebDriver we develop the test cases using various programming languages.
Following programming languages are supported by WebDriver
  • Java
  • .Net 
  • PHP
  • Python
  • Perl
  • Ruby
We've several methods in Selenium WebDriver, few of them are listed below
  1. get()
  2. getCurrentUrl();
  3. getTitle()
  4. findElements()
  5. findElement()
  6. getPageSource()
  7. close()
  8. quit()
  9. getWindowHandles()
  10. getWindowHandle()
  11. manage()
  12. navigate()
Let’s look into the detail for each of the above methods.
Method Name: - get()
Syntax: get(url)
Purpose: It will load a new web page in the current browser window. This is done using an http get operation, and the method will block until the load is complete.
Parameters: URL - The URL to load and it should be a fully qualified URL
Example:
@Test
Public void testGetMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
}
Method Name: getCurrentUrl()
Syntax: getCurrentUrl()
Purpose: Gets a string representing the current URL that the browser is opened.
Returns: The URL of the page currently loaded in the browser
Example:
@Test
Public void testGetCurrentURLMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentURL = driver. getCurrentUrl();
System.out.println(currentURL);
}
Method Name: getTitle()
Syntax: getTitle();
Purpose: Gets the title of the current web page.
Returns: The title of the current page, with leading and trailing white space stripped, or null if one is not already set
Example:
@Test
Public void testGetTitleMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String pageTitle = driver. getTitle ();
System.out.println(pageTitle);
}
Method Name: findElements()
Syntax: findElements(By byLocator);
Purpose: Find all elements within the current page using the given criteria.
Parameters: By - The locating technique (Like id, className, xPath,cssSelector, linkText etc.,)
Returns: A list of all WebElements, or an empty list if nothing matches
Example:
@Test
Public void testFindElementsMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
List<WebElement> listElements = driver. findElements(By byLocator);
}
Method Name: findElement ()
Syntax: WebElement findElement(By by);
Purpose: Find the first WebElement using the given criteria.
Parameters: By - The locating technique (Like id, className, xPath,cssSelector, linkText etc.,)
Returns: The first matching element on the current page
Throws: NoSuchElementException - it will return exception if no matching elements are found
Example:
@Test
Public void testFindElementMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
WebElement element = driver. findElement();
}
Method Name: getPageSource()
Syntax: getPageSource();
Purpose: Get the source of the currently loaded page. If the page has been modified after loading (for example, by JavaScript) there is no guarantee that the returned text is that of the modified page.
Returns: The source of the current page
Example:
@Test
Public void testGetPageSourceMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String pageSource = driver. getPageSource ();
System.out.println(pageSource);
}
Method Name: close()
Syntax: void close();
Purpose: Close the current window, if there are multiple windows, it will close the current window which is active and quits the browser if it's the last window opened currently.
Example:
@Test
Public void testDriverCloseMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
driver. close();
}
Method Name: quit()
Syntax: void quit();
Purpose: Quits this driver instance, closing every associated window which is opened.
Example:
@Test
Public void testDriverQuitMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
driver. quit();
}
Method Name: getWindowHandles()
Syntax: Set getWindowHandles();
Purpose: Return a set of window handles which can be used to iterate over all the open windows of this Webdriver instance by passing them to switchTo().window() method
Returns: A set of window handles which can be used to iterate over all the open windows.
Example:
@Test
Public void testGetWindowHandlesMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
List<String> pageHandles = driver. getWindowHandles ();
For(String pageHandle: pageHandles)
System.out.println(pageHandle);
}
Method Name: getWindowHandle()
Syntax: String getWindowHandle();
Purpose: To get the current window handle, this is used to navigate back to the parent window
Returns: Return a string which represent the current window handle
Example:
@Test
Public void testGetWindowHandleMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String pageHandle = driver. getWindowHandle();
System.out.println(pageHandle);
}
Method Name: switchTo
Syntax: driver.switchTo()
Purpose: To perform the next future commands on different frame, alert or window.
Returns: A Target Locator which can be used to switch or select a frame or window
Example:
@Test
Public void testSwitchToWindowMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentWindowName = driver.getWindowHandle();
driver. switchTo ().window(<window name>/<window id>);
System.out.println(pageTitle);

// To close the current window
Driver.close();

//To switch to parent window
Driver.switchTo.window(currentWindowName);
}

@Test
Public void testSwitchToAlertMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentWindowName = driver.getWindowHandle();
driver. switchTo ().alert();

//To Accept the alert
driver.accept();

//To dismiss the alert
driver. dismiss();

//To switch to parent window
Driver.switchTo.window(currentWindowName);
}

@Test
Public void testSwitchToFrameMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
String currentWindowName = driver.getWindowHandle();
driver. switchTo ().Frame(<FrameWebElement>/<Frame ID> / <Frame Name>);

//To switch to parent Frame
Driver.switchTo.defaultContent();}
Method Name: manage()
Syntax: driver.manage()
Purpose: To manage the driver (Maximize, minimize, implicitlyWait etc.)
Returns: Returns an instance of underlying implementation of Interface Options which could be EventFiringOptions / RemoteWebDriverOptions.
Example:
@Test
Public void testManageWindowMethod()
{
WebDriver driver=new FirefoxDriver();
driver.get(URL);
//To Maximize the window
driver. manage().window().maximize();

//To get Size of the window
Dimension d= driver. manage().window().getSize();

//To set Size of window
driver.manage().window().setSize(new Dimension(width, height));

//To set wait time to the window
driver.manage().timeouts().implicitlyWait(<integer value>, TimeUnit.SECONDS)
}
Method Name: navigate()
Syntax: WebDriver.Navigation navigate()
Purpose: An abstraction allowing the driver to access the browser's history and to navigate to a given URL.
Returns: A WebDriver.Navigation that allows the selection of what to do next
Example:
@Test
public void navigationToURLExample()
{
driver= new FirefoxDriver();
driver.get(URL);
System.out.println(driver.getTitle());
driver.navigate().to("http://www.google.com");
System.out.println(driver.getTitle());
}

@Test
public void navigationBackExample()
{
driver= new FirefoxDriver();
String URL="http://www.facebook.com";
driver.navigate().to(URL);
System.out.println(driver.getTitle());

driver.findElement(By.linkText("Forgot your password?")).click();
System.out.println(driver.getTitle());

driver.navigate().back();
System.out.println(driver.getTitle());
}

@Test
public void navigationForwardExample()
{
driver= new FirefoxDriver();
String URL="http://www.facebook.com";
driver.navigate().to(URL);
System.out.println(driver.getTitle());
driver.findElement(By.linkText("Forgot your password?")).click();
System.out.println(driver.getTitle());
driver.navigate().back();
System.out.println(driver.getTitle());
driver.navigate().forward();
System.out.println(driver.getTitle());
}

@Test
public void navigationRefreshExample()
{
driver= new FirefoxDriver();
String URL="http://www.facebook.com";
driver.navigate().to(URL);
driver.findElement(By.linkText("Forgot your password?")).click();
driver.findElement(By.id("identify_email")).sendKeys("sample@email.com");
driver.navigate().refresh();
}