Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.tests;
- import java.util.regex.Pattern;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import java.util.concurrent.TimeUnit;
- import org.junit.*;
- import static org.junit.Assert.*;
- import static org.hamcrest.CoreMatchers.*;
- import org.openqa.selenium.*;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.ie.InternetExplorerDriver;
- import org.openqa.selenium.support.ui.Select;
- public class DataTables {
- private WebDriver driver;
- private String baseUrl;
- private boolean acceptNextAlert = true;
- private StringBuffer verificationErrors = new StringBuffer();
- @Before
- public void setUp() throws Exception {
- System.setProperty("webdriver.ie.driver", "C:\\Users\\mpastana\\workspace4\\v1.3\\driver\\IEDriverServer.exe");
- driver = new InternetExplorerDriver();
- baseUrl = "https://datatables.net/";
- driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
- }
- @Test
- public void testCapacitacion() throws Exception {
- driver.get(baseUrl);
- List<String> tableList = new ArrayList();
- List<String> tableName = new ArrayList();
- new Select(driver.findElement(By.name("example_length"))).selectByVisibleText("100");
- tableList = tablaResult(6);
- capturarMayorValor(tableList);
- tableList = tablaResult(4);
- capturarEdad(tableList);
- tableList = tablaResult(5);
- capturarDate(tableList);
- capturarNombres("Sidney");
- }
- private void capturarNombres(String string) {
- List<String> tableList = new ArrayList();
- for (int y = 1; isElementPresent(By.xpath("//table[@id='example']//tr[" + y + "]//td[2]")); y++) {
- if (driver.findElement(By.xpath("//table[@id='example']//tr[" + y + "]//td[3]")).getText().equals("Sidney"))
- tableList.add(driver.findElement(By.xpath("//table[@id='example']//tr[" + y + "]//td[1]")).getText());
- }
- for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
- String name = (String) iterator.next();
- System.out.println(name);
- }
- }
- public void capturarDate(List<String> tableList)
- {
- int count = 0;
- for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
- String value = (String) iterator.next();
- if (value.substring(0, 4).equals("2012"))
- count++;
- }
- System.out.println("La cuantidad de funcionarios de 2012 son " + count);
- }
- public void capturarEdad(List<String> tableList)
- {
- int count = 0;
- for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
- String value = (String) iterator.next();
- if (50 <= Integer.parseInt(value) && 60 >= Integer.parseInt(value))
- count++;
- }
- System.out.println("La cuantidad de funcionarios con la edad entre 50 y 60 és " + count);
- }
- public void capturarMayorValor(List<String> tableList)
- {
- int max = 0;
- for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
- String value = (String) iterator.next();
- value = value.replaceAll("\\$", "");
- value = value.replaceAll(",", "");
- if (max < Integer.parseInt(value))
- max = Integer.parseInt(value);
- }
- System.out.println("El mayor salário és " + max);
- }
- public List tablaResult(int tipo)
- {
- List<String> tableList = new ArrayList();
- for (int y = 1; isElementPresent(By.xpath("//table[@id='example']//tr[" + y + "]//td[2]")); y++) {
- tableList.add(driver.findElement(By.xpath("//table[@id='example']//tr[" + y + "]//td[ " + tipo + "]")).getText());
- }
- return tableList;
- }
- @After
- public void tearDown() throws Exception {
- driver.quit();
- String verificationErrorString = verificationErrors.toString();
- if (!"".equals(verificationErrorString)) {
- fail(verificationErrorString);
- }
- }
- private boolean isElementPresent(By by) {
- try {
- driver.findElement(by);
- return true;
- } catch (NoSuchElementException e) {
- return false;
- }
- }
- private boolean isAlertPresent() {
- try {
- driver.switchTo().alert();
- return true;
- } catch (NoAlertPresentException e) {
- return false;
- }
- }
- private String closeAlertAndGetItsText() {
- try {
- Alert alert = driver.switchTo().alert();
- String alertText = alert.getText();
- if (acceptNextAlert) {
- alert.accept();
- } else {
- alert.dismiss();
- }
- return alertText;
- } finally {
- acceptNextAlert = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement