下面列出了org.openqa.selenium.By#className ( ) 实例代码,或者点击链接到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;
}
@Override
public void executeAfterJSPreconditionHasBeenSatisfied(WebDriver driver,
ReplayingState state,
Function<SearchContext, SearchContext> contextProvider) {
By elementsLocator = By.className(initialCollectorClass);
List<WebElement> webElements = contextProvider.apply(driver).findElements(elementsLocator);
Optional<WebElement> webElementOptional = webElements.stream()
.filter(this::elementTextIsEqualToAndIsDisplayed)
.findFirst();
if (!webElementOptional.isPresent()) {
throw new ElementNotSelectableException("Element with class "
+ initialCollectorClass
+ " and text " + text + " was not found" );
}
webElementOptional.get().click();
}
@Test
public void throwsExceptionIfNoSuitableElementsAreFound() {
WebElement incorrectWebElement = mock(WebElement.class);
when(incorrectWebElement.getText()).thenReturn("sometATPhing");
when(incorrectWebElement.isDisplayed()).thenReturn(true);
List<WebElement> webElements = new ArrayList<>();
webElements.add(incorrectWebElement);
By elementsLocator = By.className(initialCollectorClass);
WebDriver webDriver = mock(WebDriver.class);
when(webDriver.findElements(elementsLocator)).thenReturn(webElements);
SearchContextFunction contextProvider = webDriver1 -> webDriver;
ClickClassByText clickClassByText = new ClickClassByText(initialCollectorClass, text, expectsHttp, contextProvider);
thrown.expect(ElementNotSelectableException.class);
clickClassByText.executeAfterJSPreconditionHasBeenSatisfied(webDriver, mock(ReplayingState.class), contextProvider);
}
@When("^I add (\\d+) items? to the cart$")
public void add_items_to_cart(int items){
By itemButton = By.className("btn_primary");
IntStream.range(0, items).forEach(i -> {
wait.until(ExpectedConditions.elementToBeClickable(getDriver().findElement(itemButton)));
getDriver().findElement(itemButton).click();
});
}
private By getBy(Identifier identifier) {
LOG.debug("Finding selenium Element : " + identifier.getLocator() + " by : " + identifier.getIdentifier());
if (identifier.getIdentifier().equalsIgnoreCase("id")) {
return By.id(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("name")) {
return By.name(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("class")) {
return By.className(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("css")) {
return By.cssSelector(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("xpath")) {
return By.xpath(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("link")) {
return By.linkText(identifier.getLocator());
} else if (identifier.getIdentifier().equalsIgnoreCase("data-cerberus")) {
return By.xpath("//*[@data-cerberus='" + identifier.getLocator() + "']");
} else {
throw new NoSuchElementException(identifier.getIdentifier());
}
}
@Test
public void throwsExceptionIfNoElementsAreFound() {
List<WebElement> webElements = new ArrayList<>();
By elementsLocator = By.className(initialCollectorClass);
WebDriver webDriver = mock(WebDriver.class);
when(webDriver.findElements(elementsLocator)).thenReturn(webElements);
SearchContextFunction contextProvider = webDriver1 -> webDriver;
ClickClassByText clickClassByText = new ClickClassByText(initialCollectorClass, text, expectsHttp, contextProvider);
thrown.expect(ElementNotSelectableException.class);
clickClassByText.executeAfterJSPreconditionHasBeenSatisfied(webDriver, mock(ReplayingState.class), contextProvider);
}
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 ProductTabContentForm() {
super(By.className("tab-content"), "Product tab content");
}
public By switchLocator() {
return By.className(this.uiElementClass.switchLocator());
}
@Override
public By getBy()
{
return By.className(getValue());
}
public By navigationBarLocator() {
return By.className(this.uiElementClass.navigationBarLocator());
}
public By webViewLocator() {
return By.className(this.uiElementClass.webViewLocator());
}
public By frameLayoutLocator() {
return By.className(this.uiElementClass.frameLayoutLocator());
}
public By tabHostLocator() {
return By.className(this.uiElementClass.tabHostLocator());
}
public By buttonLocator() {
return By.className(this.uiElementClass.buttonLocator());
}
public By tabWidgetLocator() {
return By.className(this.uiElementClass.tabWidgetLocator());
}
public By textViewLocator() {
return By.className(this.uiElementClass.textViewLocator());
}
private String getDatepickerMonth() {
By by = By.className("ui-datepicker-month");
waitForElementPresence(by);
return browser.driver.findElement(by).getText();
}
public By imageLocator() {
return By.className(this.uiElementClass.imageLocator());
}
public By imageButtonLocator() {
return By.className(this.uiElementClass.imageButtonLocator());
}