Showing posts with label manual testing jobs. Show all posts
Showing posts with label manual testing jobs. Show all posts

Thursday, 7 May 2015

Immediate windows popups handled by using Selenium Webdriver API



There are many such cases now a days, where any application (web site) displays multiple windows pop up, when you open a website for any purpose. Those are may be some kind of advertisements or may be, some information are showing on popup windows. We can handle those thing via using Windows Handlers in selenium webdriver API.
Step 1: Opening any website, we need to get the main window handle by using below syntex driver.getWindowHandle();
Window handle shall be in a form of lengthy alpha numeric data
Step 2: If we need to get all the window handles by using driver.getWindowHandles(); command
Step 3: We can compare all the window handles with the main Window handles and perform the operation the window whatever we need.
The below example explain how to handle multiple windows and close all the child windows which are not need. We need to compare the main window handle to all the other window handles and close them.
package com.suit;

import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class WindowhandlearExamples {
        WebDriver driver;

        @Test
        public void CloseAllWindowsExceptMainWindow_test() {
               driver = new FirefoxDriver();
               // It will open the Naukri.com website with multiple windows
               driver.get("http://www.naukri.com/");
              
               // To find the main window handle
               String getwindowTitle= getCurrentWindowTitle();
               String getmainWindow = getMainWindowHandle(driver);
               Assert.assertTrue(closeAllOtherWindows(mainWindow));
               Assert.assertTrue(grtwindowTitle.contains("Jobs - Recruitment"), "Main window title is not matching");
        }
              
        public String getMainWindowHandle(WebDriver driver) {
               return driver.getWindowHandle();
        }

        public String getCurrentWindowTitle() {
               String windowTitle = driver.getTitle();
               return windowTitle;
        }
       
        //To close all the other windows except the main window here we go.
        public static boolean closeAllOtherWindows(String openWindowHandle) {
               Set<String> allWindowHandles = driver.getWindowHandles();
               for (String currentWindowHandle : allWindowHandles) {
                       if (!currentWindowHandle.equals(openWindowHandle)) {
                               driver.switchTo().window(currentWindowHandle);
                               driver.close();
                       }
               }
              
               driver.switchTo().window(openWindowHandle);
               if (driver.getWindowHandles().size() == 1)
                       return true;
               else
                       return false;
        }
}

Selenium Web Driver API -CSS selector’s locators for with example



In our automation script if we do not find an option to choose Id or Name, you can go with to CSS locators as the best alternative.

CSS full form is "Cascading Style Sheets" and it is meant for to display HTML in structured and colorful styles are applied to webpage.
Selectors are define patterns that match against elements in a tree, and as such form one of several technologies that can be used to select nodes in an XML document. Visit to know more W3.Org Css selectors
  • CSS has more Advantage than Xpath
  • CSS is much faster and look like simpler than the Xpath.
  • In IE Xpath works very slow, however CSS works faster then compared to Xpath.
Syntax: 
tagName[attributename=attributeValue]
Example 1: input[id=username]
Example 2: input[name= username][type=text]
CSS selectors  there are two special characters this is meant for to play an important role to identify the element in DOM.
1. dot (.) Refers To Class.
Syntax:
css=input.submitbtn
For example the below is the html tag for a sign button
<button class="submit btn primary-btn flex-table-btn js-submit" type="submit" style="background-color: rgb(86, 153, 227);">
 Log in
 </button>
In the above html, if there are multiple classes used for the single button. How to work in such a situation?

Below are the examples to work with classes. If you notify below, we have combined multiple classes to work. As the class is not unique like ID, we may require to join two classes and get the accurate element.
Example 1: css=.primary-btn
Example 2: css=.btn.primary-btn
Example 3: css=.submit.primary-btn
 
 
The above example can be written like below in selenium web driver API 
 
WebElement ele1 = driver.findElement(By.cssSelector(".primary-btn"));
WebElement ele2 = driver.findElement(By.cssSelector("btn.primary-btn"));
WebElement ele3 = driver.findElement(By.cssSelector("btn.primary-btn"));
2. Hash (#) Refers to Id, id should always unique
ie. 
css=input[id=email]