Advertisement
Guest User

Untitled

a guest
May 1st, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.40 KB | None | 0 0
  1. package ad;
  2.  
  3. import java.util.List;
  4. import org.openqa.selenium.By;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.WebElement;
  7. import org.openqa.selenium.firefox.FirefoxDriver;
  8.  
  9.  
  10. public class ff {
  11.  
  12.     static WebDriver driver = new FirefoxDriver();
  13.    
  14.    
  15.    
  16.     public static void main(String[] args) throws InterruptedException {
  17.                        
  18.         linkOnliner();
  19.         linkCatalog();
  20.         linkMobileCatalog();
  21.         searchAction();
  22.         chooseDevice();
  23.         comparePrices();
  24.         Integer[] prices = takePrices();
  25.         MinMaxResult result = foundMinMax(prices);
  26.        
  27.         System.out.println("Минимальная цена: " + result.getMin() + ", количество предложений = " + result.getCountMin());
  28.         System.out.println("Максимальная цена: " + result.getMax() + ", количество предложений = " + result.getCountMax());
  29.        
  30.         linkOnliner();        
  31.     }
  32.  
  33.     private static void linkOnliner(){
  34.         // And now use this to visit Onliner
  35.         driver.get("http://www.onliner.by");
  36.         }
  37.     private static void linkCatalog() {
  38.         //Catalog's locator
  39.       WebElement catalogElement = driver.findElement(By.cssSelector("span.b-main-navigation__text"));
  40.       catalogElement.click();
  41.     }
  42.     private static void linkMobileCatalog() {
  43. //      Mobile's device locator
  44.         WebElement mobileElement = driver.findElement(By.cssSelector("a.catalog-bar__link.catalog-bar__link_strong"));
  45.         mobileElement.click();     
  46.     }
  47.     private static void searchAction(){
  48. //      //QuickSearch;s locator
  49.       WebElement qSearch = driver.findElement(By.name("query"));
  50.       qSearch.sendKeys("Samsung Galaxy A3 (2016) Black [A310F]");
  51.       qSearch.submit();
  52.     }
  53.     private static void chooseDevice(){
  54.       //ATTENTION!! УВАГА!! AHTUNG - обход системы
  55.       //не получается найти ссылку    - какая-то муть с вэйтерами, надо спрашивать  
  56.         driver.get("http://catalog.onliner.by/mobile/samsung/a310fb");
  57.     }
  58.     private static void comparePrices(){
  59.         //CompareButton
  60.         WebElement compareWith = driver.findElement(By.linkText("Сравнить предложения"));
  61.         compareWith.click();
  62.     }
  63.     private static Integer[] takePrices(){
  64.         //Take Prices
  65.         List<WebElement> priceList = driver.findElements(By.className("js-currency-primary"));
  66.         Integer[] prices = new Integer[priceList.size()];
  67.      
  68.       //вытянул значения цен в массив
  69.       for (int i = 0; i < priceList.size(); i++) {
  70.         prices[i] = Integer.parseInt(priceList.get(i).getText().replaceAll("[^\\d]", "")); //all except 0-9                    
  71.       }
  72.       return prices;
  73.     }
  74.    
  75.     private static MinMaxResult foundMinMax (Integer[] prices){
  76.         int min = prices[0];
  77.         int max = prices[0];
  78.         int countMin = 0;
  79.         int countMax = 0;
  80.            
  81.         for (int j = 0; j < prices.length; j++) {              
  82.            
  83.             if (prices[j] < min){
  84.                 min = prices[j];
  85.                 countMin = 1;
  86.             }
  87.             else if (prices[j] == min)
  88.                 countMin++;            
  89.                
  90.             if (prices[j] > max){
  91.                 max = prices[j];
  92.                 countMax = 1;
  93.                
  94.             }
  95.             else if (prices[j] == max)
  96.                 countMax++;
  97.         }
  98.  
  99.         MinMaxResult result = new MinMaxResult();
  100.         result.setCountMax(countMax);
  101.         result.setCountMin(countMin);
  102.         result.setMax(max);
  103.         result.setMin(min);
  104.        
  105.         return result;
  106.  
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement