Guest User

Untitled

a guest
Jun 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package com.amazon.tests;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7.  
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.chrome.ChromeDriver;
  12. import org.testng.annotations.AfterClass;
  13. import org.testng.annotations.BeforeClass;
  14. import org.testng.annotations.Test;
  15.  
  16. public class Grabber {
  17. private WebDriver driver;
  18. private List<String> internalLinks = new ArrayList<String>();
  19. private Set<String> noDuplicates = new HashSet<>();
  20. private List<WebElement> linksOnPage = new ArrayList<>();
  21. private List<String> listOld = new ArrayList<>();
  22. private List<String> listNew = new ArrayList<>();
  23. private List<String> listTemp = new ArrayList<>();
  24.  
  25. @BeforeClass
  26. public void setup() {
  27. System.setProperty("webdriver.chrome.driver","C:/TEST/LIB/chromedriver.exe");
  28. driver = new ChromeDriver();
  29. }
  30.  
  31. @AfterClass
  32. public void teardown() {
  33. driver.close();
  34. }
  35.  
  36. @Test
  37. public void grabber()
  38. {
  39. String hrefvalue = null;
  40. boolean check = false;
  41.  
  42.  
  43. driver.get("https://www.redbullmobile.pl/");
  44. linksOnPage = driver.findElements(By.tagName("a"));
  45.  
  46.  
  47. for (int i = 0; i<linksOnPage.size(); i++)
  48. {
  49. hrefvalue = linksOnPage.get(i).getAttribute("href");
  50.  
  51. if(hrefvalue != null)
  52. {
  53. if(hrefvalue.contains("redbullmobile") && !hrefvalue.contains("facebook") && !hrefvalue.contains("redbullmobile.play.pl") && !hrefvalue.contains("redbullmobilecollect") && !hrefvalue.contains("fb.com") && !hrefvalue.contains(".pdf"))
  54. {
  55. noDuplicates.add(linksOnPage.get(i).getAttribute("href"));
  56. }
  57. }
  58. }
  59. listOld.addAll(noDuplicates);
  60. hrefvalue = null;
  61. linksOnPage.clear();
  62. noDuplicates.clear();
  63.  
  64.  
  65. do
  66. {
  67. listTemp.clear();
  68. for (int i = 0; i<listOld.size(); i++)
  69. {
  70. driver.get(listOld.get(i));
  71. linksOnPage = driver.findElements(By.tagName("a"));
  72. for (int i1 = 0; i1<linksOnPage.size(); i1++)
  73. {
  74. hrefvalue = linksOnPage.get(i1).getAttribute("href");
  75. if(hrefvalue != null)
  76. {
  77. if(hrefvalue.contains("redbullmobile") && !hrefvalue.contains("facebook") && !hrefvalue.contains("redbullmobile.play.pl") && !hrefvalue.contains("redbullmobilecollect") && !hrefvalue.contains("fb.com") && !hrefvalue.contains(".pdf"))
  78. {
  79. noDuplicates.add(linksOnPage.get(i1).getAttribute("href"));
  80. }
  81. }
  82. }
  83. }
  84. //When all links from listOld are visited compare listOld and listNew, every link listNew that's not on List is added to listTemp.
  85.  
  86. //Links from ListTemp and ListOld are added to ListFinal
  87. internalLinks.addAll(listOld);
  88. internalLinks.addAll(listTemp);
  89. //listOld and listNew are cleared, listTemp content is added to listOld
  90. listOld.clear();
  91. listNew.clear();
  92. listOld.addAll(listTemp);
  93. //Go back to 3rd step
  94. //Program end when at then end of 6th step listTemp = null (so simple do...while with check, check changes to true when at the end of 6th step listTemp = null)
  95. if (listTemp.isEmpty())
  96. {
  97. check = true;
  98. }
  99. }while(check);
  100. }
  101. }
Add Comment
Please, Sign In to add comment