NỘI DUNG BÀI HỌC

Cách xử lý Popup Window, Alert, iFrame

Alert (thông báo) trong Selenium là gì?

Một Alert trong Selenium là một hộp thông báo nhỏ xuất hiện trên màn hình để cung cấp cho người dùng một số thông tin hoặc thông báo. Nó thông báo cho người dùng một số thông tin hoặc lỗi cụ thể, yêu cầu quyền để thực hiện các tác vụ nhất định và nó cũng cung cấp các thông báo cảnh báo.

Dưới đây là một số loại cảnh báo:

1) Cảnh báo đơn giản

Lớp cảnh báo đơn giản trong Selenium hiển thị một số thông tin hoặc cảnh báo trên màn hình.

Xử lý cảnh báo & cửa sổ bật lên trong Selenium WebDriver

2) Cảnh báo nhắc nhở.

Thông báo nhắc nhở này yêu cầu một số đầu vào từ người dùng và trình duyệt web Selenium có thể nhập văn bản bằng cách sử dụng các phím gửi ("đầu vào….").

Xử lý cảnh báo & cửa sổ bật lên trong Selenium WebDriver

3) Cảnh báo xác nhận.

Cảnh báo xác nhận này yêu cầu quyền để thực hiện một số loại hoạt động.

Xử lý cảnh báo & cửa sổ bật lên trong Selenium WebDriver

 

Cách xử lý Alert trong Selenium WebDriver

Chèn thư viện cái:

import org.openqa.selenium.Alert;

Các hàm xử lý:

1) void dismiss() // To click on the 'Cancel' button of the alert.

driver.switchTo().alert().dismiss();

2) void accept() // To click on the 'OK' button of the alert.

driver.switchTo().alert().accept();

3) String getText() // To get message Text.

driver.switchTo().alert().getText();

4) void sendKeys(String stringToSend) // To input data on inputtext to alert box.

driver.switchTo().alert().sendKeys("Text");


Bắt đầu nào 

Step 1) Launch the web browser and open the site "http://demo.guru99.com/test/delete_customer.php "

Step 2) Enter Any Customer id.

Alert & Popup Handling in Selenium WebDriver

Step 3) After entering the customer ID, Click on the "Submit" button.

Alert & Popup Handling in Selenium WebDriver

Step 4) Accept/Dismiss the alert.

Alert & Popup Handling in Selenium WebDriver
Hoặc dạng này


Chạy code thực hành luôn cho nóng:

package AnhTester.HandleAlertPopupiFrame;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.NoAlertPresentException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;

public class HandleAlert {
	
	public static void main(String[] args) throws NoAlertPresentException,InterruptedException  {									
		System.setProperty("webdriver.chrome.driver", "resources/chromedriver/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();
        driver.get("http://demo.guru99.com/test/delete_customer.php");			
                            		   	      	
        driver.findElement(By.name("cusid")).sendKeys("53920");					
        driver.findElement(By.name("submit")).submit();			
        		
        // Switching to Alert
        Alert alert = driver.switchTo().alert();		
        		
        // Capturing alert message
        String alertMessage= driver.switchTo().alert().getText();		
        		
        // Displaying alert message
        System.out.println(alertMessage);	
        Thread.sleep(2000);
        		
        // Accepting alert		
        alert.accept();
        
        Thread.sleep(2000);
        driver.quit();
    }	
}

 

How to handle Pop-up window using Selenium Webdriver

Pop-up window là cái gì??


[Selenium Java] Lesson 13: Cách xử lý Alert, Popup Window và iFrame | Anh Tester
[Selenium Java] Lesson 13: Cách xử lý Alert, Popup Window và iFrame | Anh Tester
Thực hành thao tác nào:

Step 1) Launch the site.

Launch the browser and open the site " http://demo.guru99.com/popup.php "

Alert & Popup Handling in Selenium WebDriver

Step 2) Click on link "Click Here ".

When the user clicks on the " Click Here " link, new child window opens.

Alert & Popup Handling in Selenium WebDriver

Step 3) New Child Window opens.

A new window opens, ask the user to enter email id and submit the page.

Alert & Popup Handling in Selenium WebDriver

Step 4) Enter your email ID and submit.

Step 5) Display the Access Credentials on submitting the page.

Alert & Popup Handling in Selenium WebDriver

When you execute the code, you will see the child window is open in new tab.

  1. Close the Child window on which credentials are displayed.

Alert & Popup Handling in Selenium WebDriver

  1. Switch to the parent window.

Alert & Popup Handling in Selenium WebDriver


Code code code:

