diff --git a/java/client/src/org/openqa/selenium/support/AbstractFindByBuilder.java b/java/client/src/org/openqa/selenium/support/AbstractFindByBuilder.java index c1756633e3e1f..f78618eeec5f2 100644 --- a/java/client/src/org/openqa/selenium/support/AbstractFindByBuilder.java +++ b/java/client/src/org/openqa/selenium/support/AbstractFindByBuilder.java @@ -76,43 +76,7 @@ protected By buildByFromShortFindBy(FindBy findBy) { } protected By buildByFromLongFindBy(FindBy findBy) { - How how = findBy.how(); - String using = findBy.using(); - - switch (how) { - case CLASS_NAME: - return By.className(using); - - case CSS: - return By.cssSelector(using); - - case ID: - case UNSET: - return By.id(using); - - case ID_OR_NAME: - return new ByIdOrName(using); - - case LINK_TEXT: - return By.linkText(using); - - case NAME: - return By.name(using); - - case PARTIAL_LINK_TEXT: - return By.partialLinkText(using); - - case TAG_NAME: - return By.tagName(using); - - case XPATH: - return By.xpath(using); - - default: - // Note that this shouldn't happen (eg, the above matches all - // possible values for the How enum) - throw new IllegalArgumentException("Cannot determine how to locate element "); - } + return findBy.how().buildBy(findBy.using()); } protected void assertValidFindBys(FindBys findBys) { diff --git a/java/client/src/org/openqa/selenium/support/How.java b/java/client/src/org/openqa/selenium/support/How.java index e79590037a1fa..83a2d7341cde0 100644 --- a/java/client/src/org/openqa/selenium/support/How.java +++ b/java/client/src/org/openqa/selenium/support/How.java @@ -17,15 +17,69 @@ package org.openqa.selenium.support; +import org.openqa.selenium.By; + public enum How { - CLASS_NAME, - CSS, - ID, - ID_OR_NAME, - LINK_TEXT, - NAME, - PARTIAL_LINK_TEXT, - TAG_NAME, - XPATH, - UNSET + CLASS_NAME { + @Override + public By buildBy(String value) { + return By.className(value); + } + }, + CSS { + @Override + public By buildBy(String value) { + return By.cssSelector(value); + } + }, + ID { + @Override + public By buildBy(String value) { + return By.id(value); + } + }, + ID_OR_NAME { + @Override + public By buildBy(String value) { + return new ByIdOrName(value); + } + }, + LINK_TEXT { + @Override + public By buildBy(String value) { + return By.linkText(value); + } + }, + NAME { + @Override + public By buildBy(String value) { + return By.name(value); + } + }, + PARTIAL_LINK_TEXT { + @Override + public By buildBy(String value) { + return By.partialLinkText(value); + } + }, + TAG_NAME { + @Override + public By buildBy(String value) { + return By.tagName(value); + } + }, + XPATH { + @Override + public By buildBy(String value) { + return By.xpath(value); + } + }, + UNSET { + @Override + public By buildBy(String value) { + return ID.buildBy(value); + } + }; + + public abstract By buildBy(String value); } diff --git a/java/client/test/org/openqa/selenium/support/ui/HowTest.java b/java/client/test/org/openqa/selenium/support/ui/HowTest.java new file mode 100644 index 0000000000000..09c03c6063fab --- /dev/null +++ b/java/client/test/org/openqa/selenium/support/ui/HowTest.java @@ -0,0 +1,64 @@ +package org.openqa.selenium.support.ui; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.support.ByIdOrName; +import org.openqa.selenium.support.How; + +public class HowTest { + + private static final String VALUE = "value"; + + @Test + public void testBuildByClassName(){ + assertEquals(By.className(VALUE).toString(), How.CLASS_NAME.buildBy(VALUE).toString()); + } + + @Test + public void testBuildByCss(){ + assertEquals(By.cssSelector(VALUE).toString(), How.CSS.buildBy(VALUE).toString()); + } + + @Test + public void testBuildById(){ + assertEquals(By.id(VALUE).toString(), How.ID.buildBy(VALUE).toString()); + } + + @Test + public void testBuildByIdOrName(){ + assertEquals(new ByIdOrName(VALUE).toString(), How.ID_OR_NAME.buildBy(VALUE).toString()); + } + + @Test + public void testBuildByLinkText(){ + assertEquals(By.linkText(VALUE).toString(), How.LINK_TEXT.buildBy(VALUE).toString()); + } + + @Test + public void testBuildByName(){ + assertEquals(By.name(VALUE).toString(), How.NAME.buildBy(VALUE).toString()); + } + + @Test + public void testBuildByPartialLinkText(){ + assertEquals(By.partialLinkText(VALUE).toString(), + How.PARTIAL_LINK_TEXT.buildBy(VALUE).toString()); + } + + @Test + public void testBuildByTagName(){ + assertEquals(By.tagName(VALUE).toString(), How.TAG_NAME.buildBy(VALUE).toString()); + } + + @Test + public void testBuildByXpath(){ + assertEquals(By.xpath(VALUE).toString(), How.XPATH.buildBy(VALUE).toString()); + } + + @Test + public void testBuildUnset(){ + assertEquals(By.id(VALUE).toString(), How.UNSET.buildBy(VALUE).toString()); + } +}