Java Code Examples for org.openqa.selenium.By#name()

The following examples show how to use org.openqa.selenium.By#name() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PropertyFileObjectRepositoryManager.java    From qashowcase with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Gets the By locator through reading the object repository.
 * @param key Is in the form of HomePage.LoginLink or ${pageName}.${elementName}
 * @return A By locator that can be used to locate WebElement(s).
 */
public By getByFromObjectRepositoryLocator(String key) {
    By by = null;
    String locatorType = getLocatorTypeString(key);
    String locatorString = getLocatorValueString(key);
    if(locatorType != null && locatorString != null) {
        if(locatorType.equalsIgnoreCase("LINK_TEXT")) {
            by = By.linkText(locatorString);
        } else if (locatorType.equalsIgnoreCase("PARTIAL_LINK_TEXT")) {
            by = By.partialLinkText(locatorString);
        } else if (locatorType.equalsIgnoreCase("NAME")) {
            by = By.name(locatorString);
        } else if (locatorType.equalsIgnoreCase("ID")) {
            by = By.id(locatorString);
        } else if (locatorType.equalsIgnoreCase("CLASS_NAME")) {
            by = By.className(locatorString);
        } else if (locatorType.equalsIgnoreCase("TAG_NAME")) {
            by = By.tagName(locatorString);
        } else if (locatorType.equalsIgnoreCase("CSS_SELECTOR")) {
            by = By.cssSelector(locatorString);
        } else if (locatorType.equalsIgnoreCase("XPATH")) {
            by = By.xpath(locatorString);
        }
    }
    return by;
}
 
Example 2
Source File: BaseUI.java    From movieapp-dialog with Apache License 2.0 6 votes vote down vote up
/**
 * Converts a locator string with a known prefix to a By object
 * @param myLocator
 * 		Supported locators:
 * 			xpath - "//"
 * 			   id - "id="
 * 	 css selector - "css="
 * 			xpath - "xpath="
 * 	 	 linktext - "link="
 * 		     name - "name="
 *linkpartialtext - "linkpartial="
 * @return By object extracted from given string locator
 */
private static By byFromLocator(String locator) {
	if (locator.startsWith("//")) {
		return By.xpath(locator);
	}
	if (locator.startsWith("id=")) {
		return By.id(locator.replaceFirst("id=", ""));
	}
	if (locator.startsWith("css=")) {
		return By.cssSelector(locator.replaceFirst("css=", ""));
	}
	if (locator.startsWith("xpath=")) {
		return By.xpath(locator.replaceFirst("xpath=", ""));
	}
	if (locator.startsWith("name=")) {
		return By.name(locator.replaceFirst("name=", ""));
	}
	if (locator.startsWith("link=")) {
		return By.linkText(locator.replaceFirst("link=", ""));
	}
	if (locator.startsWith("linkpartial=")) {
		return By.partialLinkText(locator.replaceFirst("linkpartial=", ""));
	}
	throw new IllegalArgumentException("Locator not supported: "
			+ locator);
}
 
Example 3
Source File: ByAllTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void findFourElementsByAnyInReverseOrder() {
  final WebElement elem1 = mock(WebElement.class, "webElement1");
  final WebElement elem2 = mock(WebElement.class, "webElement2");
  final WebElement elem3 = mock(WebElement.class, "webElement3");
  final WebElement elem4 = mock(WebElement.class, "webElement4");
  final List<WebElement> elems12 = new ArrayList<>();
  elems12.add(elem1);
  elems12.add(elem2);
  final List<WebElement> elems34 = new ArrayList<>();
  elems34.add(elem3);
  elems34.add(elem4);
  final List<WebElement> elems3412 = new ArrayList<>();
  elems3412.addAll(elems34);
  elems3412.addAll(elems12);

  when(driver.findElements(By.name("cheese"))).thenReturn(elems12);
  when(driver.findElements(By.name("photo"))).thenReturn(elems34);

  ByAll by = new ByAll(By.name("photo"), By.name("cheese"));
  assertThat(by.findElements(driver)).isEqualTo(elems3412);

  verify(driver, times(2)).findElements(any(By.class));
  verifyNoMoreInteractions(driver);
}
 
