Advertisement
Atanasov_88

Untitled

Jul 12th, 2016
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. package bg.pragmatic.lecture13mvn.waits;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4.  
  5. import org.junit.AfterClass;
  6. import org.junit.BeforeClass;
  7. import org.junit.Test;
  8. import org.openqa.selenium.Alert;
  9. import org.openqa.selenium.By;
  10. import org.openqa.selenium.NoAlertPresentException;
  11. import org.openqa.selenium.WebDriver;
  12. import org.openqa.selenium.WebElement;
  13. import org.openqa.selenium.firefox.FirefoxDriver;
  14. import org.openqa.selenium.security.Credentials;
  15. import org.openqa.selenium.security.UserAndPassword;
  16.  
  17. public class AlertsTest {
  18.    
  19.     static WebDriver driver;
  20.    
  21.     @BeforeClass
  22.     public static void setUp()
  23.     {
  24.         driver = new FirefoxDriver();
  25.         driver.get("http://dl.dropbox.com/u/55228056/Alerts.html");
  26.     }
  27.    
  28.     @Test
  29.     public void testSimpleAlert()
  30.     {
  31.         //Clicking button will show a simple Alert with OK Button
  32.         WebElement button = driver.findElement(By.id("simple"));
  33.         button.click();
  34.        
  35.         try {
  36.            
  37.             //Get the Alert
  38.             Alert alert = driver.switchTo().alert();
  39.  
  40.             //Get the Text displayed on Alert using getText() method of Alert class
  41.             String textOnAlert = alert.getText();
  42.            
  43.             //Click OK button, by calling accept() method of Alert Class
  44.             alert.accept();
  45.            
  46.             //Verify Alert displayed correct message to user
  47.             assertEquals("Hello! I am an alert box!",textOnAlert);
  48.            
  49.         } catch (NoAlertPresentException e) {
  50.             e.printStackTrace();
  51.         }
  52.     }
  53.    
  54.     @Test
  55.     public void testConfirmAccept()
  56.     {
  57.         //Clicking button will show a Confirmation Alert with OK and Cancel Button
  58.         WebElement button = driver.findElement(By.id("confirm"));
  59.         button.click();
  60.        
  61.         try {
  62.        
  63.             //Get the Alert
  64.             Alert alert = driver.switchTo().alert();
  65.            
  66.             //Click OK button, by calling accept() method of Alert Class
  67.             alert.accept();
  68.            
  69.             //Verify Page displays correct message on Accept
  70.             WebElement message = driver.findElement(By.id("demo"));
  71.             assertEquals("You Accepted Alert!", message.getText());
  72.            
  73.         } catch (NoAlertPresentException e) {
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.    
  78.     @Test
  79.     public void testConfirmDismiss()
  80.     {
  81.         //Clicking button will show a Confirmation Alert with OK and Cancel Button
  82.         WebElement button = driver.findElement(By.id("confirm"));
  83.         button.click();
  84.        
  85.         try {
  86.            
  87.             //Get the Alert
  88.             Alert alert = driver.switchTo().alert();
  89.            
  90.             //Click Cancel button, by calling dismiss() method of Alert Class
  91.             alert.dismiss();
  92.  
  93.             //Verify Page displays correct message on Dismiss
  94.             WebElement message = driver.findElement(By.id("demo"));
  95.             assertEquals("You Dismissed Alert!", message.getText());
  96.            
  97.         } catch (NoAlertPresentException e) {
  98.             e.printStackTrace();
  99.         }
  100.     }
  101.    
  102.     @Test
  103.     public void testPrompt()
  104.     {
  105.        
  106.        
  107.        
  108.         //Clicking button will show a Prompt Alert asking user to enter
  109.         //value/text with OK and Cancel Button
  110.         WebElement button = driver.findElement(By.id("prompt"));
  111.         button.click();
  112.        
  113.        
  114.         try {
  115.            
  116.             //Get the Alert
  117.             Alert alert = driver.switchTo().alert();
  118.            
  119.            
  120.  
  121.             //Example of HTTP authentication(which is NOT part of this test but, as requested during the course to leave it as comment)
  122.             //Credentials creds = new UserAndPassword("milen", "parola");
  123.             //alert.authenticateUsing(creds);
  124.            
  125.             //Enter some value on Prompt by calling sendKeys() method of Alert Class
  126.             alert.sendKeys("Foo");
  127.            
  128.             //Click OK button, by calling accept() method of Alert Class
  129.             alert.accept();
  130.            
  131.             //Verify Page displays message with value entered in Prompt
  132.             WebElement message = driver.findElement(By.id("prompt_demo"));
  133.             assertEquals("Hello Foo! How are you today?", message.getText());
  134.             //WaitTool.waitForElementRefresh(driver, By.id("bla"), 20);
  135.         } catch (NoAlertPresentException e) {
  136.             e.printStackTrace();
  137.         }
  138.     }
  139.    
  140.     @AfterClass
  141.     public static void tearDown()
  142.     {
  143.         driver.quit();
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement