下面列出了org.openqa.selenium.By#linkText ( ) 实例代码,或者点击链接到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;
}
/**
* Determine locator.
*
* @param locator
* the locator
* @return the by
*/
public By determineLocator(String locator) {
if (locator.startsWith(XPATH)) {
return By.xpath(findLocatorSubstring(locator));
} else if (locator.startsWith("//")) {
return By.xpath(locator);
} else if (locator.startsWith(CSS)) {
return ByExtended.cssSelector(findLocatorSubstring(locator));
} else if (locator.startsWith(NAME)) {
return By.name(findLocatorSubstring(locator));
} else if (locator.startsWith(LINK)) {
return By.linkText(findLocatorSubstring(locator));
} else if (locator.startsWith(ID)) {
return By.id(findLocatorSubstring(locator));
}
else {
return By.id(locator);
}
}
/**
* 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);
}
/**
* 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.");
}
}
protected void testDemoTravelAccountLookUpAutoSearch() throws Exception {
waitForTextPresent("Showing 1 to 10");
By[] results = {By.linkText("a1"), By.linkText("a2"), By.linkText("a3"), By.linkText("a4"), By.linkText("a5"),
By.linkText("a6"), By.linkText("a7"), By.linkText("a8"), By.linkText("a9"), By.linkText("a10"),
By.linkText("a11"), By.linkText("a12"), By.linkText("a13"), By.linkText("a14")};
assertElementsPresentInResultPages(results);
waitAndClickByXpath("//div[@data-label='Travel Account Type Code']/div/div/button[@class='btn btn-default uif-action icon-search']");
waitSearchAndReturnFromLightbox();
// TODO should the foid work?
// waitAndTypeByName("lookupCriteria[foId]","1");
// waitAndClickButtonByText(SEARCH);
// Thread.sleep(3000);
// assertElementPresentByXpath("//a[contains(text(), 'a1')]");
// if(isElementPresentByLinkText("a2") || isElementPresentByLinkText("a3")) {
// fail("Search Functionality not working properly.");
// }
}
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);
}
}
/**
* This method read a application descriptor file and return a {@link org.openqa.selenium.By} object (xpath, id, link ...).
*
* @param applicationKey
* Name of application. Each application has its fair description file.
* @param code
* Name of element on the web Page.
* @param args
* list of description (xpath, id, link ...) for code.
* @return a {@link org.openqa.selenium.By} object (xpath, id, link ...)
*/
public static By getLocator(String applicationKey, String code, Object... args) {
By locator = null;
log.debug("getLocator with this application key : {}", applicationKey);
log.debug("getLocator with this code : {}", code);
log.debug("getLocator with this locator file : {}", Context.iniFiles.get(applicationKey));
final Ini ini = Context.iniFiles.get(applicationKey);
final Map<String, String> section = ini.get(code);
if (section != null) {
final Entry<String, String> entry = section.entrySet().iterator().next();
final String selector = String.format(entry.getValue(), args);
if ("css".equals(entry.getKey())) {
locator = By.cssSelector(selector);
} else if ("link".equals(entry.getKey())) {
locator = By.linkText(selector);
} else if ("id".equals(entry.getKey())) {
locator = By.id(selector);
} else if ("name".equals(entry.getKey())) {
locator = By.name(selector);
} else if ("xpath".equals(entry.getKey())) {
locator = By.xpath(selector);
} else if ("class".equals(entry.getKey())) {
locator = By.className(selector);
} else {
Assert.fail(entry.getKey() + " NOT implemented!");
}
} else {
Assert.fail("[" + code + "] NOT implemented in ini file " + Context.iniFiles.get(applicationKey) + "!");
}
return locator;
}
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());
}
}
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;
}
}
protected void testDemoTravelAccountLookUpAdditionalSearch() throws Exception {
waitForTextPresent("Showing 1 to 10");
By[] results = {By.linkText("a1"), By.linkText("a2"), By.linkText("a3"), By.linkText("a4"), By.linkText("a5"),
By.linkText("a6"), By.linkText("a7"), By.linkText("a8"), By.linkText("a9"), By.linkText("a10"),
By.linkText("a11"), By.linkText("a12"), By.linkText("a13"), By.linkText("a14")};
assertElementsPresentInResultPages(results);
waitAndClickButtonByText(SEARCH);
checkForIncidentReport();
}
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;
}
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));
}
@Override
public By getBy()
{
return By.linkText(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 = "link_text")
public By getByLinkText(String linkText) {
return By.linkText(linkText);
}
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;
}
}
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));
}
}
/**
* Test for template create, edit and delete functions
*
* @throws Exception
*/
@Test(groups = "wso2.esb",
description = "Verify that endpoints page renders when an invalid dynamic endpoint is present.")
public void testTemplatesListing() throws Exception {
LoginPage loginPage = new LoginPage(driver);
loginPage.loginAs(userInfo.getUserName(), userInfo.getPassword());
//go to template listing
driver.findElement(By.linkText("Templates")).click();
//create a new template
By newTemplateButoon = By.linkText("Add Sequence Template");
Assert.assertTrue(isElementPresent(newTemplateButoon),
"Unable to find the button to create a new sequence template");
driver.findElement(newTemplateButoon).click();
driver.findElement(By.id("sequence.name")).clear();
driver.findElement(By.id("sequence.name")).sendKeys("sampleTemplate");
//create template configuration
driver.findElement(By.linkText("Add Child")).click();
driver.findElement(By.linkText("Core")).click();
driver.findElement(By.linkText("Log")).click();
new Select(driver.findElement(By.id("mediator.log.log_level"))).selectByVisibleText("Custom");
driver.findElement(By.linkText("Add Property")).click();
driver.findElement(By.id("propertyName0")).clear();
driver.findElement(By.id("propertyName0")).sendKeys("MESSAGE");
new Select(driver.findElement(By.id("propertyTypeSelection0"))).selectByVisibleText("Expression");
driver.findElement(By.id("propertyValue0")).clear();
driver.findElement(By.id("propertyValue0")).sendKeys("$func:message");
driver.findElement(By.cssSelector("input.button")).click();
driver.findElement(By.linkText("Add Parameter")).click();
driver.findElement(By.id("templatePropertyName0")).clear();
driver.findElement(By.id("templatePropertyName0")).sendKeys("message");
driver.findElement(By.id("saveButton")).click();
By editButton = By.linkText("Edit");
Assert.assertTrue(isElementPresent(editButton),
"New template creation failed, unable to find the created template");
//edit and save the template
driver.findElement(editButton).click();
driver.findElement(By.linkText("switch to source view")).click();
driver.findElement(By.linkText("switch to design view")).click();
driver.findElement(By.linkText("Log")).click();
driver.findElement(By.id("propertyName0")).clear();
driver.findElement(By.id("propertyName0")).sendKeys("GREETING_MESSAGE");
driver.findElement(By.id("saveButton")).click();
Assert.assertTrue(isElementPresent(editButton), "Unable to find the edited template entry.");
//delete the create template
driver.findElement(By.linkText("Delete")).click();
driver.findElement(By.xpath("/html/body/div[3]/div[2]/button[1]")).click();
Assert.assertFalse(isElementPresent(editButton), "Deleted template entry is still available.");
driver.close();
}
public static By locateByLinkText(final String linkText) {
return By.linkText(linkText);
}
public Exact(String text) {
super(By.linkText(text),
new XPathBy(".//a[descendant-or-self::text()[normalized(.)='%s']]", text));
}