NexGenration

Selenium practice

Dec 18th, 2021 (edited)
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. package test.java;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import org.junit.After;
  6. import org.junit.AfterClass;
  7. import org.junit.Before;
  8. import org.junit.BeforeClass;
  9. import org.junit.Test;
  10. import org.openqa.selenium.By;
  11. import org.openqa.selenium.WebDriver;
  12. import org.openqa.selenium.chrome.ChromeDriver;
  13.  
  14. public class Demo {
  15.  
  16.     private WebDriver driver;
  17.     String url = "http://the-internet.herokuapp.com";
  18.    
  19.     @Before
  20.     public void setup() {
  21.         String driverPath = "D:\\Favorites\\Documents\\Programming\\Java\\eclipse-workspace\\chromedriver.exe";
  22.         System.setProperty("webdriver.chrome.driver", driverPath);
  23.         driver = new ChromeDriver();
  24.         driver.get(url);
  25.     }
  26.  
  27.     @Test
  28.     public void test() throws InterruptedException {
  29.         //Wait until page title is available and retrieve it, then print it
  30.         String pageTitle = "";
  31.         do {
  32.             pageTitle = driver.getTitle();
  33.             Thread.sleep(10);
  34.         } while (pageTitle == "");
  35.         System.out.println("Page title: " + pageTitle);
  36.        
  37.         //Click on "Form Authentication"
  38.         driver.findElement(By.linkText("Form Authentication")).click();
  39.        
  40.         //Wait until page header is available and retrieve it, then print it
  41.         String headerString = "";
  42.         do {
  43.             headerString = driver.findElement(By.tagName("h2")).getText();
  44.             Thread.sleep(10);
  45.         } while (headerString == "");
  46.         System.out.println(headerString);
  47.        
  48.         //Enter credentials and click "login"
  49.         driver.findElement(By.id("username")).sendKeys("tomsmith");
  50.         driver.findElement(By.id("password")).sendKeys("SuperSecretPassword!");
  51.         driver.findElement(By.xpath("//button[@type='submit]")).click();
  52.        
  53.         //Wait until page header says "You logged into a secure area!", then print "Login Successful"
  54.         String successString = "";
  55.         do {
  56.             successString = driver.findElement(By.id("flash")).getText();
  57.             Thread.sleep(10);
  58.         } while (successString != "You logged into a secure area!");
  59.         System.out.println("Login Successful");
  60.     }
  61.  
  62.     @After
  63.     public void after() {
  64.         //Close the browser
  65.         driver.close();
  66.     }
  67.  
  68.     @AfterClass
  69.     public static void afterClass() {
  70.         System.out.println("Inside after class method.");
  71.     }
  72. }
  73.  
Add Comment
Please, Sign In to add comment