下面列出了org.openqa.selenium.By#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Extracts XPath from passed locator.
*
* @param locator locator to get xpath from.
* @return extracted XPath.
*/
@Override
protected String extractXPathFromLocator(By locator) {
String locatorString = locator.toString();
int indexOfDots = locatorString.indexOf(':');
String locValuableString = indexOfDots == -1
// case ByIdOrName:
? locatorString.substring(locatorString.indexOf('"')).replace("\"", "")
: locatorString.substring(indexOfDots + 1).trim();
Class<? extends By> locatorClass = locator.getClass();
return getLocatorToXPathTemplateMap().containsKey(locator.getClass())
? String.format(getLocatorToXPathTemplateMap().get(locatorClass), locValuableString)
: super.extractXPathFromLocator(locator);
}
protected WebElement findElement(By by) {
try {
PageAwareBy pageBy = awareBy(by);
By inner = pageBy.getBy();
if (engine.isHealingEnabled()) {
return lookUp(pageBy);
}
return driver.findElement(inner);
} catch (Exception ex) {
throw new NoSuchElementException("Failed to find element using " + by.toString(), ex);
}
}
@Override
protected WebElement findElement(By by) {
try {
PageAwareBy pageBy = awareBy(by);
if (engine.isHealingEnabled()) {
return lookUp(pageBy);
}
return delegate.findElement(pageBy.getBy());
} catch (Exception ex) {
throw new NoSuchElementException("Failed to find element using " + by.toString(), ex);
}
}
@Override
public List<WebElement> findElements(By by) {
if (by instanceof By.ByCssSelector) {
String rendered = by.toString();
int idxOfColon = rendered.indexOf(":");
String css = rendered.substring(idxOfColon + 1).trim();
return fakesByCss.containsKey(css) ? Collections.singletonList(fakesByCss.get(css)) : Collections.emptyList();
}
return Collections.emptyList();
}
@Override
public String toString()
{
By by = getBy();
String srtBy = (by == null ? "null locator" : by.toString());
return srtBy;
}
private String formatBy(By by) {
if (by instanceof Selectors.ByChain) {
return formatBy(((Selectors.ByChain) by).getLastBy());
}
if (by instanceof Selectors.ByCubaId) {
return ((Selectors.ByCubaId) by).getCubaId();
}
return by.toString();
}
public boolean waitForNotVisible(By locator, int timeOut, boolean failOnVisible) {
this.client.setWait(1);
long startTime = new Date().getTime();
boolean found = true;
for (int i = 0; i < 1000; i++) {
long currentTime = new Date().getTime();
if ((currentTime - startTime) < timeOut * 1000) {
List<UIElement> elements = null;
try {
elements = this.find.elementsByLocator(locator);
} catch (Exception e) {
}
if ((elements != null) && (elements.size() != 0)) {
LOGGER_BASE.debug("OldElement exists: " + locator.toString());
} else {
found = false;
break;
}
}
}
this.client.setWait(this.settings.defaultTimeout);
if (found) {
String error = "OldElement still visible: " + locator.toString();
LOGGER_BASE.error(error);
if (failOnVisible) {
Assert.fail(error);
}
} else {
LOGGER_BASE.debug("OldElement not found: " + locator.toString());
}
return found;
}
protected List<WebElement> findElements(By by, WebElement element) {
if (element == null) {
checkForIncidentReport();
throw new AssertionError("element to findElements on for " + by.toString() + " is null in class " + this.getClass().toString());
}
List<WebElement> found = element.findElements(by);
return found;
}
@Override
public By buildBy() {
By by = super.buildBy();
String param = by.toString();
// replace by using localization pattern
Matcher matcher = L10N_PATTERN.matcher(param);
while (matcher.find()) {
int start = param.indexOf(SpecialKeywords.L10N + ":") + 5;
int end = param.indexOf("}");
String key = param.substring(start, end);
param = StringUtils.replace(param, matcher.group(), L10N.getText(key));
}
if (getField().isAnnotationPresent(Predicate.class)) {
// TODO: analyze howto determine iOS or Android predicate
param = StringUtils.remove(param, "By.xpath: ");
by = MobileBy.iOSNsPredicateString(param);
// by = MobileBy.AndroidUIAutomator(param);
} else if (getField().isAnnotationPresent(ClassChain.class)) {
param = StringUtils.remove(param, "By.xpath: ");
by = MobileBy.iOSClassChain(param);
} else if (getField().isAnnotationPresent(AccessibilityId.class)) {
param = StringUtils.remove(param, "By.name: ");
by = MobileBy.AccessibilityId(param);
} else if (getField().isAnnotationPresent(ExtendedFindBy.class)) {
if (param.startsWith("By.AndroidUIAutomator: ")) {
param = StringUtils.remove(param, "By.AndroidUIAutomator: ");
by = MobileBy.AndroidUIAutomator(param);
}
LOGGER.debug("Annotation ExtendedFindBy has been detected. Returning locator : " + by);
} else {
by = createBy(param);
}
return by;
}
/**
* Find Extended Web Element on page using By starting search from this
* object.
*
* @param by Selenium By locator
* @param name Element name
* @param timeout Timeout to find
* @return ExtendedWebElement if exists otherwise null.
*/
public ExtendedWebElement findExtendedWebElement(final By by, String name, long timeout) {
if (isPresent(by, timeout)) {
try {
return new ExtendedWebElement(getCachedElement().findElement(by), name, by);
} catch (StaleElementReferenceException e) {
return new ExtendedWebElement(getElement().findElement(by), name, by);
}
} else {
throw new NoSuchElementException("Unable to find dynamic element using By: " + by.toString());
}
}
@Override
public WebElement findElement(By by) {
return new NullWebElement(by.toString());
}
public BaseElement(By by) {
this.by = by;
oriElementStr = by.toString();
}
public SmartWebElement(WebDriver driver, By by) {
this(driver, by.toString(), by);
}
public List<WebElement> findElements( By by )
{
setLastAction();
if ( log.isInfoEnabled() )
log.info( Thread.currentThread().getName() + ": Locating element using [" + by + "]" );
if ( cachingEnabled && cachedDocument == null )
cacheData();
if ( cachingEnabled && cachedDocument != null )
{
try
{
XPath xPath = xPathFactory.newXPath();
String path = by.toString();
path = path.substring( path.indexOf( ": " ) + 2 );
NodeList nodes = (NodeList) xPath.evaluate( path, cachedDocument, XPathConstants.NODESET );
List<WebElement> elementList = new ArrayList<WebElement>( 10 );
for ( int i = 0; i < nodes.getLength(); i++ )
{
if ( reportingElement )
elementList.add( new ReportingWebElementAdapter( new CachedWebElement( this, webDriver, by, nodes.item( i ) ), this, by ) );
else
elementList.add( new CachedWebElement( this, webDriver, by, nodes.item( i ) ) );
}
return elementList;
}
catch ( Exception e )
{
log.warn( "Error reading from cache " + e.getMessage() );
if ( !syntheticConnection )
{
cachingEnabled = false;
cachedDocument = null;
}
else
return null;
}
}
if ( reportingElement )
{
List<WebElement> currentList = webDriver.findElements( by );
if ( !currentList.isEmpty() )
{
List<WebElement> newList = new ArrayList<WebElement>( currentList.size() );
for ( WebElement wE : currentList )
newList.add( new ReportingWebElementAdapter( wE, this, by ) );
return newList;
}
else
return currentList;
}
else
return webDriver.findElements( by );
}
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;
}
/**
* Get the underlying value of the specified Selenium locator
*
* @param locator Selenium locator
* @return value extracted from the specified locator
*/
private static String valueOf(final By locator) {
String str = locator.toString();
int i = str.indexOf(':');
return str.substring(i + 1).trim();
}
/**
* Create an {@link Identification identification} using the given Selenium
* {@link By}. This constructor is intended to be used in cases where an
* existing {@link By} has to be transformed into an identification.
*
* @param seleniumBy the {@link By} to store
* @since 0.9.9
*/
public Identification(By seleniumBy) {
this.seleniumBy = seleniumBy;
this.toStringMessage = seleniumBy.toString();
}