Advertisement
Guest User

OfficialCharts

a guest
Feb 3rd, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package com.charttest.webdriver;
  2.  
  3. import org.junit.AfterClass;
  4. import org.junit.BeforeClass;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.junit.runners.Parameterized;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  12.  
  13. import java.util.*;
  14. import static junit.framework.Assert.assertTrue;
  15.  
  16. @RunWith(Parameterized.class)
  17. public class OfficialChartsTest_Revised
  18.  
  19. {
  20.     private String chartYear;
  21.  
  22.     private static WebDriver driver;
  23.     public static String urlBase = "http://www.officialcharts.com/archive-chart/_/1/";
  24.  
  25.     @BeforeClass
  26.     public static void createDriver() {
  27.         driver = new HtmlUnitDriver();
  28.     }
  29.  
  30.     public OfficialChartsTest_Revised(String chartYear) {
  31.         this.chartYear = chartYear;
  32.     }
  33.  
  34.     @Parameterized.Parameters(name="{0} chart archive")
  35.     public static Collection chartYears() {
  36.         return Arrays.asList(new Object[][] {
  37.                 {"1960"}, {"1961"}, {"1962"}, {"1963"}, {"1964"}, {"1965"}, {"1966"},{"1967"},{"1968"},{"1969"},
  38.                 {"1970"}, {"1971"}, {"1972"}, {"1973"}, {"1974"}, {"1975"}, {"1976"},{"1977"},{"1978"},{"1979"},
  39.                 {"1980"}, {"1981"}, {"1982"}, {"1983"}, {"1984"}, {"1985"}, {"1986"},{"1987"},{"1988"},{"1989"},
  40.                 {"1990"}, {"1991"}, {"1992"}, {"1993"}, {"1994"}, {"1995"}, {"1996"},{"1997"},{"1998"},{"1999"},
  41.                 {"2000"}, {"2001"}, {"2002"}, {"2003"}, {"2004"}, {"2005"}, {"2006"},{"2007"},{"2008"},{"2009"},
  42.                 {"2010"}, {"2011"}, {"2012"}, {"2013"}, {"2014"}, {"2015"}
  43.         });
  44.  
  45.     }
  46.  
  47.     @Test
  48.     public void GetChartsForYear()
  49.     {
  50.         driver.navigate().to(urlBase + chartYear);
  51.         WebElement archiveTable = driver.findElement(By.cssSelector(".archive"));
  52.  
  53.         List<WebElement> chartLinks;
  54.         chartLinks = archiveTable.findElements(By.cssSelector("td[class='links']"));
  55.         Set<String> links = new HashSet<String>();
  56.  
  57.         for (WebElement e: chartLinks)
  58.         {
  59.             links.add(e.findElement(By.tagName("a")).getAttribute("href"));
  60.         }
  61.  
  62.         for (String thisLink: links)
  63.         {
  64.             CheckChartPage(thisLink);
  65.         }
  66.        
  67.     }
  68.  
  69.     public void CheckChartPage(String chartUrl)
  70.     {
  71.         driver.navigate().to(chartUrl);
  72.  
  73.         // Get last element from URL... magic numbers, could have used a split!
  74.         String chartDate = chartUrl.substring(48,58);
  75.  
  76.         assertTrue("Chart for " + chartDate + " not found", driver.getTitle().startsWith(chartDate));
  77.     }
  78.  
  79.     @AfterClass
  80.     public static void quitDriver() {
  81.         driver.quit();
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement