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

The following examples show how to use org.openqa.selenium.By#toString() . 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: BaseHandler.java    From healenium-web with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 2
Source File: ExtendedWebElement.java    From carina with Apache License 2.0 5 votes vote down vote up
/**
  * 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());
     }
 }
 
Example 3
Source File: LocalizedAnnotations.java    From carina with Apache License 2.0 5 votes vote down vote up
@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;
 }
 
Example 4
Source File: WebDriverAftBase.java    From rice with Educational Community License v2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: Wait.java    From functional-tests-core with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: LoggingInvocationHandler.java    From masquerade with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 7
Source File: AbstractLocator.java    From phoenix.webui.framework with Apache License 2.0 5 votes vote down vote up
@Override
public String toString()
{
	By by = getBy();
	String srtBy = (by == null ? "null locator" : by.toString());
	
	return srtBy;
}
 
Example 8
Source File: FakeWebDriver.java    From webtau with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 9
Source File: WebElementProxyHandler.java    From healenium-web with Apache License 2.0 5 votes vote down vote up
@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);
    }

}
 
Example 10
Source File: ElementFactory.java    From aquality-selenium-java with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 11
Source File: BaseElement.java    From WebAndAppUITesting with GNU General Public License v3.0 4 votes vote down vote up
public BaseElement(By by) {
	this.by = by;
	oriElementStr = by.toString();
}
 
Example 12
Source File: SmartWebElement.java    From blueocean-plugin with MIT License 4 votes vote down vote up
public SmartWebElement(WebDriver driver, By by) {
    this(driver, by.toString(), by);
}
 
Example 13
Source File: DeviceWebDriver.java    From xframium-java with GNU General Public License v3.0 4 votes vote down vote up
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 );
}
 
Example 14
Source File: NullWebElement.java    From webtau with Apache License 2.0 4 votes vote down vote up
@Override
public WebElement findElement(By by) {
    return new NullWebElement(by.toString());
}
 
Example 15
Source File: ExtendedWebElement.java    From carina with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 16
Source File: ByType.java    From Selenium-Foundation with Apache License 2.0 2 votes vote down vote up
/**
 * 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();
}
 
Example 17
Source File: Identification.java    From webtester-core with Apache License 2.0 2 votes vote down vote up
/**
 * 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();
}