package AnhTester.HandleAlertPopupiFrame;

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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HandlePopupWindow {

	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver", "resources/chromedriver/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://demo.guru99.com/popup.php");
		Thread.sleep(2000);
		driver.findElement(By.xpath("//*[contains(@href,'popup.php')]")).click();
		
		// Lưu lại lớp window đầu tiên - mã ID hơi dài, in ra sẽ thấy :)
		String MainWindow = driver.getWindowHandle();
		System.out.println(MainWindow);
		
		// Get all new opened window.
		Set<String> windows = driver.getWindowHandles();
		
		//Set là một Collection không thể chứa các phần tử trùng lặp.
		//Cách duyệt từng phần tử không trùng lặp trong Collection (Set)
		for(String window : windows){
			System.out.println(window);
			if (!MainWindow.equalsIgnoreCase(window)) {
				//So sánh nếu thằng nào khác thằng Chính (đầu tiên) thì chuyển hướng qua nó mới thao tác được
				//Switch to Child window
				driver.switchTo().window(window);
				Thread.sleep(2000);
				System.out.println("Đã chuyển đến lớp Window con");
				driver.findElement(By.name("emailid")).sendKeys("abc@gmail.com");
				driver.findElement(By.name("btnLogin")).click();
				// Closing the Child Window.
				Thread.sleep(2000);
				driver.close();
			}
		}
		// Switching to Parent window (Main Window)
		driver.switchTo().window(MainWindow);
		System.out.println("Đã chuyển về lớp Window chính: "+ driver.getTitle());
		
		Thread.sleep(2000);
        driver.quit();
	}
}


Nói thêm về Set chút
Ví dụ có:  Set<String> setA = driver.getWindowHandles();


Duyệt Set với bộ lặp Iterator:

Iterator<String> iterator = setA.iterator();

while (iterator.hasNext()) {
      System.out.println((String) iterator.next());
}

 

Duyệt Set với vòng lặp for-each:
for (String element : setA) {
    System.out.println(element);
}


Cách xử lý iFrames trong Selenium Webdriver: switchTo ()

 

iFrame là gì?

iFrame là một trang web hoặc một khung nội tuyến được nhúng trong một trang web khác hoặc một tài liệu HTML được nhúng bên trong một tài liệu HTML khác. Khung nội tuyến thường được sử dụng để thêm nội dung từ các nguồn khác như quảng cáo vào một trang web.

Khung nội tuyến được xác định bằng thẻ <iframe>

Cách xác định iframe trên trang

[Selenium Java] Lesson 13: Cách xử lý Alert, Popup Window và iFrame | Anh Tester

Nếu dùng Chrome thì right click vào cái Messenger trang Anh Tester menu Contact sẽ thấy như hình

Hoặc dạng quảng cáo chèn vào các trang báo

[Selenium Java] Lesson 13: Cách xử lý Alert, Popup Window và iFrame | Anh Tester

Chúng ta có thể xác định tổng số iframe bằng cách sử dụng đoạn mã sau:

int sizeIFrame = driver.findElements(By.tagName("iframe")).Size();


Cách chuyển đến iFrame cụ thể

Chuyển sang khung theo Chỉ mục (thứ tự):
Giả sử nếu có 100 khung trong trang, chúng ta có thể chuyển sang khung trong Selenium bằng cách sử dụng chỉ mục.

driver.switchTo().frame(0);
driver.switchTo().frame(1);​

Chuyển sang khung theo Tên hoặc ID:

Tên và ID là các thuộc tính để xử lý các khung trong Selenium mà qua đó chúng ta có thể chuyển sang iframe.

driver.switchTo().frame("iframe1");
driver.switchTo().frame("id của phần tử");​
Chiến nào code code code

@Test(priority = 0)
    public void iFrame() throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.navigate().to("https://anhtester.com");
        sleep(2000);
        System.out.println("iframe total: "+driver.findElements(By.tagName("iframe")).size());
        sleep(1000);
        //----Switch to content of Messenger--------
        driver.switchTo().frame(0);
        System.out.println(driver.findElement(By.tagName("strong")).getText());

        //----Switch to icon of Messenger---------
        //Switch to Parent WindowHandle
        driver.switchTo().parentFrame();
        //Switch to iframe icon of Messenger
        driver.switchTo().frame(1);
        driver.findElement(By.tagName("svg")).click();

        Thread.sleep(2000);
        driver.quit();
    }​

Teacher

Teacher

Anh Tester

Software Quality Engineer

Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu

Cộng đồng Automation Testing Việt Nam:

🌱 Telegram Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 
Facebook Fanpage: Cộng đồng Automation Testing Việt Nam - Selenium
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

Chia sẻ khóa học lên trang

Bạn có thể đăng khóa học của chính bạn lên trang Anh Tester để kiếm tiền

Danh sách bài học