Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Slider movement possible in Selenium? [duplicate]

Possible Duplicate:
How to move Horizontal Slider or Vertical Slider of jQuery using Selenium Webdriver

There are many examples of slider in internet like

http://jqueryui.com/demos/slider/

Is it possible to move the slider using Selenium?

like image 577
Nayan Avatar asked Aug 25 '12 14:08

Nayan


2 Answers

Working code-

WebDriver driver = new InternetExplorerDriver();
driver.get("http://jqueryui.com/demos/slider/");
//Identify WebElement
WebElement slider = driver.findElement(By.xpath("//div[@id='slider']/a"));

//Using Action Class
Actions move = new Actions(driver);
Action action = move.dragAndDropBy(slider, 30, 0).build();
action.perform();

driver.quit();

Source - https://gist.github.com/2497551

like image 73
some_other_guy Avatar answered Oct 17 '22 17:10

some_other_guy


Have you ever tried the Action interface?

Especially the point "Generating Action chains" should help you

/**
 * Moves a jQuery slider to percental position, don't care about directions
 * @param slider to move
 * @param percent to set the slider
 */
public void moveSliderToPercent(WebElement slider, int percent){

    Actions builder = new Actions(this.driver);

    Action dragAndDrop;

    int height = slider.getSize().getHeight();
    int width = slider.getSize().getWidth();


    if(width>height){
        //highly likely a horizontal slider
        dragAndDrop = builder.clickAndHold(slider).moveByOffset(-(width/2),0).
                       moveByOffset((int)((width/100)*percent),0).
                       release().build();
    }else{
        //highly likely a vertical slider
        dragAndDrop = builder.clickAndHold(slider).moveByOffset(0, -(height/2)).
                       moveByOffset(0,(int)((height/100)*percent)).
                       release().build();
    }


    dragAndDrop.perform();

}
like image 2
Franz Ebner Avatar answered Oct 17 '22 15:10

Franz Ebner