Clase 9: Windows handler y Mapas

				
					package clase9;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Set;
import java.util.concurrent.TimeUnit;

public class MultipleWindowsTest {

    WebDriver driver;

    @BeforeMethod
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
        driver = new ChromeDriver();

    }

    @Test

    public void registrationTest() throws InterruptedException {
        driver.get("http://www.salesforce.com");
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        System.out.println("--> URL : " + driver.getTitle());
        System.out.println("--> URL : " + driver.getCurrentUrl());

        String mainTab = driver.getWindowHandle();
        System.out.println("pagina principal: " + mainTab);
        driver.findElement(By.xpath("//a[@href='/mx/form/signup/freetrial-sales-pe/?d=70130000000EqoP']")).click();

        Set<String> allTabs = driver.getWindowHandles();

        String registrationTab = "";
        for (String tab : allTabs) {
            System.out.println("---> " + tab);

            if (tab.equals(mainTab) == false) {
                driver.switchTo().window(tab);
                System.out.println("Cambiando de tab.....");
                registrationTab = tab;
            }
        }

        System.out.println("--> URL : " + driver.getTitle());
        System.out.println("--> URL : " + driver.getCurrentUrl());

        driver.findElement(By.name("UserFirstName")).sendKeys("Jhon");
        driver.findElement(By.name("UserLastName")).sendKeys("Cowen");
        driver.findElement(By.name("UserTitle")).sendKeys("QA");

        driver.switchTo().window(mainTab);
        Thread.sleep(2000);
        driver.switchTo().window(registrationTab);
        Thread.sleep(2000);
        driver.switchTo().window(mainTab);
        Thread.sleep(2000);
        driver.switchTo().window(registrationTab);
        Thread.sleep(2000);

    }


    @Test
    public void multipleTabsTest() {
        driver.get("http://www.naukri.com");
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);

        String mainWindow = driver.getWindowHandle();

        Set<String> allWindows = driver.getWindowHandles();

        System.out.println("TITULO! " + driver.getTitle());
        System.out.println("URL! " + driver.getCurrentUrl());
        System.out.println(allWindows.size());
        System.out.println("Main Window --->  " + mainWindow);
        for (String window : allWindows) {
            System.out.println("a window: " + window);
            if (window.equals(mainWindow) == false) {
                driver.switchTo().window(window);
                System.out.println("closing window: " + window);
                driver.close();
            }
        }

    }
}

				
			
				
					package clase9;

import org.testng.annotations.Test;

import java.util.HashMap;

public class EjemploMapas {

    //   < clave , valor >
    //    <111 , Ana Lopez>
    //   <12341234, Juan Andres>
    //   <2323, Rodrigo Perez>

    //ORDEN = tiempo que se demora en encontrar a un elemento
    // LISTA de N elementos, el tiempo puede llegar a ser N
    // MAPA de N elementos, el tiempo de encontrar un elemento es 1

    @Test
    public void mapasTest(){
        HashMap<Integer, String> mapaPersonas = new HashMap<>();
        mapaPersonas.put(111, "Analia Lopez");
        mapaPersonas.put(123, "Ana Rodriguez");
        mapaPersonas.put(222, "Juan Perez");

        System.out.println(mapaPersonas.get(12333));

    }

}