Advertisement
Raizen

Untitled

Jun 6th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. package com.example.tests;
  2.  
  3. import java.util.regex.Pattern;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.concurrent.TimeUnit;
  8. import org.junit.*;
  9. import static org.junit.Assert.*;
  10. import static org.hamcrest.CoreMatchers.*;
  11. import org.openqa.selenium.*;
  12. import org.openqa.selenium.chrome.ChromeDriver;
  13. import org.openqa.selenium.firefox.FirefoxDriver;
  14. import org.openqa.selenium.ie.InternetExplorerDriver;
  15. import org.openqa.selenium.support.ui.Select;
  16.  
  17. public class DataTables {
  18.   private WebDriver driver;
  19.   private String baseUrl;
  20.   private boolean acceptNextAlert = true;
  21.   private StringBuffer verificationErrors = new StringBuffer();
  22.  
  23.   @Before
  24.   public void setUp() throws Exception {
  25.     System.setProperty("webdriver.ie.driver", "C:\\Users\\mpastana\\workspace4\\v1.3\\driver\\IEDriverServer.exe");
  26.     driver = new InternetExplorerDriver();
  27.     baseUrl = "https://datatables.net/";
  28.     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  29.   }
  30.  
  31.   @Test
  32.   public void testCapacitacion() throws Exception {
  33.     driver.get(baseUrl);
  34.     List<String> tableList = new ArrayList();
  35.     List<String> tableName = new ArrayList();
  36.     new Select(driver.findElement(By.name("example_length"))).selectByVisibleText("100");
  37.     tableList = tablaResult(6);
  38.     capturarMayorValor(tableList);
  39.    
  40.     tableList = tablaResult(4);
  41.    
  42.     capturarEdad(tableList);
  43.    
  44.     tableList = tablaResult(5);
  45.    
  46.     capturarDate(tableList);
  47.     capturarNombres("Sidney");
  48.   }
  49.  
  50.   private void capturarNombres(String string) {
  51.       List<String> tableList = new ArrayList();
  52.       for (int y = 1; isElementPresent(By.xpath("//table[@id='example']//tr[" + y + "]//td[2]")); y++) {
  53.           if (driver.findElement(By.xpath("//table[@id='example']//tr[" + y + "]//td[3]")).getText().equals("Sidney"))
  54.               tableList.add(driver.findElement(By.xpath("//table[@id='example']//tr[" + y + "]//td[1]")).getText());
  55.       }    
  56.       for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
  57.         String name = (String) iterator.next();
  58.         System.out.println(name);
  59.     }
  60. }
  61.  
  62. public void capturarDate(List<String> tableList)
  63.   {
  64.       int count = 0;
  65.      for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
  66.         String value = (String) iterator.next();
  67.         if (value.substring(0, 4).equals("2012"))
  68.             count++;
  69.     }
  70.      System.out.println("La cuantidad de funcionarios de 2012 son " + count);
  71.   }
  72.  
  73.   public void capturarEdad(List<String> tableList)
  74.   {
  75.       int count = 0;
  76.      for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
  77.         String value = (String) iterator.next();
  78.         if (50 <= Integer.parseInt(value) && 60 >= Integer.parseInt(value))
  79.             count++;
  80.     }
  81.      System.out.println("La cuantidad de funcionarios con la edad entre 50 y 60 és " + count);
  82.   }
  83.  
  84.  
  85.   public void capturarMayorValor(List<String> tableList)
  86.   {
  87.       int max = 0;
  88.      for (Iterator iterator = tableList.iterator(); iterator.hasNext();) {
  89.         String value = (String) iterator.next();
  90.         value = value.replaceAll("\\$", "");
  91.         value = value.replaceAll(",", "");
  92.         if (max < Integer.parseInt(value))
  93.             max = Integer.parseInt(value);
  94.     }
  95.      System.out.println("El mayor salário és " + max);
  96.   }
  97.  
  98.   public List tablaResult(int tipo)
  99.   {
  100.       List<String> tableList = new ArrayList();
  101.           for (int y = 1; isElementPresent(By.xpath("//table[@id='example']//tr[" + y + "]//td[2]")); y++) {
  102.               tableList.add(driver.findElement(By.xpath("//table[@id='example']//tr[" + y + "]//td[ " + tipo + "]")).getText());
  103.           }
  104.     return tableList;
  105.   }
  106.  
  107.  
  108.   @After
  109.   public void tearDown() throws Exception {
  110.     driver.quit();
  111.     String verificationErrorString = verificationErrors.toString();
  112.     if (!"".equals(verificationErrorString)) {
  113.       fail(verificationErrorString);
  114.     }
  115.   }
  116.  
  117.   private boolean isElementPresent(By by) {
  118.     try {
  119.       driver.findElement(by);
  120.       return true;
  121.     } catch (NoSuchElementException e) {
  122.       return false;
  123.     }
  124.   }
  125.  
  126.   private boolean isAlertPresent() {
  127.     try {
  128.       driver.switchTo().alert();
  129.       return true;
  130.     } catch (NoAlertPresentException e) {
  131.       return false;
  132.     }
  133.   }
  134.  
  135.   private String closeAlertAndGetItsText() {
  136.     try {
  137.       Alert alert = driver.switchTo().alert();
  138.       String alertText = alert.getText();
  139.       if (acceptNextAlert) {
  140.         alert.accept();
  141.       } else {
  142.         alert.dismiss();
  143.       }
  144.       return alertText;
  145.     } finally {
  146.       acceptNextAlert = true;
  147.     }
  148.   }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement