NexGenration

Selenium LEX 1

Dec 2nd, 2021
4,767
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.test;
  2. import java.util.Calendar;
  3. import java.util.Set;
  4. import java.util.concurrent.TimeUnit;
  5. import org.junit.After;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.WebDriver;
  10. import org.openqa.selenium.WebElement;
  11. import org.openqa.selenium.firefox.FirefoxDriver;
  12. import org.openqa.selenium.support.ui.Select;
  13. public class Demo008_WindowHandles {
  14.     WebDriver driver;
  15.     String url = "http://localhost:8080/EDUBank/tellerLogin/";
  16.  
  17.     @Before
  18.     public void before() throws Exception {
  19.         //Set the key/value property according to the browser you are using.
  20.         System.setProperty("webdriver.gecko.driver",driverPath+"geckodriver.exe");
  21.                  
  22.         //Open browser instance
  23.         driver = new FirefoxDriver();
  24.                        
  25.         //Open the AUT
  26.         driver.get(url);
  27.        
  28.         //Declare an implicit wait which is bounded to WebDriver instance
  29.         driver.manage().timeouts().implicitlyWait(5000, TimeUnit.MILLISECONDS);
  30.            
  31.     }
  32.    
  33.     @Test
  34.     public void test() {
  35.         //Enter username
  36.         driver.findElement(By.id("tellerId")).sendKeys("T7302");
  37.        
  38.         //Enter password
  39.         driver.findElement(By.id("password")).sendKeys("T7302*abc");
  40.        
  41.         //Click on Login button
  42.         driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/form/button")).click();
  43.        
  44.         //Enter name
  45.         driver.findElement(By.id("custName")).sendKeys("John");
  46.                
  47.         //Enter EmailId
  48.         driver.findElement(By.id("email")).sendKeys("johnwin"+Calendar.getInstance().getTime().getTime()+"@gmail.com");;
  49.        
  50.         //Enter dateofBirth
  51.         driver.findElement(By.id("dob")).sendKeys("21/10/1987");
  52.        
  53.         ///Find the security Question dropdown list and store it as a WebElement
  54.         WebElement sectyq = driver.findElement(By.id("secQId"));
  55.        
  56.         //Pass the reference variable for sectyq as a parameter for the Select class
  57.         Select secqdd = new Select(sectyq);
  58.        
  59.         //Use the select reference variable for selecting any option using index/value/visible text approach
  60.         secqdd.selectByValue("210002");
  61.        
  62.         //Enter the security answer
  63.         driver.findElement(By.id("secAns")).sendKeys("Chicago");
  64.        
  65.         //Click on Add Customer
  66.         driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/div[1]/form/button[1]")).click();
  67.        
  68.         //Click on Download Customer Details
  69.         driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/div[4]/a")).click();
  70.        
  71.         // Fetch the number of opened windows
  72.         Set<String> windowHandles = driver.getWindowHandles();
  73.         System.out.println("Number of opened windows: " + windowHandles.size());
  74.        
  75.         Integer temp=0;
  76.        
  77.         String msg=null;
  78.        
  79.         //Iterate through all the available windows
  80.         for (String string : windowHandles) {
  81.             //Switch between windows using the string reference variable
  82.             driver.switchTo().window(string);
  83.            
  84.             //Fetch the url of the page post successful switch
  85.             String title = driver.getTitle();
  86.            
  87.             //check whether the url post switch is the desired page
  88.             if (!title.equals("Teller Home")) {
  89.                 temp = 1;
  90.                 msg="Window found.";
  91.                 break;
  92.             } else {
  93.                 temp = 0;
  94.             }
  95.         }
  96.  
  97.         if (temp == 1) {
  98.            
  99.             System.out.println(msg);
  100.        
  101.             //Find some element from the switched page to verify that the switch is successful
  102.             WebElement Bankname = driver.findElement(By.xpath("/html/body/div/span[1]"));
  103.             System.out.println(Bankname.getText()+" Bank");
  104.         }
  105.         else if (temp == 0) {
  106.             System.out.println("Desired Window not found.");
  107.         }
  108.     }
  109.     @After
  110.     public void after() throws Exception{
  111.         //Close the driver
  112.         driver.close();
  113.     }
  114. }
  115.  
RAW Paste Data