List of usage examples for org.openqa.selenium WebDriver findElements
@Override List<WebElement> findElements(By by);
From source file:com.github.codewrs.selenium.FindElementAdvanced.java
License:Open Source License
/** * Find all elements within the WebElement using the given mechanism. * Returns WebElement by matching with index of the WebElement. * @param driver WebDriver instance./*from w w w . j a v a2s .co m*/ * @param by The locating mechanism. * @param elementIndex Index of the WebElement to find. * @param wait Explicit Wait Time. * @return WebElement or null if nothing matches. */ protected WebElement findElementOnList(WebDriver driver, By by, int elementIndex, WebDriverWait wait) { try { List<WebElement> elementList = driver.findElements(by); wait.until(ExpectedConditions.visibilityOfAllElements(elementList)); int listSize = elementList.size(); System.out.println("The number of element counted is : " + listSize); if (listSize > 0) { int i = 0; for (WebElement ele : elementList) { if (elementIndex == i) { System.out.println("The returned WebElement text : " + ele.getText()); return ele; } i++; } } } catch (NoSuchElementException e) { System.out.println("No Element found."); } System.out.println("No Element found."); return null; }
From source file:com.github.codewrs.selenium.FindElementAdvanced.java
License:Open Source License
/** * Find all elements within the current page using the given mechanism. * Returns WebElement with the matching text. * @param driver WebDriver instance.//from www. j a v a 2 s.c o m * @param by The locating mechanism. * @param textToMatch Text to match with text of found WebElements. * @param wait Explicit Wait Time. * @return WebElement or null if nothing matches. */ protected WebElement findElementOnList(WebDriver driver, By by, String valueToMatch, WebDriverWait wait) { try { List<WebElement> elementList = driver.findElements(by); wait.until(ExpectedConditions.visibilityOfAllElements(elementList)); int listSize = elementList.size(); System.out.println("The number of element counted is : " + listSize); if (listSize > 0) { for (WebElement ele : elementList) { if (ele.getText() != null) { if (ele.getText().contentEquals(valueToMatch)) { System.out.println("The returned WebElement text : " + ele.getText()); return ele; } } } } } catch (NoSuchElementException e) { System.out.println("No Element found."); } System.out.println("No Element found."); return null; }
From source file:com.github.codewrs.selenium.FindElementAdvanced.java
License:Open Source License
/** * Find all elements within the current page using the given mechanism. * Returns WebElement by matching the value of the attribute. * @param driver WebDriver instance.//from www.ja v a 2 s . co m * @param by The locating mechanism. * @param attributeToSearch The attribute to locate. * @param valueToMatch Text to match with attribute's value. * @param wait Explicit Wait Time. * @return WebElement or null if nothing matches. */ protected WebElement findElementOnList(WebDriver driver, By by, String attributeToSearch, String valueToMatch, WebDriverWait wait) { try { List<WebElement> elementList = driver.findElements(by); wait.until(ExpectedConditions.visibilityOfAllElements(elementList)); int listSize = elementList.size(); System.out.println("The number of WebElements found : " + listSize); if (listSize > 0) { for (WebElement ele : elementList) { if (ele.getAttribute(attributeToSearch) != null) { if (ele.getAttribute(attributeToSearch).contentEquals(valueToMatch)) { System.out.println("The returned WebElement text : " + ele.getText()); return ele; } } } } } catch (NoSuchElementException e) { System.out.println("No Element found."); } System.out.println("No Element found."); return null; }
From source file:com.github.swt_release_fetcher.Main.java
License:Apache License
public static void main(String[] args) throws Exception { for (String arg : args) { if (arg.equals("--deploy")) { deployArtifacts = true;/*from w w w. j ava 2s . c o m*/ } if (arg.equals("--help")) { showHelp(); } if (arg.equals("--debug")) { debug = true; } if (arg.equals("--silent")) { silentMode = true; } } // the mirror we use for all following downloads String mirrorUrl = ""; // lightweight headless browser WebDriver driver = new HtmlUnitDriver(); // determine if the website has changed since our last visit // stop if no change was detected // Ignore this check if we just want to deploy if (!deployArtifacts) { SwtWebsite sw = new SwtWebsite(); try { if (!sw.hasChanged(driver, WEBSITE_URL)) { // exit if no change was detected printSilent("SWT website hasn't changed since our last visit. Stopping here."); driver.quit(); System.exit(0); } else { // proceed if the site has changed System.out .println("Page change detected! You may want to run the script in deploy mode again."); } } catch (IOException ioe) { System.out.println(ioe.getMessage()); } } // get SWT's main site printDebug("Parsing eclipse.org/swt to find a mirror"); driver.get(WEBSITE_URL); printDebug(WEBSITE_URL); // find the stable release branch link and hit it final List<WebElement> elements = driver.findElements(By.linkText("Linux")); final String deeplink = elements.get(0).getAttribute("href"); printDebug("deeplink: " + deeplink); driver.get(deeplink); // get the direct download link from the next page final WebElement directDownloadLink = driver.findElement(By.linkText("Direct link to file")); printDebug("direct download link: " + directDownloadLink.getAttribute("href")); // the direct link again redirects, here is our final download link! driver.get(directDownloadLink.getAttribute("href")); final String finalDownloadLink = driver.getCurrentUrl(); printDebug("final download link: " + finalDownloadLink); // Close the browser driver.quit(); // extract the mirror URL for all following downloads String[] foo = finalDownloadLink.split("\\/", 0); final String filename = foo[foo.length - 1]; mirrorUrl = (String) finalDownloadLink.subSequence(0, finalDownloadLink.length() - filename.length()); // debug output printDebug("full download url: " + finalDownloadLink); printDebug("mirror url: " + mirrorUrl); // determine current release name String[] releaseName = filename.split("-gtk-linux-x86.zip"); String versionName = releaseName[0].split("-")[1]; System.out.println("current swt version: " + versionName); // TODO move to properties file PackageInfo[] packages = { // Win32 new PackageInfo("win32-win32-x86.zip", "org.eclipse.swt.win32.win32.x86"), new PackageInfo("win32-win32-x86_64.zip", "org.eclipse.swt.win32.win32.x86_64"), // Linux new PackageInfo("gtk-linux-ppc.zip", "org.eclipse.swt.gtk.linux.ppc"), new PackageInfo("gtk-linux-ppc64.zip", "org.eclipse.swt.gtk.linux.ppc64"), new PackageInfo("gtk-linux-x86.zip", "org.eclipse.swt.gtk.linux.x86"), new PackageInfo("gtk-linux-x86_64.zip", "org.eclipse.swt.gtk.linux.x86_64"), new PackageInfo("gtk-linux-s390.zip", "org.eclipse.swt.gtk.linux.s390"), new PackageInfo("gtk-linux-s390x.zip", "org.eclipse.swt.gtk.linux.s390x"), // OSX new PackageInfo("cocoa-macosx.zip", "org.eclipse.swt.cocoa.macosx"), new PackageInfo("cocoa-macosx-x86_64.zip", "org.eclipse.swt.cocoa.macosx.x86_64"), // Additional platforms new PackageInfo("gtk-aix-ppc.zip", "org.eclipse.swt.gtk.aix.ppc"), new PackageInfo("gtk-aix-ppc64.zip", "org.eclipse.swt.gtk.aix.ppc64"), new PackageInfo("gtk-hpux-ia64.zip", "org.eclipse.swt.gtk.hpux.ia64"), new PackageInfo("gtk-solaris-sparc.zip", "org.eclipse.swt.gtk.solaris.sparc"), new PackageInfo("gtk-solaris-x86.zip", "org.eclipse.swt.gtk.solaris.x86") }; File downloadDir = new File("downloads"); if (!downloadDir.exists()) { downloadDir.mkdirs(); } for (PackageInfo pkg : packages) { final String zipFileName = releaseName[0] + "-" + pkg.zipName; final URL downloadUrl = new URL(mirrorUrl + zipFileName); final URL checksumUrl = new URL(mirrorUrl + "checksum/" + zipFileName + ".md5"); System.out.print("* Downloading " + pkg.zipName + " ... "); Artifact artifact = new Artifact(new File(downloadDir, zipFileName), versionName, pkg.artifactId); artifact.downloadAndValidate(downloadUrl, checksumUrl); if (deployArtifacts) { artifact.deploy(); } } }
From source file:com.google.appengine.tck.login.UserLogin.java
License:Open Source License
public void login(@Observes EventContext<Before> event) throws Exception { Before before = event.getEvent();/* ww w . j a va 2 s. c om*/ UserIsLoggedIn userIsLoggedIn = null; if (before.getTestMethod().isAnnotationPresent(UserIsLoggedIn.class)) { userIsLoggedIn = before.getTestMethod().getAnnotation(UserIsLoggedIn.class); } else if (before.getTestClass().isAnnotationPresent(UserIsLoggedIn.class)) { userIsLoggedIn = before.getTestClass().getAnnotation(UserIsLoggedIn.class); } if (userIsLoggedIn != null) { final URI baseUri = getBaseURI(before.getTestMethod()); final WebDriver driver = createWebDriver(); try { driver.manage().deleteAllCookies(); driver.navigate().to(baseUri + USER_LOGIN_SERVLET_PATH + "?location=" + URLEncoder.encode(userIsLoggedIn.location(), "UTF-8")); // did we navigate to this requested page, or did we get redirected/forwarded to login already List<WebElement> loginUrlElts = driver.findElements(By.id("login-url")); if (loginUrlElts.size() > 0) { String loginURL = loginUrlElts.get(0).getText(); // check if (isInternalLink(loginURL)) { loginURL = baseUri + loginURL; } // go-to login page driver.navigate().to(loginURL); } // find custom login handler, if exists LoginHandler loginHandler = TestBase.instance(getClass(), LoginHandler.class); if (loginHandler == null) { loginHandler = new DefaultLoginHandler(); } loginHandler.login(driver, new UserLoginContext(userIsLoggedIn)); // copy cookies Set<Cookie> cookies = driver.manage().getCookies(); for (Cookie cookie : cookies) { ModulesApi.addCookie(cookie.getName(), cookie.getValue()); } } finally { driver.close(); } } event.proceed(); }
From source file:com.google.caja.plugin.BrowserTestCase.java
License:Apache License
/** * Do what should be done with the browser. */// w w w . j a v a 2s . c om protected String driveBrowser(final WebDriver driver) { // long timeout: something we're doing is leading to huge unpredictable // slowdowns in random test startup; perhaps we're holding onto a lot of ram // and we're losing on swapping/gc time. unclear. countdown(10000, 200, new Countdown() { @Override public String toString() { return "startup"; } public int run() { List<WebElement> readyElements = driver.findElements(By.className("readytotest")); return readyElements.size() == 0 ? 1 : 0; } }); // 4s because test-domado-dom-events has non-click tests that can block // for a nontrivial amount of time, so our clicks aren't necessarily // processed right away. countdown(4000, 200, new Countdown() { private List<WebElement> clickingList = null; @Override public String toString() { return "clicking done (Remaining elements = " + renderElements(clickingList) + ")"; } public int run() { clickingList = driver.findElements(By.xpath("//*[contains(@class,'clickme')]/*")); for (WebElement e : clickingList) { // TODO(felix8a): webdriver fails if e has been removed e.click(); } return clickingList.size(); } }); // override point waitForCompletion(driver); // check the title of the document String title = driver.getTitle(); assertTrue("The title shows " + title, title.contains("all tests passed")); return title; }
From source file:com.google.caja.plugin.BrowserTestCase.java
License:Apache License
/** * After startup and clicking is done, wait an appropriate amount of time * for tests to pass./* www . j av a 2 s.c o m*/ */ protected void waitForCompletion(final WebDriver driver) { countdown(waitForCompletionTimeout(), 200, new Countdown() { private List<WebElement> waitingList = null; @Override public String toString() { return "completion (Remaining elements = " + renderElements(waitingList) + ")"; } public int run() { // TODO(felix8a): this used to check for just class "waiting", but now // "waiting" is redundant and should be removed. waitingList = driver.findElements(By.xpath("//*[contains(@class,'testcontainer')" + " and not(contains(@class,'done'))" + " and not(contains(@class,'manual'))]")); return waitingList.size(); } }); }
From source file:com.google.caja.plugin.DomitaTest.java
License:Apache License
void exerciseFirefox(String pageName) { //System.setProperty("webdriver.firefox.bin", "/usr/bin/firefox"); WebDriver driver = new FirefoxDriver(); driver.get("http://localhost:8000/" + "ant-lib/com/google/caja/plugin/" + pageName); int clickRounds = 0; List<WebElement> clickingList = null; for (; clickRounds < clickRoundLimit; clickRounds++) { clickingList = driver.findElements(By.xpath("//*[contains(@class,'clickme')]/*")); if (clickingList.size() == 0) { break; }/* w w w .j av a2 s .c om*/ for (WebElement e : clickingList) { e.click(); } } assertTrue("Too many click rounds. " + "Remaining elements = " + renderElements(clickingList), clickRounds < clickRoundLimit); int waitRounds = 0; List<WebElement> waitingList = null; for (; waitRounds < waitRoundLimit; waitRounds++) { waitingList = driver.findElements(By.xpath("//*[contains(@class,'waiting')]")); if (waitingList.size() == 0) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { } } assertTrue("Too many wait rounds. " + "Remaining elements = " + renderElements(waitingList), waitRounds < waitRoundLimit); // check the title of the document String title = driver.getTitle(); assertTrue("The title shows " + title, title.contains("all tests passed")); driver.quit(); }
From source file:com.google.caja.plugin.ThirdPartyBrowserTest.java
License:Apache License
/** * For QUnit-based tests, read QUnit's status text to determine if progress is * being made./*from w w w . j a v a2 s. com*/ */ @Override protected void waitForCompletion(final WebDriver driver) { final String testResultId = "qunit-testresult-caja-guest-0___"; if (driver.findElements(By.id(testResultId)).size() == 0) { // Not a QUnit test case; use default behavior. super.waitForCompletion(driver); return; } // Let it run as long as the report div's text is changing WebElement statusElement = driver.findElement(By.id(testResultId)); String currentStatus = statusElement.getText(); String lastStatus = null; // Check every second. // If the text starts with "Tests completed", then we're done. // If the text has changed, reset the time limit. int limit = 30; // tries for (int chances = limit; chances > 0; --chances) { if (currentStatus.startsWith("Tests completed")) { break; } try { Thread.sleep(1000); } catch (InterruptedException e) { // Keep trying } statusElement = driver.findElement(By.id(testResultId)); lastStatus = currentStatus; currentStatus = statusElement.getText(); if (!lastStatus.equals(currentStatus)) { chances = limit; } } }
From source file:com.hotwire.selenium.desktop.us.helpcenter.HelpCenterPage.java
License:Open Source License
public HelpCenterPage(WebDriver webdriver) { super(webdriver); if (webdriver.findElements(By.xpath("//meta[@name='pageName']")).size() > 0) { // Meta tag for page name exists. LOGGER.info("Page name meta tag exists. Checking if old help center page."); new WebDriverWait(getWebDriver(), 5).until(waitForExpectedPageName(webdriver)); } else {/*from w ww . j ava2 s.c om*/ LOGGER.info("Page name meta tag missing. Checking if new help center page."); new WebDriverWait(getWebDriver(), 5) .until(new VisibilityOf(By.cssSelector(".homeContent .breadcrumbs li a"))); } }