Example 4
Source File: ByChainedTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void findElementsTwoByEmptyChild() {
  final AllDriver driver = mock(AllDriver.class);
  final WebElement elem1 = mock(WebElement.class, "webElement1");
  final WebElement elem2 = mock(AllElement.class, "webElement2");
  final WebElement elem3 = mock(AllElement.class, "webElement3");
  final WebElement elem4 = mock(AllElement.class, "webElement4");
  final WebElement elem5 = mock(AllElement.class, "webElement5");

  final List<WebElement> elems = new ArrayList<>();
  final List<WebElement> elems12 = new ArrayList<>();
  elems12.add(elem1);
  elems12.add(elem2);
  final List<WebElement> elems34 = new ArrayList<>();
  elems34.add(elem3);
  elems34.add(elem4);
  final List<WebElement> elems5 = new ArrayList<>();
  elems5.add(elem5);
  final List<WebElement> elems345 = new ArrayList<>();
  elems345.addAll(elems34);
  elems345.addAll(elems5);

  when(driver.findElements(By.name("cheese"))).thenReturn(elems12);
  when(elem1.findElements(By.name("photo"))).thenReturn(elems);
  when(elem2.findElements(By.name("photo"))).thenReturn(elems5);

  ByChained by = new ByChained(By.name("cheese"), By.name("photo"));
  assertThat(by.findElements(driver)).isEqualTo(elems5);
}
 
Example 5
Source File: ByAllTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void findElementOneBy() {
  final WebElement elem1 = mock(WebElement.class, "webElement1");
  final WebElement elem2 = mock(WebElement.class, "webElement2");
  final List<WebElement> elems12 = new ArrayList<>();
  elems12.add(elem1);
  elems12.add(elem2);

  when(driver.findElements(By.name("cheese"))).thenReturn(elems12);

  ByAll by = new ByAll(By.name("cheese"));
  assertThat(by.findElement(driver)).isEqualTo(elem1);
}
 
Example 6
Source File: ByAllTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void findElementsOneBy() {
  final WebElement elem1 = mock(WebElement.class, "webElement1");
  final WebElement elem2 = mock(WebElement.class, "webElement2");
  final List<WebElement> elems12 = new ArrayList<>();
  elems12.add(elem1);
  elems12.add(elem2);

  when(driver.findElements(By.name("cheese"))).thenReturn(elems12);

  ByAll by = new ByAll(By.name("cheese"));
  assertThat(by.findElements(driver)).isEqualTo(elems12);
}
 
Example 7
Source File: BaseAction.java    From PatatiumAppUi with GNU General Public License v2.0 5 votes vote down vote up
static By getBy (ByType byType,Locator locator)
{
	switch(byType)
	{
		case id:
			return By.id(locator.getElement());
		case cssSelector:
			return By.cssSelector(locator.getElement());
		case name:
			return By.name(locator.getElement());
		case xpath:
			return By.xpath(locator.getElement());
		case className:
			return By.className(locator.getElement());
		case tagName:
			return By.tagName(locator.getElement());
		case linkText:
			return By.linkText(locator.getElement());
		case partialLinkText:
			return By.partialLinkText(locator.getElement());
		//return null也可以放到switch外面
		default:
			return null;
	}


}
 
Example 8
Source File: ByChainedTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void findElementsOneBy() {
  final AllDriver driver = mock(AllDriver.class);
  final WebElement elem1 = mock(WebElement.class, "webElement1");
  final WebElement elem2 = mock(WebElement.class, "webElement2");
  final List<WebElement> elems12 = new ArrayList<>();
  elems12.add(elem1);
  elems12.add(elem2);

  when(driver.findElements(By.name("cheese"))).thenReturn(elems12);

  ByChained by = new ByChained(By.name("cheese"));
  assertThat(by.findElements(driver)).isEqualTo(elems12);
}
 
Example 9
Source File: ByAllTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void findElementOneByEmpty() {
  final List<WebElement> elems = new ArrayList<>();

  when(driver.findElements(By.name("cheese"))).thenReturn(elems);

  ByAll by = new ByAll(By.name("cheese"));
  assertThatExceptionOfType(NoSuchElementException.class)
      .isThrownBy(() -> by.findElement(driver));
}
 
Example 10
Source File: HTMLElement.java    From at.info-knowledge-base with MIT License 5 votes vote down vote up
public final void initElement(final SearchBy elementSearchCriteria, final String elementValue) {

        switch (elementSearchCriteria) {
            case ID:
                locator = By.id(elementValue);
                break;
            case XPATH:
                locator = By.xpath(elementValue);
                break;
            case LINK_TEXT:
                locator = By.linkText(elementValue);
                break;
            case CLASS_NAME:
                locator = By.className(elementValue);
                break;
            case CSS_SELECTOR:
                locator = By.cssSelector(elementValue);
                break;
            case TAG_NAME:
                locator = By.tagName(elementValue);
                break;
            case NAME:
                locator = By.name(elementValue);
                break;
            case PARTIAL_LINK_TEXT:
                locator = By.partialLinkText(elementValue);
                break;
        }
    }
 
