mtsonkova

Untitled

Jul 20th, 2022 (edited)
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import io.github.bonigarcia.wdm.WebDriverManager;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. import org.openqa.selenium.WebElement;
  5. import org.openqa.selenium.firefox.FirefoxDriver;
  6. import org.openqa.selenium.support.ui.FluentWait;
  7. import org.openqa.selenium.support.ui.Wait;
  8. import org.testng.Assert;
  9. import org.testng.annotations.AfterTest;
  10. import org.testng.annotations.BeforeTest;
  11. import org.testng.annotations.Test;
  12.  
  13. import java.time.Duration;
  14. import java.util.List;
  15. import java.util.NoSuchElementException;
  16.  
  17. public class TelecomTests {
  18.     public static final String BASE_URL = "https://demo.guru99.com/telecom/";
  19.     WebDriver driver;
  20.  
  21.     @BeforeTest
  22.     public void setBasicTestProperties() {
  23.         WebDriverManager.firefoxdriver().setup();
  24.         driver = new FirefoxDriver();
  25.         driver.get(BASE_URL);
  26.         Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
  27.                 .withTimeout(Duration.ofSeconds(10))
  28.                 .pollingEvery(Duration.ofSeconds(5))
  29.                 .ignoring(NoSuchElementException.class);
  30.     }
  31.  
  32.    @Test
  33.     public void printTextOfAllLinksOnHomePage() {
  34.         List<WebElement> linksOnPage = getAllLinksFromHomePage();
  35.         StringBuilder builder = new StringBuilder();
  36.  
  37.         for(WebElement link: linksOnPage) {
  38.             String attribute = link.getAttribute("href");
  39.             String text = link.getText();
  40.            builder.append(attribute + " -> " + text);
  41.            builder.append(System.lineSeparator());
  42.         }
  43.        
  44.         System.out.println(builder.toString());
  45.     }
  46.  
  47.   @Test
  48.     public void printTextOfAllLinksOnHomePage2() {
  49.         List<WebElement> linksOnPage = getAllLinksFromHomePage();
  50.  
  51.         for(WebElement link: linksOnPage) {
  52.             System.out.println(link.getText());
  53.         }
  54.     }
  55.  
  56.     @AfterTest
  57.     public void performActivitiesAfterEachTest() {
  58.         driver.quit();
  59.     }
  60.  
  61. // methods to be used in test cases
  62.     public  List<WebElement> getAllLinksFromHomePage() {
  63.         List<WebElement> homePageLinks = driver.findElements(By.tagName("a"));
  64.         return homePageLinks;
  65.     }
  66. }
  67.  
Add Comment
Please, Sign In to add comment