[Java – Webdriver 02] – Thiết lập cố định chiều rộng và chiều cao của trình duyệt

Nếu bạn muốn thiết lập trình duyệt (cố định chiều rộng và chiều cao) khi chạy automation, thì Selenium WebDriver hỗ trợ thay đổi kích cỡ và mở rộng tối đa cửa sổ trình duyệt từ các API (import gói org.openqa.selenium.Dimension).

thiết lập trình duyệt

Khởi tạo một biến size với chiều rộng và chiều cao để thiết lập trình duyệt như trong source demo:

Dimension size = new Dimension(1024,768);

Thiết lập trình duyệt thay đổi kích thước cửa sổ hiện tại với tỉ lệ (1024,768), kích thước ở đây do bạn tự điều chỉnh theo ý muốn

driver.manage().window().setSize(size);

Thiết lập trình duyệt thay đổi kích thước cửa sổ hiện tại với tỉ lệ lớn nhất (kích thước sẽ tùy thuộc vào độ phân giải của máy hiện tại)

driver.manage().window().maximize();


Source demo:

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;

public class WebDriver01_SizeBrowser {

@Test
public void openBrowserwithGivenDimension1() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to(“http://www.yopmail.com/”);
Dimension size = new Dimension(320, 480);
driver.manage().window().setSize(size);
}

@Test
public void openBrowserwithGivenDimension2() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to(“https://www.chatwork.com”);
Dimension size = new Dimension(640, 960);
driver.manage().window().setSize(size);
}

@Test
public void openBrowserwithGivenDimension3() {
WebDriver driver = new FirefoxDriver();
driver.navigate().to(“http://prntscr.com”);
driver.manage().window().maximize();
}
}


Video Demo: