Best Selenium code snippet using org.openqa.selenium.support.AbstractFindByBuilder.assertValidFindBy
Source:CustomFindByBuilder.java
...9import java.util.Set;10public abstract class CustomFindByBuilder extends AbstractFindByBuilder {11 public abstract By buildIt(Object annotation, Field field);12 protected By buildByFromFindBy(Find findBy) {13 assertValidFindBy(findBy);14 return buildByFromShortFindBy(findBy);15 }16 protected By buildByFromShortFindBy(Find findBy) {17 if (!"".equals(findBy.className())) {18 return By.className(findBy.className());19 }20 if (!"".equals(findBy.css())) {21 return By.cssSelector(findBy.css());22 }23 if (!"".equals(findBy.id())) {24 return By.id(findBy.id());25 }26 if (!"".equals(findBy.linkText())) {27 return By.linkText(findBy.linkText());28 }29 if (!"".equals(findBy.name())) {30 return By.name(findBy.name());31 }32 if (!"".equals(findBy.partialLinkText())) {33 return By.partialLinkText(findBy.partialLinkText());34 }35 if (!"".equals(findBy.attribute())) {36 return By.cssSelector("[" + findBy.attribute() + "=\"" + findBy.value() + "\"]");37 }38 if (!"".equals(findBy.value())) {39 return By.cssSelector("[value=\"" + findBy.value() + "\"]");40 }41 if (!"".equals(findBy.xpath())) {42 return By.xpath(findBy.xpath());43 }44 if (!"".equals(findBy.xpathText())) {45 String xpath = findBy.isDescendant() ? "descendant::" : "//";46 xpath += !("".equals(findBy.tagName())) ? findBy.tagName() : "*";47 xpath += findBy.checkContains() ? ("[contains(text(), \"" + findBy.xpathText() + "\")]")48 : ("[text()=\"" + findBy.xpathText() + "\"]");49 return By.xpath(xpath);50 }51 if (!"".equals(findBy.xpathString())) {52 String xpath = findBy.isDescendant() ? "descendant::" : "//";53 xpath += !("".equals(findBy.tagName())) ? findBy.tagName() : "*";54 xpath += findBy.checkContains() ? ("[contains(string(), \"" + findBy.xpathString() + "\")]")55 : ("[string()=\"" + findBy.xpathText() + "\"]");56 return By.xpath(xpath);57 }58 if (!"".equals(findBy.tagName())) {59 return By.tagName(findBy.tagName());60 }61 // Fall through62 return null;63 }64 protected void assertValidFindBys(Finds findBys) {65 for (Find findBy : findBys.value()) {66 assertValidFindBy(findBy);67 }68 }69 protected void assertValidFindBy(Find findBy) {70 if (findBy.checkContains()) {71 if (findBy.xpathText().equals("") && findBy.xpathString().equals("")) {72 throw new IllegalArgumentException(73 "If you set 'checkContains' to true, you must also set 'xpathText' or 'xpathString'");74 }75 }76 if (findBy.isDescendant()) {77 if (findBy.xpathText().equals("") && findBy.xpathString().equals("")) {78 throw new IllegalArgumentException(79 "If you set 'isDescendant' to true, you must also set 'xpathText' or 'xpathString'");80 }81 }82 if (!findBy.attribute().equals("") && findBy.value().equals("")) {83 throw new IllegalArgumentException("If you set 'attribute' you must also set 'value'.");84 }85 Set<String> finders = new HashSet<>();86 if (!"".equals(findBy.className())) finders.add("class name:" + findBy.className());87 if (!"".equals(findBy.css())) finders.add("css:" + findBy.css());88 if (!"".equals(findBy.id())) finders.add("id: " + findBy.id());89 if (!"".equals(findBy.linkText())) finders.add("link text: " + findBy.linkText());90 if (!"".equals(findBy.name())) finders.add("name: " + findBy.name());91 if (!"".equals(findBy.partialLinkText()))92 finders.add("partial link text: " + findBy.partialLinkText());93 if (!"".equals(findBy.tagName())) finders.add("tag name: " + findBy.tagName());94 if (!"".equals(findBy.xpath())) finders.add("xpath: " + findBy.xpath());95 // A zero count is okay: it means to look by name or id.96 if (finders.size() > 1) {97 throw new IllegalArgumentException(98 String.format("You must specify at most one location strategy. Number found: %d (%s)",99 finders.size(), finders.toString()));100 }101 }102 protected void assertValidFindAll(FindAll findBys) {103 for (Find findBy : findBys.value()) {104 assertValidFindBy(findBy);105 }106 }107}...
Source:AbstractFindByBuilder.java
...21import java.util.Set;22public abstract class AbstractFindByBuilder {23 public abstract By buildIt(Object annotation, Field field);24 protected By buildByFromFindBy(FindBy findBy) {25 assertValidFindBy(findBy);26 By ans = buildByFromShortFindBy(findBy);27 if (ans == null) {28 ans = buildByFromLongFindBy(findBy);29 }30 return ans;31 }32 protected By buildByFromShortFindBy(FindBy findBy) {33 if (!"".equals(findBy.className())) {34 return By.className(findBy.className());35 }36 if (!"".equals(findBy.css())) {37 return By.cssSelector(findBy.css());38 }39 if (!"".equals(findBy.id())) {40 return By.id(findBy.id());41 }42 if (!"".equals(findBy.linkText())) {43 return By.linkText(findBy.linkText());44 }45 if (!"".equals(findBy.name())) {46 return By.name(findBy.name());47 }48 if (!"".equals(findBy.partialLinkText())) {49 return By.partialLinkText(findBy.partialLinkText());50 }51 if (!"".equals(findBy.tagName())) {52 return By.tagName(findBy.tagName());53 }54 if (!"".equals(findBy.xpath())) {55 return By.xpath(findBy.xpath());56 }57 // Fall through58 return null;59 }60 protected By buildByFromLongFindBy(FindBy findBy) {61 return findBy.how().buildBy(findBy.using());62 }63 protected void assertValidFindBys(FindBys findBys) {64 for (FindBy findBy : findBys.value()) {65 assertValidFindBy(findBy);66 }67 }68 protected void assertValidFindBy(FindBy findBy) {69 if (findBy.how() != null) {70 if (findBy.using() == null) {71 throw new IllegalArgumentException(72 "If you set the 'how' property, you must also set 'using'");73 }74 }75 Set<String> finders = new HashSet<>();76 if (!"".equals(findBy.using())) finders.add("how: " + findBy.using());77 if (!"".equals(findBy.className())) finders.add("class name:" + findBy.className());78 if (!"".equals(findBy.css())) finders.add("css:" + findBy.css());79 if (!"".equals(findBy.id())) finders.add("id: " + findBy.id());80 if (!"".equals(findBy.linkText())) finders.add("link text: " + findBy.linkText());81 if (!"".equals(findBy.name())) finders.add("name: " + findBy.name());82 if (!"".equals(findBy.partialLinkText()))83 finders.add("partial link text: " + findBy.partialLinkText());84 if (!"".equals(findBy.tagName())) finders.add("tag name: " + findBy.tagName());85 if (!"".equals(findBy.xpath())) finders.add("xpath: " + findBy.xpath());86 // A zero count is okay: it means to look by name or id.87 if (finders.size() > 1) {88 throw new IllegalArgumentException(89 String.format("You must specify at most one location strategy. Number found: %d (%s)",90 finders.size(), finders.toString()));91 }92 }93 protected void assertValidFindAll(FindAll findBys) {94 for (FindBy findBy : findBys.value()) {95 assertValidFindBy(findBy);96 }97 }98}...
Source:FindByBuilder.java
...25 return null;26 }27 private By getSimpleBy(FindBy findBy) {28 return ofNullable(findBy).map(findSimple -> {29 assertValidFindBy(findSimple);30 return buildByFromFindBy(findSimple);31 }).orElse(null);32 }33 private By getChainedBy(FindBys findBys) {34 return ofNullable(findBys)35 .map(findByChained -> {36 assertValidFindBys(findByChained);37 FindBy[] findByArray = findByChained.value();38 By[] byArray = new By[findByArray.length];39 for (int i = 0; i < findByArray.length; i++) {40 byArray[i] = buildByFromFindBy(findByArray[i]);41 }42 return new ByChained(byArray);43 }).orElse(null);44 }45 private By getByAll(FindAll findAll) {46 return ofNullable(findAll).map(findByAll -> {47 assertValidFindAll(findByAll);48 FindBy[] findByArray = findByAll.value();49 By[] byArray = new By[findByArray.length];50 for (int i = 0; i < findByArray.length; i++) {...
Source:interfacce_FindBy.java
...66 String xpath() default "";67 public static class FindByBuilder extends AbstractFindByBuilder {68 public By buildIt(Object annotation, Field field) {69 FindBy findBy = (FindBy) annotation;70 assertValidFindBy(findBy);71 By ans = buildByFromShortFindBy(findBy);72 if (ans == null) {73 ans = buildByFromLongFindBy(findBy);74 }75 return ans;76 }77 }78}...
Source:FindBy.java
...65 String xpath() default "";66 public static class FindByBuilder extends AbstractFindByBuilder {67 public By buildIt(Object annotation, Field field) {68 FindBy findBy = (FindBy) annotation;69 assertValidFindBy(findBy);70 By ans = buildByFromShortFindBy(findBy);71 if (ans == null) {72 ans = buildByFromLongFindBy(findBy);73 }74 return ans;75 }76 }77}...
Source:PageAwareFindBy.java
...40 class FindByBuilder extends AbstractFindByBuilder {41 public By buildIt(Object annotation, Field field) {42 PageAwareFindBy pageAwareFindBy = (PageAwareFindBy) annotation;43 FindBy findBy = pageAwareFindBy.findBy();44 assertValidFindBy(findBy);45 By ans = buildByFromShortFindBy(findBy);46 if (ans == null) {47 ans = buildByFromLongFindBy(findBy);48 }49 String actualPageName = pageAwareFindBy.page();50 if (DEFAULT_PAGE_NAME.equals(actualPageName)) {51 actualPageName = field.getDeclaringClass().getSimpleName();52 }53 return PageAwareBy.by(actualPageName, ans);54 }55 }56}...
Source:FindElementBy.java
...32 33 @Override34 public By buildIt(Object annotation, Field field) {35 FindElementBy findElementBy = (FindElementBy) annotation;36 assertValidFindBy(findElementBy);37 By ans = buildByFromFindElementBy(findElementBy);38 return ans;39 }40 41 private By buildByFromFindElementBy(FindElementBy findBy) {42 if (!"".equals(findBy.css())) {43 return new FindByCssSelector(findBy.css(), SelectorType.CSS_SELECTOR.toString());44 }45 if (!"".equals(findBy.xpath())) {46 return new FindByXPath(findBy.xpath(), SelectorType.XPATH.toString());47 }48 49 return null;50 }51 52 private void assertValidFindBy(FindElementBy findBy) {53 Set<String> finders = new HashSet<>();54 if (!"".equals(findBy.css())) finders.add("css:" + findBy.css());55 if (!"".equals(findBy.xpath())) finders.add("xpath: " + findBy.xpath());56 // A zero count is okay: it means to look by name or id.57 if (finders.size() > 1) {58 throw new IllegalArgumentException(59 String.format("You must specify at most one location strategy. Number found: %d (%s)",60 finders.size(), finders.toString()));61 }62 }63 }64}...
assertValidFindBy
Using AI Code Generation
1import org.openqa.selenium.By;2import org.openqa.selenium.WebElement;3import org.openqa.selenium.support.AbstractFindByBuilder;4import org.openqa.selenium.support.FindBy;5import org.openqa.selenium.support.How;6import org.openqa.selenium.support.pagefactory.AbstractAnnotations;7import org.openqa.selenium.support.pagefactory.Annotations;8import org.openqa.selenium.support.pagefactory.ElementLocator;9import org.openqa.selenium.support.pagefactory.FieldDecorator;10import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler;11import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandler;12import org.openqa.selenium.support.pagefactory.internal.LocatingElementListIterator;13import org.openqa.selenium.support.ui.Clock;14import org.openqa.selenium.support.ui.DefaultClock;15import org.openqa.selenium.support.ui.SlowLoadableComponent;16import org.openqa.selenium.support.ui.SystemClock;17import org.openqa.selenium.support.ui.WebDriverWait;18import org.testng.annotations.Test;19import java.lang.reflect.Field;20import java.lang.reflect.InvocationHandler;21import java.lang.reflect.InvocationTargetException;22import java.lang.reflect.Method;23import java.lang.reflect.Proxy;24import java.util.List;25import java.util.concurrent.TimeUnit;26import static org.testng.Assert.assertEquals;27import static org.testng.Assert.assertNotNull;28public class CustomElementLocator extends SlowLoadableComponent<CustomElementLocator> implements FieldDecorator {29 private final WebDriver driver;30 private final long timeOutInSeconds;31 private final long sleepInMillis;32 private final Clock clock;33 private final Class<?> pageClassToProxy;34 private final By rootElement;35 private final boolean ignoreStaleElements;36 public CustomElementLocator(WebDriver driver, Class<?> pageClassToProxy, By rootElement) {37 this(driver, new SystemClock(), 10, 500, pageClassToProxy, rootElement, false);38 }39 public CustomElementLocator(WebDriver driver, long timeOutInSeconds, Class<?> pageClassToProxy, By rootElement) {40 this(driver, new SystemClock(), timeOutInSeconds, 500, pageClassToProxy, rootElement, false);41 }42 public CustomElementLocator(WebDriver driver, Clock clock, long timeOutInSeconds, long sleepInMillis,43 Class<?> pageClassToProxy, By rootElement, boolean ignoreStaleElements) {44 this.driver = driver;45 this.clock = clock;46 this.timeOutInSeconds = timeOutInSeconds;47 this.sleepInMillis = sleepInMillis;48 this.pageClassToProxy = pageClassToProxy;49 this.rootElement = rootElement;
assertValidFindBy
Using AI Code Generation
1import org.openqa.selenium.support.AbstractFindByBuilder;2import org.openqa.selenium.support.FindBy;3import org.openqa.selenium.support.pagefactory.Annotations;4import org.testng.annotations.Test;5import java.lang.reflect.Field;6public class TestClass {7 public void test() throws NoSuchFieldException {8 Field field = TestClass.class.getDeclaredField("field");9 Annotations annotations = new Annotations(field);10 AbstractFindByBuilder builder = new AbstractFindByBuilder() {11 protected String assertValidFindBy(FindBy findBy) {12 return findBy.how().toString();13 }14 };15 System.out.println(builder.buildIt(annotations));16 }17 @FindBy(how = FindBy.How.XPATH, using = "some xpath")18 private String field;19}
assertValidFindBy
Using AI Code Generation
1 public static void assertValidFindBy(FindBy findBy) {2 if (findBy == null) {3 throw new IllegalArgumentException("Cannot use a null FindBy");4 }5 if (findBy.className().length() > 0 && findBy.css().length() > 0) {6 throw new IllegalArgumentException("Cannot use both className and css to locate an element");7 }8 if (findBy.className().length() > 0 && findBy.id().length() > 0) {9 throw new IllegalArgumentException("Cannot use both className and id to locate an element");10 }11 if (findBy.className().length() > 0 && findBy.linkText().length() > 0) {12 throw new IllegalArgumentException("Cannot use both className and linkText to locate an element");13 }14 if (findBy.className().length() > 0 && findBy.name().length() > 0) {15 throw new IllegalArgumentException("Cannot use both className and name to locate an element");16 }17 if (findBy.className().length() > 0 && findBy.partialLinkText().length() > 0) {18 throw new IllegalArgumentException("Cannot use both className and partialLinkText to locate an element");19 }20 if (findBy.className().length() > 0 && findBy.tagName().length() > 0) {21 throw new IllegalArgumentException("Cannot use both className and tagName to locate an element");22 }23 if (findBy.className().length() > 0 && findBy.xpath().length() > 0) {24 throw new IllegalArgumentException("Cannot use both className and xpath to locate an element");25 }26 if (findBy.css().length() > 0 && findBy.id().length() > 0) {27 throw new IllegalArgumentException("Cannot use both css and id to locate an element");28 }29 if (findBy.css().length() > 0 && findBy.linkText().length() > 0) {30 throw new IllegalArgumentException("Cannot use both css and linkText to locate an element");31 }32 if (findBy.css().length() > 0 && findBy.name().length() > 0) {33 throw new IllegalArgumentException("Cannot use both css and name to locate an element");34 }35 if (findBy.css().length() > 0 && findBy.partialLinkText().length() > 0) {36 throw new IllegalArgumentException("Cannot use both css and partialLinkText to locate an element");37 }38 if (findBy.css().length() > 0
assertValidFindBy
Using AI Code Generation
1public class FindByBuilder {2 public static void main(String[] args) {3 By by = By.id("foo");4 assertValidFindBy(by);5 }6}
assertValidFindBy
Using AI Code Generation
1package org.openqa.selenium.support;2import org.openqa.selenium.By;3import java.lang.reflect.Field;4public class AbstractFindByBuilder {5 public void assertValidFindBy(FindBy findBy) {6 if (findBy == null) {7 throw new IllegalArgumentException("Cannot find elements when @FindBy is null");8 }9 if (findBy.className().length() > 0) {10 assertValidBy(findBy.className(), By.class);11 }12 if (findBy.css().length() > 0) {13 assertValidBy(findBy.css(), By.class);14 }15 if (findBy.id().length() > 0) {16 assertValidBy(findBy.id(), By.class);17 }18 if (findBy.linkText().length() > 0) {19 assertValidBy(findBy.linkText(), By.class);20 }21 if (findBy.name().length() > 0) {22 assertValidBy(findBy.name(), By.class);23 }24 if (findBy.partialLinkText().length() > 0) {25 assertValidBy(findBy.partialLinkText(), By.class);26 }27 if (findBy.tagName().length() > 0) {28 assertValidBy(findBy.tagName(), By.class);29 }30 if (findBy.xpath().length() > 0) {31 assertValidBy(findBy.xpath(), By.class);32 }33 }34 private void assertValidBy(String byValue, Class<? extends By> byType) {35 if (byValue == null || byValue.length() == 0) {36 throw new IllegalArgumentException(37 String.format("Cannot find elements when the %s is empty", byType.getSimpleName()));38 }39 }40 public By buildByFromFindBy(FindBy findBy) {41 if (findBy == null) {42 throw new IllegalArgumentException("Cannot find elements when @FindBy is null");43 }44 if (findBy.className().length() > 0) {45 return By.className(findBy.className());46 }47 if (findBy.css().length() > 0) {48 return By.cssSelector(findBy.css());49 }50 if (findBy.id().length() > 0) {51 return By.id(findBy.id());52 }53 if (findBy.linkText().length() > 0) {54 return By.linkText(findBy.linkText());55 }
LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!