Pages

Thursday, 15 December 2011

TO CHECK ALL CHECKBOXES IN A WEBPAGE


Here is the code to check the all checkboxes in a webpage....


package qtt.selenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;

public class TestCheckbox {
DefaultSelenium selenium;
SeleniumServer ss;

    @Before
    public void setUp()throws Exception{
        ss= new SeleniumServer();
        ss.start();
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://browsershots.org/");
        selenium.start();
        selenium.open("/");
    }

    @Test
    public void selectAllCheckboxes() throws InterruptedException{
        selenium.open("http://browsershots.org/");
        Thread.sleep(10000);
        int totalCheckboxes = selenium.getXpathCount("//input[@type='checkbox']").intValue();
        for (int i = 1; i < totalCheckboxes; i++) {

            if(!selenium.isChecked("//input[@type='checkbox'][" + i + "]"))
            {
                selenium.click("//input[@type='checkbox'][" + i + "]");
                Thread.sleep(5000);

            }
        }

    }
@After
public void teardown()throws Exception
{
    selenium.stop();
    ss.stop();
}

}


1 comment: