Skip to content

seleniumQuery Plugin Support .as() function

Antônio C. Jr edited this page Feb 18, 2015 · 2 revisions

seleniumQuery supports plugins through the .as() function. Here's an example:

import io.github.seleniumquery.SeleniumQueryObject;
import io.github.seleniumquery.functions.as.SeleniumQueryPlugin;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static infrastructure.IntegrationTestUtils.classNameToTestFileUrl;
import static io.github.seleniumquery.SeleniumQuery.$;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class SeleniumQueryPluginTest {

    @Before
    public void setUp() {
        $.driver().useHtmlUnit();
        $.url("http://www.google.com");
    }

    @After
    public void tearDown()  {
        $.quit();
    }

    @Test
    public void as_SIZER_plugin_example() {
        int theSize = $("div").as(Sizer.SIZER).gimmeTheSize();

        assertThat(theSize, is(9999));
        /* I don't know how many divs there are there, thus the '9999'
           just check and change this for the test to pass */
    }

}

// the plugin class
class Sizer {
    public static final SeleniumQueryPlugin<Sizer> SIZER =
        new SeleniumQueryPlugin<Sizer>() {
            @Override
            public Sizer as(SeleniumQueryObject seleniumQueryObject) {
                return new Sizer(seleniumQueryObject);
            }
    };

    private SeleniumQueryObject seleniumQueryObject;
    public Sizer(SeleniumQueryObject seleniumQueryObject) {
        this.seleniumQueryObject = seleniumQueryObject;
    }

    public int gimmeTheSize() {
        return this.seleniumQueryObject.get().size();
    }

}