Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.9 kB
7
Indexable
Never
@Test
	public void TC_02_More_Than_2_Window_Tabs() throws InterruptedException {
		
		// Thằng cha:
		driver.get("https://automationfc.github.io/basic-form/index.html");
		String githubID = driver.getWindowHandle();
	
		//Click vào link tiki:
		driver.findElement(By.xpath("//a[text()='GOOGLE']")).click();
		Thread.sleep(10);
		//Switch sang trang tiki:
		switchToWindowByTitle("GOOGLE");
		System.out.println(driver.getCurrentUrl());
		System.out.println(driver.getTitle());

		driver.findElement(By.name("q")).sendKeys("Selenium");
		driver.findElement(By.name("q")).sendKeys(Keys.ENTER);

		Thread.sleep(10);
		
		
		//Switch sang trang Selenium Webdriver
		switchToWindowByTitle("Selenium WebDriver");

		
		//Click vào link Facebook mở trang facebook
		//Nhưng thực tế driver vẫn ở trang Selenium Webdriver
	//	driver.findElement(By.xpath("//a[text()='FACEBOOK']")).click();
	//	Thread.sleep(10);
	
		//Switch sang trang facebook:
		
	//	switchToWindowByTitle("Facebook – log in or sign up");
	
//		Thread.sleep(10);
	//	driver.findElement(By.id("email")).sendKeys("nam@yopmail.com");
	//	driver.findElement(By.id("pass")).sendKeys("123456789");

	//	Thread.sleep(10);
		//switch sang trang selenium
	//	switchToWindowByTitle("Selenium WebDriver");
		
	}

	private void switchToWindowByTitle(String expectedTitle) {
	Set<String> allIDs = driver.getWindowHandles();
	for (String id : allIDs) {
		driver.switchTo().window(id);
		String actualTitle = driver.getTitle();
		if (actualTitle.equals(expectedTitle))  {
			break;
		}
	}	
	}

	public void closeALlWindowWithoutExpectedID(String expectedID)  {
		Set<String> allIDs = driver.getWindowHandles();

		for (String id: allIDs) {
			if (!id.equals(expectedID)) {
				driver.switchTo().window(id);
				driver.close();
			}
		}
		driver.switchTo().window(expectedID);
			
	}