Example 11
Source File: ByAllTest.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void findElementsOneByEmpty() {
  when(driver.findElements(By.name("cheese"))).thenReturn(new ArrayList<>());

  ByAll by = new ByAll(By.name("cheese"));
  assertThat(by.findElements(driver)).isEmpty();
}
 
Example 12
Source File: SeleniumElement.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
private ByWrapper _useBy( BY byType, String keyValue )
{
  switch (byType)
  {
  case CLASS:
    return new ByWrapper(By.className(keyValue));

  case CSS:
    return new ByWrapper(By.cssSelector(keyValue));

  case ID:
    return new ByWrapper(By.id(keyValue));

  case LINK_TEXT:
    return new ByWrapper(By.linkText(keyValue));

  case NAME:
    return new ByWrapper(By.name(keyValue));

  case TAG_NAME:
    return new ByWrapper(By.tagName(keyValue));

  case XPATH:

    if (getWebDriver().getPopulatedDevice().getOs() != null && getWebDriver().getPopulatedDevice().getOs().toUpperCase().equals("IOS"))
    {
      if ((getWebDriver().getPopulatedDevice().getOsVersion() != null && getWebDriver().getPopulatedDevice().getOsVersion().startsWith("11")) || (getWebDriver().getCapabilities().getCapability(DriverFactory.AUTOMATION_NAME) != null && getWebDriver().getCapabilities().getCapability(DriverFactory.AUTOMATION_NAME).equals("XCUITest")))
      {
        if (keyValue.contains("UIA"))
        {
          log.warn("UI Autopmation XPATH detected - replacing UIA Code in " + keyValue);
          keyValue = XPathGenerator.XCUIConvert(keyValue);
        }
      }
    }

    return new ByWrapper(By.xpath(keyValue));

  case NATURAL:
    return new ByWrapper(new ByNaturalLanguage(keyValue, getWebDriver()));

  case V_TEXT:
    return new ByWrapper(new ByOCR(keyValue, getElementProperties(), getWebDriver()));

  case V_IMAGE:
    return new ByWrapper(new ByImage(keyValue, getElementProperties(), getWebDriver()));

  case HTML:
    HTMLElementLookup elementLookup = new HTMLElementLookup(keyValue);
    return new ByWrapper(By.xpath(elementLookup.toXPath()));

  case PROP:
    Map<String, String> propertyMap = new HashMap<String, String>(10);
    propertyMap.put("resource-id", ((DeviceWebDriver) getWebDriver()).getAut().getAndroidIdentifier());
    return new ByWrapper(By.xpath(XPathGenerator.generateXPathFromProperty(propertyMap, keyValue)));
  default:
    return null;
  }
}
 
Example 13
Source File: Name.java    From webtester2-core with Apache License 2.0 4 votes vote down vote up
@Override
public By createBy(String value) {
    return By.name(value);
}
 
Example 14
Source File: AppiumTestAction.java    From opentest with MIT License 4 votes vote down vote up
protected By readLocatorArgument(String argName) {
    Object argumentValue = readArgument(argName);

    if (argumentValue instanceof String) {
        Map<String, Object> newArgValue = new HashMap<String, Object>();
        newArgValue.put("xpath", argumentValue);
        argumentValue = newArgValue;
    }

    Map<String, Object> argValueAsMap = (Map<String, Object>) argumentValue;

    if (argValueAsMap.containsKey("id")) {
        return By.id(argValueAsMap.get("id").toString());
    } else if (argValueAsMap.containsKey("accessibilityId")) {
        return MobileBy.AccessibilityId(argValueAsMap.get("accessibilityId").toString());
    } else if (argValueAsMap.containsKey("predicate")) {
        return MobileBy.iOSNsPredicateString(argValueAsMap.get("predicate").toString());
    } else if (argValueAsMap.containsKey("iosClassChain")) {
        return MobileBy.iOSClassChain(argValueAsMap.get("iosClassChain").toString());
    } else if (argValueAsMap.containsKey("androidUiAutomator")) {
        return MobileBy.AndroidUIAutomator(argValueAsMap.get("androidUiAutomator").toString());
    } else if (argValueAsMap.containsKey("name")) {
        return By.name(argValueAsMap.get("name").toString());
    } else if (argValueAsMap.containsKey("css")) {
        return By.cssSelector(argValueAsMap.get("css").toString());
    } else if (argValueAsMap.containsKey("class")) {
        return By.className(argValueAsMap.get("class").toString());
    } else if (argValueAsMap.containsKey("tag")) {
        return By.tagName(argValueAsMap.get("tag").toString());
    } else if (argValueAsMap.containsKey("linkText")) {
        return By.linkText(argValueAsMap.get("linkText").toString());
    } else if (argValueAsMap.containsKey("partialLinkText")) {
        return By.partialLinkText(argValueAsMap.get("partialLinkText").toString());
    } else if (argValueAsMap.containsKey("xpath")) {
        String xpath = argValueAsMap.get("xpath").toString().replace("''", "'");
        return By.xpath(xpath);
    } else if (argValueAsMap.containsKey("image")) {
        String imageBase64 = argValueAsMap.get("image").toString();
        return MobileBy.image(imageBase64); // TO DO continue implementation
    } else {
        throw new RuntimeException(
                "You must provide at least one valid identification method the locator "
                + "object by populating at least 1 of the following properties: id, name, "
                + "css, class, tag, linkText, partialLinkText, xpath.");
    }
}
 
