Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.61 KB | None | 0 0
  1. package org.example.testSelenium;
  2.  
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.WebDriver;
  5. import org.openqa.selenium.WebElement;
  6. import org.openqa.selenium.firefox.FirefoxDriver;
  7. import org.openqa.selenium.support.ui.ExpectedCondition;
  8. import org.openqa.selenium.support.ui.WebDriverWait;
  9.  
  10. public class App
  11. {
  12.     public static void main(String[] args) throws Exception{
  13.         System.setProperty("webdriver.gecko.driver", "/home/vivien/softs-dev/geckodriver");
  14.        
  15. //        test1();
  16. //        test2();
  17. //      test3();
  18.         test4();
  19. //      test7();
  20. //      test8();
  21.     }
  22.    
  23.     private static WebDriver getDriverWithoutCookie(String url){
  24.         WebDriver webDriver = new FirefoxDriver();
  25.         webDriver.get(url);
  26.        
  27.         webDriver.findElement(By.className("eu-cookie-compliance-default-button")).click();
  28.         return webDriver;
  29.     }
  30.    
  31.     private static void test1() {
  32.         WebDriver webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr");
  33.        
  34.         WebElement link =  webDriver.findElement(By.partialLinkText("Toutes les actualités".toUpperCase()));
  35.        
  36.         if(link != null)
  37.             System.out.println("Link found");
  38.         else
  39.             System.out.println("Link not found");
  40.        
  41.         webDriver.quit();
  42.     }
  43.    
  44.     private static void test2() {
  45.         WebDriver webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr");
  46.        
  47.         WebElement link =  webDriver.findElement(By.partialLinkText("Toutes les actualités".toUpperCase()));
  48.         link.click();
  49.        
  50.         webDriver.findElement(By.partialLinkText("Accueil".toUpperCase()));
  51.        
  52.         System.out.println("link found");
  53.        
  54.         webDriver.quit();
  55.     }
  56.    
  57.     private static void test3(){
  58.         WebDriver webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr");
  59.        
  60.         webDriver.findElement(
  61.             By.partialLinkText("Toutes les actualités".toUpperCase())
  62.         ).click();
  63.        
  64.         WebElement image = webDriver.findElement(By.xpath("//img[@alt='Accueil']"));
  65.         image.click();
  66.        
  67.         webDriver.findElement(By.partialLinkText("Toutes les actualités".toUpperCase()));
  68.        
  69.         System.out.println("Link found");
  70.        
  71.         webDriver.quit();
  72.     }
  73.    
  74.     private static void test4() {
  75.         WebDriver webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr/rechercher");
  76.        
  77.         WebElement searchBox = webDriver.findElement(By.id("edit-search-api-fulltext"));
  78.         searchBox.sendKeys("Donald Trump");
  79.        
  80.         webDriver.findElement(
  81.             By.xpath("//input[contains(@value,'Appliquer les filtres')]")
  82.         ).click();
  83.        
  84.        
  85.         webDriver.findElement(By.xpath("//*[contains(text(), 'Aucun résultat ne correspond à votre recherche')]"));
  86.         webDriver.quit();
  87.     }
  88.    
  89. //  private static void test5() {
  90. //      WebDriver webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr/formation/trouver-ma-formation");
  91. //     
  92. //      webDriver.findElements(By.tagName("input"))
  93. //  }
  94.    
  95.     private static void test7() {
  96.         WebDriver webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr");
  97.        
  98.         WebElement post = webDriver.findElements(By.className("actu_home_ctner_inner_cell1_titre")).get(0);
  99.         String title = post.getText();
  100.        
  101.         post.click();
  102.        
  103.         webDriver.findElement(By.xpath("//*[contains(text(), '" + title + "')]"));
  104.        
  105.         System.out.println("Text found");
  106.        
  107.         webDriver.quit();
  108.     }
  109.  
  110.     private static void test8() throws Exception {
  111.         WebDriver webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr");
  112.        
  113.         WebElement post = webDriver.findElements(By.className("actu_home_ctner_inner_cell1_titre")).get(1);
  114.         String title = post.getText();
  115.         WebElement link = post.findElement(By.xpath("./.."));
  116.         String linkUrl = link.getAttribute("href");
  117.         System.out.println("URL1 : " + linkUrl);
  118.         webDriver.quit();
  119.         webDriver = getDriverWithoutCookie("http://www.imt-atlantique.fr/fr/rechercher");
  120.         WebElement searchBox = webDriver.findElement(By.id("edit-search-api-fulltext"));
  121.         searchBox.sendKeys(title);
  122.        
  123.         webDriver.findElement(
  124.             By.xpath("//input[contains(@value,'Appliquer les filtres')]")
  125.         ).click();
  126.        
  127.         Boolean found = webDriver.findElements(By.tagName("a")).stream().anyMatch(a -> a.getAttribute("href").equals(linkUrl));
  128.        
  129.         if(!found)
  130.             throw new Exception("link not found");
  131.        
  132.         webDriver.quit();
  133.     }
  134.        
  135.     private static void example() {
  136.         // Create a new instance of the Firefox driver
  137.         // Notice that the remainder of the code relies on the interface,
  138.         // not the implementation.
  139.         WebDriver driver = new FirefoxDriver();
  140.  
  141.         // And now use this to visit Google
  142.         driver.get("http://www.google.com");
  143.         // Alternatively the same thing can be done like this
  144.         // driver.navigate().to("http://www.google.com");
  145.  
  146.         // Find the text input element by its name
  147.         WebElement element = driver.findElement(By.name("q"));
  148.  
  149.         // Enter something to search for
  150.         element.sendKeys("Cheese!");
  151.  
  152.         // Now submit the form. WebDriver will find the form for us from the element
  153.         element.submit();
  154.  
  155.         // Check the title of the page
  156.         System.out.println("Page title is: " + driver.getTitle());
  157.        
  158.         // Google's search is rendered dynamically with JavaScript.
  159.         // Wait for the page to load, timeout after 10 seconds
  160.         (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
  161.             public Boolean apply(WebDriver d) {
  162.                 return d.getTitle().toLowerCase().startsWith("cheese!");
  163.             }
  164.         });
  165.  
  166.         // Should see: "cheese! - Google Search"
  167.         System.out.println("Page title is: " + driver.getTitle());
  168.        
  169.         //Close the browser
  170.         driver.quit();
  171.        
  172.         System.out.println("Finished");
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement