Skip to content

Commit

Permalink
Move building of locators to How enum
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Stewart <simon.m.stewart@gmail.com>
  • Loading branch information
valfirst authored and shs96c committed Sep 28, 2017
1 parent 40a78e4 commit 893b377
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
74 changes: 64 additions & 10 deletions java/client/src/org/openqa/selenium/support/How.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
64 changes: 64 additions & 0 deletions java/client/test/org/openqa/selenium/support/ui/HowTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 893b377

Please sign in to comment.