public static String convertToCSSSelector(String type, String value) { if ("css selector".equals(type)) { return value; } if ("id".equals(type)) { // return "#" + value; // doesn't work for id starting with an int. return "[id='" + value + "']"; } if ("tag name".equals(type)) { return value; } if ("class name".equals(type)) { // detect composite class name if (isCompoundName(value)) { throw new InvalidSelectorException("Compound class names aren't allowed"); } return "." + value; } if ("class name".equals(type)) { return "." + value; } if ("name".equals(type)) { return "[name='" + value + "']"; } throw new WebDriverException("NI , selector type " + type); }
static boolean isInvalidSelectorRootCause(Throwable e) { if (e == null) { return false; } if (InvalidSelectorException.class.isAssignableFrom(e.getClass())) { return true; } if (String.valueOf(e.getMessage()).contains(INVALID_SELECTOR_PATTERN) || String .valueOf(e.getMessage()).contains("Locator Strategy \\w+ is not supported")) { return true; } return isInvalidSelectorRootCause(e.getCause()); }
/** * Select a Value based on visible text from List * * @param ddList * List to select from * @param val * Text to select */ public void select(WebElement ddList, String val) { Select option = new Select(ddList); int retry = 0; while (retry < MAX_RETRY) { try { option.selectByVisibleText(val); break; } catch (InvalidSelectorException ex) { browser.simpleWait(EXCEPTION_INTERVAL); } } log.trace("Selct Value : {} from List : {} ", val, ddList); }
/** * convertToBy: is a method that follows the general guidlines of Selenium * Element Locators as defined at: * http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.ISelenium.html * We use this method to simplify the "find element by" commands: * waitForElementPresent("#ID .CLASS div a[ATTRIBUTE=\"something\"]) or * waitForElementPresent("//div/") or waitForElementPresent("id=happy") or * just about any other combination... see comments in-line. * * @param selectorString * @return the By */ public static By convertToBy(String selectorString) { if (selectorString == null || selectorString.isEmpty()) { throw new InvalidSelectorException("Invalid parameter: " + selectorString); } String[] parts = selectorString.split("="); By findBy; if (parts.length > 1) { String getType = parts[0]; String key = selectorString.substring(getType.length() + 1); //Separate the type and the key on the '=' character if (getType.equalsIgnoreCase("css")) { //Check to see if it is CSS findBy = By.cssSelector(key); //"css=#something.somethingElse" should findBy By.cssSelector <id="something" class="somethingElse"> } else if (getType.equalsIgnoreCase("identifier") || getType.equalsIgnoreCase("id")) { //Check to see if it is an ID findBy = By.id(key); //"identifier=something" should findBy By.id <id="something"> } else if (getType.equalsIgnoreCase("link")) { //Check to see if it is a LINK findBy = By.linkText(key); //"link=something" should findBy By.linkText <a href="http://www.google.com/search?q=something">something</a> } else if (getType.equalsIgnoreCase("name")) { //Check to see if it is a NAME findBy = By.name(key); //"name=something" should findBy By.name <id="dude" name="something"> } else if (getType.equalsIgnoreCase("tag")) { //Check to see if it is a TAG findBy = By.tagName(key); //"tag=a" should findBy By.tagName <a href="http://www.google.com/search?q=something">something</a> } else if (getType.equalsIgnoreCase("xpath")) { //Check to see if it is an XPATH findBy = By.xpath(key); //"xpath=//div/article/div[4]/div[3]" should findBy By.xpath first div, first article in first div, 4 divs down inside article, 3 divs down inside the 4th div down } else { findBy = null; //If it is neither identified as CSS, an ID, a LINK, a NAME, a TAG, or an XPATH, then make it NULL } } else { findBy = null; //If it is empty, make it NULL } // if findBy is NULL, check to see if it is XPATH with a single "/" (more generic search), or check if it is an ID, // Class, Attribute, Tag, etc... and if not, assume it is CSS, try anyway... or run it as ID and fail. if (findBy == null) { //CHeck to see if findBy is a NULL, which means the string isn't identified, or it was empty. if (selectorString.startsWith("/")) { //Check to see if it is a default XPATH findBy = By.xpath(selectorString); //If it starts with "//" or "/" it is an XPATH (// = more specific, / = less specific) } else if (selectorString.startsWith("#") || selectorString.startsWith(".") || selectorString.startsWith("[")) { //Check to see if it is an ID (#), a CLASS (.) or an ATTRIBUTE ([) findBy = By.cssSelector(selectorString); //If any of these (CLASS, ID, ATTRIBUTE) then treat it like CSS } else if (!selectorString.isEmpty()) { //If it is NOT XPATH and NOT an ID, CLASS or ATTRIBUTE, then treat it like CSS, it may be a tag or other element findBy = By.cssSelector(selectorString); } else { //FAILSAFE: if it is not any of the above, use this to fail the selection. findBy = By.id(selectorString); } } return findBy; }