Example 15
Source File: RecordClickNameIT.java    From bromium with MIT License 4 votes vote down vote up
public RecordClickNameIT() {
    super(CLICK_NAME_DEMO_PAGE, CLICK_CLICK_NAME_TEST, By.name(CLICK_NAME_TEST));
}
 
Example 16
Source File: CalculatorScreen.java    From seleniumtestsframework with Apache License 2.0 4 votes vote down vote up
public ButtonElement getSymbolElement(final String symbol) {
    return new ButtonElement("Button Element", By.name(symbol));
}
 
Example 17
Source File: NameByConverter.java    From webtester-core with Apache License 2.0 4 votes vote down vote up
@Override
public By toBy(String value) {
    return By.name(value);
}
 
Example 18
Source File: CalculatorScreen.java    From seleniumtests with Apache License 2.0 4 votes vote down vote up
public ButtonElement getSymbolElement(final String symbol) {
    return new ButtonElement("Button Element", By.name(symbol));
}
 
Example 19
Source File: ByIdOrName.java    From selenium with Apache License 2.0 4 votes vote down vote up
public ByIdOrName(String idOrName) {
  this.idOrName = idOrName;
  idFinder = By.id(idOrName);
  nameFinder = By.name(idOrName);
}
 
Example 20
Source File: LocalizedAnnotations.java    From carina with Apache License 2.0 4 votes vote down vote up
private By createBy(String locator) {
    if (locator.startsWith("id=")) {
        return By.id(StringUtils.remove(locator, "id="));
    } else if (locator.startsWith("name=")) {
        return By.name(StringUtils.remove(locator, "name="));
    } else if (locator.startsWith("xpath=")) {
        return By.xpath(StringUtils.remove(locator, "xpath="));
    } else if (locator.startsWith("linkText=")) {
        return By.linkText(StringUtils.remove(locator, "linkText="));
    } else if (locator.startsWith("partialLinkText=")) {
        return By.partialLinkText(StringUtils.remove(locator, "partialLinkText="));
    } else if (locator.startsWith("cssSelector=")) {
        return By.cssSelector(StringUtils.remove(locator, "cssSelector="));
    } else if (locator.startsWith("css=")) {
        return By.cssSelector(StringUtils.remove(locator, "css="));
    } else if (locator.startsWith("tagName=")) {
        return By.tagName(StringUtils.remove(locator, "tagName="));
    } else if (locator.startsWith("className=")) {
        return By.className(StringUtils.remove(locator, "className="));
    } else if (locator.startsWith("By.id: ")) {
        return By.id(StringUtils.remove(locator, "By.id: "));
    } else if (locator.startsWith("By.name: ")) {
        return By.name(StringUtils.remove(locator, "By.name: "));
    } else if (locator.startsWith("By.xpath: ")) {
        return By.xpath(StringUtils.remove(locator, "By.xpath: "));
    } else if (locator.startsWith("By.linkText: ")) {
        return By.linkText(StringUtils.remove(locator, "By.linkText: "));
    } else if (locator.startsWith("By.partialLinkText: ")) {
        return By.partialLinkText(StringUtils.remove(locator, "By.partialLinkText: "));
    } else if (locator.startsWith("By.css: ")) {
        return By.cssSelector(StringUtils.remove(locator, "By.css: "));
    } else if (locator.startsWith("By.cssSelector: ")) {
        return By.cssSelector(StringUtils.remove(locator, "By.cssSelector: "));
    } else if (locator.startsWith("By.className: ")) {
        return By.className(StringUtils.remove(locator, "By.className: "));
    } else if (locator.startsWith("By.tagName: ")) {
        return By.tagName(StringUtils.remove(locator, "By.tagName: "));
    }       

    throw new RuntimeException(String.format("Unable to generate By using locator: '%s'!", locator));
}