How to Get Continuously changing data (example BitConin conversion rate and Stock Market) using Selenium - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Wednesday 11 April 2018

How to Get Continuously changing data (example BitConin conversion rate and Stock Market) using Selenium

Hi,
We can use selenium to Extract the Stock price or Bit coin conversion rate using Selenium

Key thing to note here is that
1.) The data will be continuous changing
2.)We need to keep the script running, so that it can scrape the data continuously without any miss

Sample Website
https://preev.com/

Before automating we need to observe and find out WebElement which gets updated whenever there is change

in my case that WebElement is available with Xpath ".//*[@id='numField']" here the attribute named "style" gets updated whenever there is Price Change

So we will use FluentWait of Selenium to continuously pool

Sample Code
 package com.neeraj.test.neeraj;  
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.NoSuchElementException;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
 import org.openqa.selenium.support.ui.FluentWait;  
 import org.testng.annotations.Test;  
 import com.google.common.base.Function;  
 public class testclass {  
      WebDriver driver=new FirefoxDriver();  
      public WebElement fluentWait(final By locator) {  
           //System.out.println("In Fluent Wait");  
           FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)  
                     .withTimeout(60, TimeUnit.SECONDS)  
                     .pollingEvery(1, TimeUnit.MICROSECONDS)  
                     .ignoring(NoSuchElementException.class);  
           WebElement foo = wait.until(new Function<WebDriver, WebElement>() {  
                public WebElement apply(WebDriver driver) {  
                     return driver.findElement(locator);  
                }  
           });  
           return foo;  
      };  
      @Test  
      public void BitCoinConverter(){  
           driver.get("https://preev.com");  
           boolean flag=false;  
           String oldPrice="";  
           String newPrice="";  
           while(flag==false){  
                oldPrice=driver.getTitle();  
                WebElement element = fluentWait(By.xpath(".//*[@id='numField']"));  
                String enabled = element.getAttribute("style");  
                if(enabled.contains("0.5")) { // if there is change in price we get change in style attribute as 0.5  
                     newPrice=driver.getTitle();  
                     if(oldPrice.equals(newPrice)){  
                          System.out.println("no change");  
                     }  
                     else{  
                          System.out.println("Change is there and new price is-->"+newPrice);  
                     }  
                }  
           }  
      }  
 }  

No comments:

Post a Comment