Sunday, 18 May 2014

Actions In Selenium using Selenium WebDriver

 

There are different kind of actions can be perform in selenium webdriver. So every action needs to build and then it will be ready to perform. The actions will be responsible for every action performed in browser. In most of the cases we will use .Build() and .perform() after initialization of every new action.

So following are the actions that can be perform:


1. To build multiple actions in a order and then return the result of that particular action:

new Actions(driver).Build();

2. To execute currently built action and no return:

new Actions(driver).Perform();

3. To perform particular action on the browser to be perform:

new Actions(driver).Build().Perform(); 

It is better to specify by locator using findelement.

4. To click the mouse on last know mouse co-ordinates and return the actions:

new Actions(driver).Click();

5. To click the mouse on specified element and return the actions:

new Actions(driver).Click(draggable);

6. To click and hold the mouse button on last known mouse co-ordinates and return the actions:

new Actions(driver).ClickAndHold();

7. To drag-and -drop from one element to another and that return the actions:

new Actions().DragAndDrop(draggable, droppable);

8. To drag-and-drop to specified offset on one element and that returns the actions:

new Actions(driver).DragAndDropToOffset(draggable, 25, 35);

9. To move the mouse over specified element and that return the actions:

new Actions(driver).MoveToElement(draggable);

10. To move the mouse to the specified offset of the top left corner of the specified element and that returns the actions:

new Actions(driver).MovetoElement(draggable, 25, 35);

11. To release the mouse button at the last known mouse co-oridinates and that return the actions:

new Actions(driver).Release();

12 To release the mouse button on specified element and that returns the actions:

new Actions(driver).Release(draggable);

13. To send the multiple keystrokes in specific order to the browser and that return the actions:

new Actions(driver). SendKeys(Keys.Alt) 

In this above syntax key parameter is string.

14. To send sequence of key strokes to the specified element in the browser and that return the actions:

new Actions(driver) .SendKeys(draggable, Keys.Alt);

In this above syntax key parameter as string.

15. To send a modifier key down message to the browser and that return the actions:

new Actions(driver).KeyDown(Keys.Alt)

16. To send a modifier key down message to the specified element in the browser and that return the actions:

 new Actions(driver).KeyDown(draggable, Key.Alt);

17. To send a modifier key up message to the browser and that return the actions:

new Actions(driver).KeyUp(Keys.Alt)

18. To send a modifier key up message to the specified element in the browser and that return the actions:

 new Actions(driver).KeyUp(draggable, Key.Alt);  

 

Now we will see how different actions we can use in selenium.

 

public class demoActions {

        WebDriver driver;

        @Before

        public void setup() {

            driver = new FirefoxDriver();

            driver.get("http://newtours.demoaut.com/");

            driver.manage().window().maximize();

        }

   /* @After

    public void tearDown(){

        wd.quit();

    }*/

        @Test

        public void shouldDoUnitTesting() {

            WebElement uName = driver.findElement(By.name("userName"));

            WebElement pwd = driver.findElement(By.name("password"));

            WebElement login = driver.findElement(By.name("login"));

            Actions actions = new Actions(driver);

            Action build = actions.moveToElement(uName)

                    .click(uName)

                    .keyDown(Keys.SHIFT)

                    .sendKeys("s")

                    .keyUp(Keys.SHIFT)

                    .sendKeys("oftedge100")

                    .keyDown(Keys.CONTROL)

                    .sendKeys("a" +"c")

                    .keyUp(Keys.CONTROL)

                    .click(pwd)

                    .keyDown(Keys.CONTROL)

                    .sendKeys("v")

                    .keyUp(Keys.CONTROL)

                    .click(login)

                    .build();

                    build.perform();

        }

    }

Thanks to Savita Shinde....

 

Saturday, 17 May 2014

How to read data from excel file using Selenium WebDriver

Before we dive into the implementation in selenium, first we have look at the data source we are going to use. As usual I would prefer excel is best option to read and write data using selenium WebDriver. Many of the folk and depend on the requirement use sql server or any database but that little different way to implementation of reading and writting data from both.

There are lots of different approaches possible, and I am aware that the solution presented here can possible be enhanced further and extended as well.





Above is the data source from where data is being used for data driven using Selenium WebDriver.

For this purpose, I use the Apache POI library, which allows you to read, create and edit Microsoft Office-documents using Java. The library, as well as its JavaDoc,can be found at http://poi.apache.org. The classes and methods we are going to use to read data from our Excel sheet are located in the org.apache.poi.hssf.usermodelpackage.


Code:

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by Gaurav on 5/26/2014.
 */
public class demoReadFromExcel {


        public static void main(String[] args) {
            try {
                FileInputStream file = new FileInputStream(new File("D:\\data1.xls"));
                HSSFWorkbook workbook = new HSSFWorkbook(file);
                HSSFSheet sheet1 = workbook.getSheet("sheet1");
                String heading = sheet1.getRow(0).getCell(0).getStringCellValue();
                String searchText1 = sheet1.getRow(1).getCell(0).getStringCellValue();
                String searchText2 = sheet1.getRow(2).getCell(0).getStringCellValue();
                String searchText3 = sheet1.getRow(3).getCell(0).getStringCellValue();

                String heading1 = sheet1.getRow(0).getCell(1).getStringCellValue();
                String searchText4 = sheet1.getRow(1).getCell(1).getStringCellValue();
                String searchText5 = sheet1.getRow(2).getCell(1).getStringCellValue();
                String searchText6 = sheet1.getRow(3).getCell(1).getStringCellValue();


                String heading2 = sheet1.getRow(0).getCell(2).getStringCellValue();
                String searchText7 = sheet1.getRow(1).getCell(2).getStringCellValue();
                String searchText8 = sheet1.getRow(2).getCell(2).getStringCellValue();
                String searchText9 = sheet1.getRow(3).getCell(2).getStringCellValue();


                System.out.println("Heading is:" +heading+ " " +heading1+ " " +heading2);
                System.out.println("Search Text 1 is:" +searchText1+ " " +searchText4+ " " +searchText7);
                System.out.println("Search Text 2 is:" +searchText2+ " " +searchText5+ " " +searchText8);
                System.out.println("Search Text 3 is:" +searchText3+ " " +searchText6+ " " +searchText9);

                file.close();
            }
            catch (FileNotFoundException fnfe) {
                fnfe.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

Wednesday, 14 May 2014

Implicit Wait Selenium Webdriver

How to use implicit wait in selenium webdriver and why 

 

As you knows sometimes, some elements takes some time to appear on page when browser is loading the page. In this case, sometime your webdriver test will fail if you have not applied Implicit wait in your test case. If implicit wait is applied in your test case then webdriver will wait for specified amount of time if targeted element not appears on page. As you know, we can Set default timeout which is same as implicit wait in webdriver.


If you write implicit wait statement in you webdriver script then it will be applied automatically to all elements of your test case. I am suggesting you to use Implicit wait in your all test script of software web application with 10 to 15 seconds. In webdriver, Implicit wait statement is as bellow.

How To Write Implicit Wait In WebDriver

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 

Above statement will tell webdriver to wait for 15 seconds if targeted element not found/not appears on page. Le we look at simple exemple to understand implicit wait better.

 

public class demoImplicitWait {
  FirefoxDriver driver;

@Before
public void setUp()
{
  driver = new FirefoxDriver();
  driver.get("http://jsbin.com/usidix/1");


 @Test
       public void shoudImplicitWait(){
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
       String text = driver.switchTo().alert().getText();
       driver.switchTo().alert().accept();
       System.out.println(text);
    }
}

   

How to use explicit wait in selenium webdriver and why

 

Explicit waits are intelligent waits that are confined to a particular web element. Using explicit waits you are basically telling WebDriver at the max it is to wait for X units of time before it gives up.

 

In explicit wait you can write custom code for a particular element to wait for particular time of period before executing next steps in your test. This provide you better option than implicit wait. Webdriver provide “WebDriverWait”, “ExpectedCondition” classes to implement this.

 

This above classes provide set of predefined conditions to wait for the particular element to load. Following are few conditions mostly used in the Expected condition class.

  • alertIsPresent() : Alert is present

  • elementSelectionStateToBe: an element state is selection.

  • elementToBeClickable: an element is present and clickable.

  • elementToBeSelected: element is selected

  • frameToBeAvailableAndSwitchToIt: frame is available and frame selected.

  • invisibilityOfElementLocated: an element is invisible

  • presenceOfAllElementsLocatedBy: present element located by.

  • textToBePresentInElement: text present on particular an element

  • textToBePresentInElementValue: and element value present for a particular element.

  • visibilityOf: an element visible.

  • titleContains: title contains


public class demoExplicitWait {
  public WebDriver driver;
  String baseUrl;
 
 
    @Test
  public void testUntitled() throws Exception {
    driver = new FirefoxDriver();
    driver.get("http://www.wikipedia.org/");
   
    //explicit wait for search field
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("searchInput")));
    driver.findElement(By.id("searchInput")).clear();     
    driver.findElement(By.id("searchInput")).sendKeys("India");
    driver.findElement(By.className("formBtn")).click();
 
  }
  @AfterMethod
  public void tearDown() throws Exception {
    driver.quit();   
  } 
} 
_____________________________________________

Select Language