List of usage examples for org.openqa.selenium WebDriver findElements
@Override List<WebElement> findElements(By by);
From source file:org.xwiki.test.ui.po.editor.ObjectEditPage.java
License:Open Source License
public ObjectEditPane addObjectFromInlineLink(String className) { final By objectsLocator = By.cssSelector("[id='xclass_" + className + "'] .xobject"); final int initialObjectCount = getDriver().findElements(objectsLocator).size(); getDriver().findElement(By.cssSelector("[id='add_xobject_" + className + "'] .xobject-add-control")) .click();//from w ww . j av a 2 s . c o m // Make sure we wait for the element to appear since there's no page refresh. getUtil().waitUntilCondition(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver driver) { return Boolean.valueOf(driver.findElements(objectsLocator).size() > initialObjectCount); } }); List<ObjectEditPane> objects = getObjectsOfClass(className); return objects.get(objects.size() - 1); }
From source file:org.xwiki.test.ui.po.editor.UserPicker.java
License:Open Source License
/** * Waits until the list of suggestions disappears. * //from w ww .j a v a 2 s .com * @param timeout how long to wait, in seconds * @return this */ private UserPicker waitForSuggestionsToDisappear(int timeout) { int previousTimeout = getUtil().getTimeout(); getUtil().setTimeout(timeout); try { getUtil().waitUntilCondition(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver driver) { return driver.findElements(By.className("suggestItems")).size() == 0; } }); } finally { getUtil().setTimeout(previousTimeout); } return this; }
From source file:org.xwiki.test.ui.po.LiveTableElement.java
License:Open Source License
/** * @since 3.2M3/* w w w.j a v a2s . c o m*/ */ public void waitUntilRowCountGreaterThan(final int minimalExpectedRowCount) { final By by = By.xpath("//tbody[@id = '" + this.livetableId + "-display']//tr"); getUtil().waitUntilCondition(new ExpectedCondition<Boolean>() { @Override public Boolean apply(WebDriver driver) { return driver.findElements(by).size() >= minimalExpectedRowCount; } }); }
From source file:org.xwiki.test.ui.TestUtils.java
License:Open Source License
public List<WebElement> findElementsWithoutWaiting(WebDriver driver, By by) { // Temporarily remove the implicit wait on the driver since we're doing our own waits... driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS); try {//from ww w. ja v a2 s .com return driver.findElements(by); } finally { setDriverImplicitWait(driver); } }
From source file:org.zanata.page.languages.LanguagePage.java
License:Open Source License
public LanguagePage joinLanguageTeam() { log.info("Click Join"); clickElement(joinLanguageTeamButton); // we need to wait for this join to finish before returning the page waitForAMoment().until(new Function<WebDriver, Boolean>() { @Override/*from ww w .j a v a2 s . com*/ public Boolean apply(WebDriver driver) { return driver.findElements(joinLanguageTeamButton).isEmpty(); } }); return new LanguagePage(getDriver()); }
From source file:page.Crawl.java
License:Open Source License
public static boolean run(Node father, String url, WebDriver driver) { System.out.println(" > crawling url: " + url); try {/*from w w w.j a v a 2 s .c om*/ driver.get(url); try { Thread.sleep(Globals.SLEEP_WAITING_FOR_PAGE_LOAD); } catch (Exception ex) { System.out.println("InterruptedException in crawl.run()"); ex.printStackTrace(); System.exit(-1); } List<WebElement> list = null; int old_size = 0; int new_size = 0; int loop = 1; do { //SCROLL TILL THE END OF THE PAGE old_size = new_size; int THRESHOLD = loop * Globals.BASE_SCROLL_STEP; for (int i = 0; i < THRESHOLD; i++) { JavascriptExecutor jse = (JavascriptExecutor) driver; jse.executeScript("window.scrollTo(0,document.body.scrollHeight)", ""); try { Thread.sleep(Globals.SLEEP_BEFORE_SCROLL_MS); } catch (Exception ex) { System.out.println("InterruptedException in crawl.run()"); ex.printStackTrace(); System.exit(-1); } } list = driver .findElements(By.xpath("//div[starts-with(@class, '" + Globals.MAIN_CLASS_NAME + "')]")); new_size = list.size(); //System.out.println("old: "+old_size+", new: "+new_size+", loop: "+loop); loop++; } while (old_size != new_size); if (new_size == 0) { return false; } for (WebElement element : list) { String date = ""; String id = ""; List<WebElement> ids = element .findElements(By.xpath(".//a[contains(@class, '" + Globals.ID_CLASS_NAME + "')]")); for (WebElement id_element : ids) { String hovercard = id_element.getAttribute("data-hovercard"); id = hovercard.replace(Globals.HOVERCARD_ID, ""); //String href = id_element.getAttribute("href"); //System.out.println("user: "+hovercard); //System.out.println("page: "+href); } List<WebElement> dates = element .findElements(By.xpath(".//abbr[contains(@class, '" + Globals.DATE_CLASS + "')]")); for (WebElement id_date : dates) { date = id_date.getAttribute("title"); //System.out.println("date: "+date); } if (id.equalsIgnoreCase("")) { System.out.println("ERRORE: id nullo per url " + url); return false; } Node current = new Node(id, date, father); List<WebElement> shared = element .findElements(By.xpath(".//a[contains(@class, '" + Globals.SHARE_CLASS + "')]")); for (WebElement id_shared : shared) { String href = id_shared.getAttribute("href"); //System.out.println("shares: "+href); //String text = id_shared.getText(); //System.out.println(text //DFS //OPEN NEW TAB WebElement body = driver.findElement(By.tagName("body")); body.sendKeys(Keys.CONTROL + "t"); try { Thread.sleep(Globals.SLEEP_WAITING_FOR_PAGE_LOAD); } catch (Exception ex) { System.out.println("InterruptedException in crawl.run()"); ex.printStackTrace(); System.exit(-1); } //driver.switchTo().window(tabs.get(1)); if (!run(current, href, driver)) { body.sendKeys(Keys.CONTROL + "w"); return false; } ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); driver.switchTo().window(tabs.get(tabs.size() - 1)); } father.addChildren(current); } } catch (Exception e) { System.out.println("WARNING: unable to crawl url: " + url); e.printStackTrace(); } //CLOSE CURRENT TAB WebElement body = driver.findElement(By.tagName("body")); body.sendKeys(Keys.CONTROL + "w"); return true; }
From source file:piecework.util.E2eTestHelper.java
License:Educational Community License
public static void fillForm(WebDriver driver, String[][] data) { if (driver == null || data == null) { return;/*from w ww. ja v a2 s.c o m*/ } //JavascriptExecutor jse = (JavascriptExecutor) driver; for (String[] e : data) { String k = e[0]; String v = e[1]; String byAttr = e.length > 2 ? e[2] : "name"; // default to ByName boolean found = false; for (int i = 0; i < 3; ++i) { try { // System.out.println(k + ", v=" + v + ", i="+ i); WebElement element = null; if (byAttr.equals("id")) { element = driver.findElement(By.id(k)); } else { List<WebElement> elements = driver.findElements(By.name(k)); if (elements.size() == 0) { element = driver.findElement(By.name(k)); // let driver throw exception } else if (elements.size() == 1) { element = elements.get(0); } else { for (int j = 0; j < elements.size(); ++j) { String tagName = elements.get(j).getTagName(); if (tagName.equals("input") || tagName.startsWith("text") || tagName.equals("select")) { element = elements.get(j); String t = element.getAttribute("type"); if (t.equals("hidden")) { element = element.findElement(By.xpath("../input[@type='text']")); } break; } } } } String tagName = element.getTagName(); String t = element.getAttribute("type").toLowerCase(); if (t != null) { t = t.toLowerCase(); } //System.out.println(k + "'s type =" + t); if (tagName.equals("input")) { if (t.equals("hidden")) { WebElement e1 = element.findElement(By.xpath("../input[@data-ng-change]")); e1.sendKeys(v); } else if (t.equals("checkbox") || t.equals("submit")) { element.click(); } else if (t.equals("radio")) { if (v != null && !v.isEmpty()) { WebElement el = element.findElement(By.xpath( "//input[@name='" + k + "' and @type='radio' and @value='" + v + "']")); el.click(); } else { element.click(); } } else if (t.equals("file")) { element.sendKeys(v); Thread.sleep(2000); // for file upload } else { // "text", "date", "datetime" etc. //element.click(); // need this for field with maskedinput (another mask package), but messed up date picker on chrome element.sendKeys(org.openqa.selenium.Keys.HOME); // need this for field with inputmask element.sendKeys(v); } } else if (tagName.startsWith("text")) { element.sendKeys(v); } else if (tagName.equals("select")) { if (v != null && !v.isEmpty()) { WebElement el = element.findElement(By.xpath(".//option[@value='" + v + "']")); el.click(); } else { WebElement el = element.findElement(By.xpath(".//option[1]")); el.click(); } } found = true; break; } catch (Exception ex) { System.out.println(k + ", val=" + v + ", exception =" + ex.toString()); try { Thread.sleep(1000); // wait a bit, then try again } catch (InterruptedException ex1) { } } } // inner loop assertTrue(found, "could not find element <" + k + ">"); } // outer loop }
From source file:PokemonGoMapValidator.RunComparison.java
public void comparison(String currentDirectory, WebDriver driver, String mapFile) throws IOException, InterruptedException { List<String> links = new ArrayList<>(); List<WebElement> extra = new ArrayList<>(); List<String> subjectList = new ArrayList<>(); List<String> attachmentList = new ArrayList<>(); WebElement formElement;// www . j av a 2 s . c om OpenURL openURL = new OpenURL(); String localPrtscFile = "semPokemongos.png"; String urlPrtscFile = "comPokemongos.png"; String comPokemongos = currentDirectory + "/prints/"; String semPokemongos = currentDirectory + "/prints/"; String comparisonPath = currentDirectory + "/prints/"; String comparisonFile = ""; String baseUrl = ""; int count = 0; int countTot = 0; int optionsAnimation = 1000; String responseCode = ""; //50*50 is the size of the squares to compare //much bigger and they start to fail ImageComparison imageComparison = new ImageComparison(50, 50, 0); //load data LoadMap carregarDados = new LoadMap(); carregarDados.LoadMap(currentDirectory, links, mapFile); if (links.size() > 0) { for (int i = 0; i < links.size(); i = i + 2) { if (links.get(i).length() > 0) { //validate directory File f = new File(currentDirectory + "/prints/" + links.get(i + 1) + "/"); if (!f.isDirectory()) { new File(currentDirectory + "/prints/" + links.get(i + 1) + "/").mkdir(); } if (f.isDirectory()) { //it is not necessary to clean the directory, but I'm doing it FileUtils.cleanDirectory(f); //validate url baseUrl = links.get(i); responseCode = openURL.openURL(driver, baseUrl); if ("200".equals(responseCode)) { //wait for some animations to run, like the red marker droping Thread.sleep(1000); //open the Options //wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Options']"))); extra = driver.findElements(By.xpath("//span[text()='Options']")); if (extra.size() > 0) { //this area runs if we define a zoom in or out //first it zooms in and then zooms out if (ZOOMINOROUT != 0) { //the red marker seems to be always present when latitude and longitude is used //when a town name is searched, sometimes the red marker is not shown //I previously was using the red marker, now I use de + and - buttons of the map extra = driver.findElements(By.xpath("//area")); if (extra.size() > 0) { //formElement = driver.findElement(By.xpath("//area")); //formElement.click(); if (ZOOMINOROUT < 0) { formElement = driver.findElement(By.xpath("//div[@title='Zoom out']")); for (int k = 1; k <= ZOOMINOROUT * (-1); k++) { //formElement.sendKeys(Keys.chord(Keys.SUBTRACT)); formElement.click(); } } if (ZOOMINOROUT > 0) { formElement = driver.findElement(By.xpath("//div[@title='Zoom in']")); for (int k = 1; k <= ZOOMINOROUT; k++) { //formElement.sendKeys(Keys.chord(Keys.ADD)); formElement.click(); } } } else { System.out .println("Google maps red marker not found. Zoom was not applied."); } } formElement = driver.findElement(By.xpath("//span[text()='Options']")); formElement.click(); Thread.sleep(optionsAnimation); //turn off all the options for (int j = 1; j < 7; j++) { if (driver.findElement(By.xpath( "//div[contains(@class, 'form-control') and contains(@class, 'switch-container')][" + j + "]/div/input")) .isSelected()) { driver.findElement(By.xpath( "//div[contains(@class, 'form-control') and contains(@class, 'switch-container')][" + j + "]/div/label")) .click(); } //Thread.sleep(250); if (j == 3) { j = j + 1; } } //close the options formElement = driver.findElement(By.xpath("//span[text()='Options']")); formElement.click(); Thread.sleep(optionsAnimation); //empty screenshot File baseFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(baseFile, new File(semPokemongos + links.get(i + 1) + "/" + localPrtscFile)); //open the options formElement = driver.findElement(By.xpath("//span[text()='Options']")); formElement.click(); Thread.sleep(optionsAnimation); //turn on the pokemongos option if (!driver.findElement(By.xpath( "//div[contains(@class, 'form-control') and contains(@class, 'switch-container')][1]/div/input")) .isSelected()) { driver.findElement(By.xpath( "//div[contains(@class, 'form-control') and contains(@class, 'switch-container')][1]/div/label")) .click(); Thread.sleep(250); } //close the options formElement = driver.findElement(By.xpath("//span[text()='Options']")); formElement.click(); //time for the elements to load Thread.sleep(LOADINGPOKEMONGOS); //take screenshot File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); // Now you can do whatever you need to do with it, for example copy somewhere FileUtils.copyFile(scrFile, new File(comPokemongos + links.get(i + 1) + "/" + urlPrtscFile)); String imgOriginal = semPokemongos + links.get(i + 1) + "/" + localPrtscFile; String imgToCompareWithOriginal = comPokemongos + links.get(i + 1) + "/" + urlPrtscFile; String imgOutputDifferences = comparisonPath + links.get(i + 1) + "/" + comparisonFile + links.get(i + 1) + ".jpg"; if (imageComparison.fuzzyEqual(imgOriginal, imgToCompareWithOriginal, imgOutputDifferences)) { System.out.println("Location without pokmon: " + links.get(i + 1) + " - " + LOADINGPOKEMONGOS + "ms"); subjectList.add("Location without pokmon: " + links.get(i + 1) + " - " + LOADINGPOKEMONGOS + "ms"); //to send by email the generated image, activate these two lines below //attachmentList.add(diferencasPath + links.get(i + 1) + "/"); //attachmentList.add(diferencasFile + links.get(i + 1) + ".jpg"); } else { //System.out.println("Images are not equal and that is good!" + links.get(i + 1)); count++; } countTot++; } else { System.out.println( "Map dimension might be to small and the Options menu isn't visible, or probably there is an error with the url: " + links.get(i) + " - Location: " + links.get(i + 1)); subjectList.add( "Map dimension might be to small and the Options menu isn't visible, or probably there is an error with the url: " + links.get(i) + " - Location: " + links.get(i + 1)); countTot++; } } else if ("408".equals(responseCode)) { System.out.println( "Timeout for the location: " + links.get(i + 1) + " - " + PAGELOADING + "ms"); subjectList.add( "Timeout for the location: " + links.get(i + 1) + " - " + PAGELOADING + "ms"); countTot++; } else { System.out.println( "Error opening the location: " + responseCode + " - " + links.get(i + 1)); subjectList .add("Error opening the location: " + responseCode + " - " + links.get(i + 1)); countTot++; } } else { System.out.println("Error opening folder: " + links.get(i + 1)); subjectList.add("Error opening folder: " + links.get(i + 1)); } } else { System.out.println("The url is empty. Line: " + i); subjectList.add("The url is empty. Line: " + i); } } subjectList.add("Total maps working: " + count); subjectList.add("Total maps tested : " + countTot); } else { System.out.println("No data was loaded!"); subjectList.add("No data was loaded!"); } //sends to console and log for (int i = 0; i < subjectList.size(); i++) { System.out.println(subjectList.get(i)); log.info(subjectList.get(i)); } //if email not configured, prints the message if (LOGIN.length() > 0) { System.out.println("Sending email..."); SendMail sendMail = new SendMail(); sendMail.sendMail(currentDirectory, subjectList, attachmentList); } else { System.out.println("Email not available. Check log file with subject."); } }
From source file:projectGutenbergSelenium.Gutenberg.java
public static void main(String[] args) throws InterruptedException { int maxTimer = 5; System.setProperty("webdriver.gecko.driver", "C:\\Program Files\\SeleniumDrivers\\geckodriver.exe"); System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\SeleniumDrivers\\chromedriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://localhost:49944"); //verify page is loaded (new WebDriverWait(driver, maxTimer)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { String title = d.findElement(By.xpath("//body//h1")).getText(); System.out.println("Text is: " + title); return "Gutenberg project".equals(title); }/*from w w w. j av a2 s .c o m*/ }); //Test GetBooksContainingCityMysql WebElement element = driver.findElement(By.id("titleAuthorWithCityTextBox")); element.sendKeys("Esbjerg"); WebElement element2 = driver.findElement(By.name("ctl02")); element2.click(); (new WebDriverWait(driver, maxTimer)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { int amount = d.findElements(By.xpath("//tbody/tr")).size(); return amount == 6; } }); expectedBooks.add(new ExpectedOutput("Denmark", "M. Pearson Thomson,")); expectedBooks.add(new ExpectedOutput("The 1990 CIA World Factbook", "M. United States. Central Intelligence Agency,")); expectedBooks.add( new ExpectedOutput("The 1994 CIA World Factbook", "United States Central Intelligence Agency,")); expectedBooks.add( new ExpectedOutput("The 1997 CIA World Factbook", "United States. Central Intelligence Agency.,")); expectedBooks.add( new ExpectedOutput("The 1998 CIA World Factbook", "United States. Central Intelligence Agency.,")); System.out.println(Compare(driver, 2)); System.out.println(Compare(driver, 3)); System.out.println(Compare(driver, 4)); System.out.println(Compare(driver, 5)); System.out.println(Compare(driver, 6)); //Test GetBooksContainingCityMongoDB element2 = driver.findElement(By.name("ctl03")); element2.click(); (new WebDriverWait(driver, maxTimer)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { int amount = d.findElements(By.xpath("//tbody/tr")).size(); System.out.println("List amount is: " + amount); return amount == 6; } }); expectedBooks.clear(); expectedBooks.add( new ExpectedOutput("The 1994 CIA World Factbook", "United States Central Intelligence Agency,")); expectedBooks.add( new ExpectedOutput("The 1997 CIA World Factbook", "United States. Central Intelligence Agency.,")); expectedBooks.add(new ExpectedOutput("Denmark", "M. Pearson Thomson,")); expectedBooks.add(new ExpectedOutput("The 1990 CIA World Factbook", "M. United States. Central Intelligence Agency,")); expectedBooks.add( new ExpectedOutput("The 1998 CIA World Factbook", "United States. Central Intelligence Agency.,")); System.out.println(Compare(driver, 2)); System.out.println(Compare(driver, 3)); System.out.println(Compare(driver, 4)); System.out.println(Compare(driver, 5)); System.out.println(Compare(driver, 6)); //Test GetCitiesInTitleMysql element = driver.findElement(By.id("mentionedInBookTextbox")); element.sendKeys("Jingle Bells"); element2 = driver.findElement(By.name("ctl04")); element2.click(); //TESTING //Test GetCitiesInTitleMongoDB element2 = driver.findElement(By.name("ctl05")); element2.click(); //Test GetCitiesWithAuthorMysql element = driver.findElement(By.id("citiesWithAuthorTextBox")); element.sendKeys("Helen Bannerman"); element2 = driver.findElement(By.name("ctl06")); element2.click(); //Test GetCitiesWithAuthorMongoDB element2 = driver.findElement(By.name("ctl07")); element2.click(); //Test GetBooksMentionedInAreaMysql element = driver.findElement(By.id("mentionedInAreaLatitudeBox")); element.sendKeys("36"); element2 = driver.findElement(By.id("mentionedInAreaLongitudeBox")); element2.sendKeys("43"); WebElement element3 = driver.findElement(By.name("ctl08")); element3.click(); (new WebDriverWait(driver, maxTimer)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { int amount = d.findElements(By.xpath("//tbody/tr")).size(); System.out.println("List amount is: " + amount); return amount == 6; } }); //Test GetBooksMentionedInAreaMongoDB }
From source file:projectGutenbergTest.GutenbergTest.java
@Test public void Test2() { WebElement element = driver.findElement(By.id("titleAuthorWithCityTextBox")); element.sendKeys("Esbjerg"); WebElement element2 = driver.findElement(By.name("ctl02")); element2.click();/*from ww w . jav a 2 s . c o m*/ (new WebDriverWait(driver, maxTimer)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { int amount = d.findElements(By.xpath("//tbody/tr")).size(); return amount == 6; } }); //Get from file later expectedBooks = new ArrayList<ExpectedOutput>(); expectedBooks.add(new ExpectedOutput("Denmark", "M. Pearson Thomson,")); expectedBooks.add(new ExpectedOutput("The 1990 CIA World Factbook", "M. United States. Central Intelligence Agency,")); expectedBooks.add( new ExpectedOutput("The 1994 CIA World Factbook", "United States Central Intelligence Agency,")); expectedBooks.add( new ExpectedOutput("The 1997 CIA World Factbook", "United States. Central Intelligence Agency.,")); expectedBooks.add( new ExpectedOutput("The 1998 CIA World Factbook", "United States. Central Intelligence Agency.,")); expectedBooks.get(0).title.equals(Compare(driver, 2).title); expectedBooks.get(1).title.equals(Compare(driver, 3).title); expectedBooks.get(2).title.equals(Compare(driver, 4).title); expectedBooks.get(3).title.equals(Compare(driver, 5).title); expectedBooks.get(4).title.equals(Compare(driver, 6).title); for (int i = 1; i < 8; i++) { driver.findElement(By.id("titleAuthorWithCityTextBox")).sendKeys(Keys.BACK_SPACE); } }