Cách thay đổi kích thước cửa sổ trình duyệt trong WebDriver

Làm cách nào để thay đổi kích thước cửa sổ trình duyệt với Selenium WebDriver? Ở đây, chúng ta xem xét ba cách khác nhau để có thể thay đổi kích thước cửa sổ trình duyệt trong WebDriver.

Bất cứ khi nào WebDriver khởi động trình duyệt, nó sẽ khởi động nó với các cài đặt mặc định. Đôi khi bắt buộc phải thay đổi kích thước cửa sổ trình duyệt, đặc biệt khi chúng tôi đang thử nghiệm các trang web đáp ứng vì chúng tôi cần kiểm tra xem các phần tử khác nhau trên trang hiển thị như thế nào khi chúng tôi thay đổi kích thước cửa sổ trình duyệt.

Webdriver có các phương pháp tiện lợi và các cách khác nhau có thể cho phép chúng ta thay đổi kích thước cửa sổ trình duyệt.




Thay đổi kích thước cửa sổ trình duyệt trong WebDriver

Java sử dụng Thứ nguyên

import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.Dimension; public class BrowserOperations {
WebDriver driver;
//this will open browser with default size
public void launchBrowser() {
driver = new FirefoxDriver();
}
public void resizeBrowser() {
Dimension d = new Dimension(800,480);
//Resize current window to the set dimension
driver.manage().window().setSize(d);
} }

Java sử dụng các tùy chọn của Chrome


import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; public class BrowserOperations {
public static void main(String[] args) {
System.setProperty('webdriver.chrome.driver';,
'/path/to/chromedriver');

ChromeOptions options = new ChromeOptions();
options.addArguments('window-size=800,480');

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);

//this will open chrome with set size
WebDriver driver = new ChromeDriver(capabilities);

driver.get('https://www.testingexcellence.com/');
} }

Nếu bạn muốn phóng to cửa sổ trình duyệt đến chiều rộng và chiều cao tối đa của màn hình, bạn chỉ cần gọi phương thức Maximum ()

Webdriver driver = new FirefoxDriver(); driver.manage().window().maximize();

Đọc thêm: