Thursday, June 11, 2015

 
















                           In automation testing , date selection is an important area you can never miss. Every date widget will have an input field where text input are accepted. So most of the times as a workaround, we tries to code accordingly to enter the date manually. But it's very important to automate the selection of date from the widget because there may be scenarios in which date widget may not work properly. Here, I will show you a piece of code to automate the date selection from the widget.

public void selectdob(String day, String month, String year){
            //To popup date widget
            new WebDriverWait(BrowserConf.driver, 10).until(
                    ExpectedConditions.presenceOfElementLocated(By.xpath(obj
                            .getProperty("dobbutton")))).click();
            //To select the year from dropdown
            Select yeardrop= new Select(new WebDriverWait(BrowserConf.driver, 10).until(
                    ExpectedConditions.presenceOfElementLocated(By.className(obj
                            .getProperty("yearclass")))));
            yeardrop.selectByVisibleText(year);
            //To select the month from dropdown
            Select monthdrop= new Select(new WebDriverWait(BrowserConf.driver, 10).until(
                    ExpectedConditions.presenceOfElementLocated(By.className(obj
                            .getProperty("monthclass")))));
            monthdrop.selectByVisibleText(month);
            //To select the date from the table
            WebElement table_element = BrowserConf.driver.findElement(By.className("ui-datepicker-calendar"));
            List<WebElement> tr_collection=table_element.findElements(By.xpath("id('ui-datepicker-div')/table/tbody/tr"));
            //System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
            int row_num,col_num;
            row_num=1;
            for(WebElement trElement : tr_collection)
            {
                List<WebElement> td_collection=trElement.findElements(By.xpath("td"));
                //System.out.println("NUMBER OF COLUMNS="+td_collection.size());
                col_num=1;
                for(WebElement tdElement : td_collection)
                {
                    if(tdElement.getText().equals(day)){
                        tdElement.click();
                    }
                    col_num++;
                }
                row_num++;
            }
        }

And you can now call the above function as:

selectdob("24","Jul","2012");

 First two statements are used for selecting month and year from the dropdown in the widget of my application. You may need to have slight change if the widget is different. Last part for selecting the date will be the same.



Related Posts:

0 comments:

Post a Comment

>

Selenium useful links

Powered by Blogger.

Featured Post

Benefits of having a user Interface for a Selenium Automation Suite/Regression Suite

Once you are able to work independently on Selenium scripting, another important task is how to ease your automation test execution. There a...

Video

Popular Posts

Our Facebook Page