下面列出了org.openqa.selenium.By#tagName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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;
}
public By pickFrom(String method, String selector) {
if ("class name".equals(method)) {
return By.className(selector);
} else if ("css selector".equals(method)) {
return By.cssSelector(selector);
} else if ("id".equals(method)) {
return By.id(selector);
} else if ("link text".equals(method)) {
return By.linkText(selector);
} else if ("partial link text".equals(method)) {
return By.partialLinkText(selector);
} else if ("name".equals(method)) {
return By.name(selector);
} else if ("tag name".equals(method)) {
return By.tagName(selector);
} else if ("xpath".equals(method)) {
return By.xpath(selector);
} else {
throw new WebDriverException("Cannot find matching element locator to: " + method);
}
}
/**
* A helper method to convert Hook to By
* @param hook The hook that specifies a web element
* @return The converted By
* @throws CandybeanException
*/
public static By getBy(Hook hook) throws CandybeanException {
switch (hook.hookStrategy) {
case CSS:
return By.cssSelector(hook.hookString);
case XPATH:
return By.xpath(hook.hookString);
case ID:
return By.id(hook.hookString);
case NAME:
return By.name(hook.hookString);
case LINK:
return By.linkText(hook.hookString);
case PLINK:
return By.partialLinkText(hook.hookString);
case CLASS:
return By.className(hook.hookString);
case TAG:
return By.tagName(hook.hookString);
default:
throw new CandybeanException("Strategy type not recognized.");
}
}
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;
}
}
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;
}
}
protected By buildByFromShortFindBy(FindBy findBy) {
if (!"".equals(findBy.className())) {
return By.className(findBy.className());
}
if (!"".equals(findBy.css())) {
return By.cssSelector(findBy.css());
}
if (!"".equals(findBy.id())) {
return By.id(findBy.id());
}
if (!"".equals(findBy.linkText())) {
return By.linkText(findBy.linkText());
}
if (!"".equals(findBy.name())) {
return By.name(findBy.name());
}
if (!"".equals(findBy.partialLinkText())) {
return By.partialLinkText(findBy.partialLinkText());
}
if (!"".equals(findBy.tagName())) {
return By.tagName(findBy.tagName());
}
if (!"".equals(findBy.xpath())) {
return By.xpath(findBy.xpath());
}
// Fall through
return null;
}
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;
}
}
@Override
public By getBy()
{
return By.tagName(getValue());
}
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.");
}
}
@SProperty(name = "type")
public By getByType(String tagName) {
return By.tagName(tagName);
}
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;
}
}
@Override
public By createBy(String value) {
return By.tagName(value);
}
public static By getBy(String loc, PropertyUtil props) {
Gson gson = new Gson();
loc = props.getSubstitutor().replace(loc);
loc = props.getString(loc, loc);
JsonElement element = JSONUtil.getGsonElement(loc);
if ((null != element) && element.isJsonObject()) {
Object obj = gson.fromJson(element, Map.class).get("locator");
loc = obj instanceof String ? (String) obj :
gson.toJson(obj);
}
element = JSONUtil.getGsonElement(loc);
if ((null != element) && element.isJsonArray()) {
String[] locs = new Gson().fromJson(element, String[].class);
return new ByAny(locs);
}
if (loc.startsWith("//")) {
return By.xpath(loc);
} else if (loc.indexOf("=") > 0) {
String parts[] = loc.split("=", 2);
if (parts[0].equalsIgnoreCase("key") || parts[0].equalsIgnoreCase("property")) {
String val = props.getSubstitutor().replace(parts[1]);
return getBy(props.getString(val, val), props);
}
if (parts[0].equalsIgnoreCase("jquery")) {
return new ByJQuery(parts[1]);
}
if (parts[0].equalsIgnoreCase("extDom")) {
return new ByExtDomQuery(parts[1]);
}
if (parts[0].equalsIgnoreCase("extComp")) {
return new ByExtCompQuery(parts[1]);
}
if (parts[0].equalsIgnoreCase("name")) {
return By.name(parts[1]);
} else if (parts[0].equalsIgnoreCase("id")) {
return By.id(parts[1]);
} else if (parts[0].equalsIgnoreCase("xpath")) {
return By.xpath(parts[1]);
} else if (parts[0].equalsIgnoreCase("css")) {
return By.cssSelector(parts[1]);
} else if (parts[0].equalsIgnoreCase("link") || parts[0].equalsIgnoreCase("linkText")) {
return By.linkText(parts[1]);
} else if (parts[0].equalsIgnoreCase("partialLink") || parts[0].equalsIgnoreCase("partialLinkText")) {
return By.partialLinkText(parts[1]);
} else if (parts[0].equalsIgnoreCase("className")) {
return By.className(parts[1]);
} else if (parts[0].equalsIgnoreCase("tagName")) {
return By.tagName(parts[1]);
} else {
return new ByCustom(parts[0], parts[1]);
}
} else {
return By.xpath(String.format("//*[@name='%s' or @id='%s' or @value='%s']", loc, loc, loc));
}
}
@Override
public By toBy(String value) {
return By.tagName(value);
}
public String getPageTitle() {
By headerTag = By.tagName("h1");
waitForElementPresence(headerTag);
return browser.driver.findElement(headerTag).getText();
}
public static RelativeBy withTagName(String tagName) {
Require.nonNull("Tag name to look for", tagName);
return new RelativeBy(By.tagName(tagName));
}
/**
* New instance.
* @param locator the locator as string.
*/
private EByFirstMatching(String locator) {
super(locator, new ByFirstMatching(By.xpath(locator), By.id(locator),
By.name(locator), By.cssSelector(locator),
By.className(locator), By.tagName(locator)));
}
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));
}
public By generateByForList(By by, int index) {
String locator = by.toString();
By resBy = null;
if (locator.startsWith("By.id: ")) {
resBy = By.id(StringUtils.remove(locator, "By.id: ") + "[" + index + "]");
}
if (locator.startsWith("By.name: ")) {
resBy = By.name(StringUtils.remove(locator, "By.name: ") + "[" + index + "]");
}
if (locator.startsWith("By.xpath: ")) {
resBy = By.xpath(StringUtils.remove(locator, "By.xpath: ") + "[" + index + "]");
}
if (locator.startsWith("linkText: ")) {
resBy = By.linkText(StringUtils.remove(locator, "linkText: ") + "[" + index + "]");
}
if (locator.startsWith("partialLinkText: ")) {
resBy = By.partialLinkText(StringUtils.remove(locator, "partialLinkText: ") + "[" + index + "]");
}
if (locator.startsWith("css: ")) {
resBy = By.cssSelector(StringUtils.remove(locator, "css: ") + ":nth-child(" + index + ")");
}
if (locator.startsWith("By.cssSelector: ")) {
resBy = By.cssSelector(StringUtils.remove(locator, "By.cssSelector: ") + ":nth-child(" + index + ")");
}
if (locator.startsWith("tagName: ")) {
resBy = By.tagName(StringUtils.remove(locator, "tagName: ") + "[" + index + "]");
}
/*
* All ClassChain locators start from **. e.g FindBy(xpath = "**'/XCUIElementTypeStaticText[`name CONTAINS[cd] '%s'`]")
*/
if (locator.startsWith("By.IosClassChain: **")) {
resBy = MobileBy.iOSClassChain(StringUtils.remove(locator, "By.IosClassChain: ") + "[" + index + "]");
}
if (locator.startsWith("By.IosNsPredicate: **")) {
resBy = MobileBy.iOSNsPredicateString(StringUtils.remove(locator, "By.IosNsPredicate: ") + "[" + index + "]");
}
if (locator.startsWith("By.AccessibilityId: ")) {
resBy = MobileBy.AccessibilityId(StringUtils.remove(locator, "By.AccessibilityId: ") + "[" + index + "]");
}
return resBy;
}