简体   繁体   中英

How do I resize the window in Chrome and Firefox when testing with Selenium?

We created extensions for Chrome, Firefox and Safari and we want to test our extensions with Selenium. We have an Ubuntu server and I installed Firefox and Chrome in it (with ChromeDriver). The tests work, but the problem is that the Chrome window is too small, and many tests fail in Chrome because of the size of the window. With Firefox the window is fine and the tests don't fail. How do I make the window size bigger? We want the window size to be 1920x1080. Here is our code:

run_daily_selenium_tests.sh :

#!/bin/bash

cd /home/ubuntu/selenium_tests

sudo Xvfb :99 -ac -screen 0 1920x1080x24 >/dev/null 2>&1 &
export DISPLAY=:99

today_date_hour=`TZ='Asia/Tel_Aviv' date +"%Y-%m-%d_%H"`
start_hour=`TZ='Asia/Tel_Aviv' date +"%H"`
selenium_test_prefix="/home/ubuntu/selenium_tests/tests/"
selenium_test_suffix=".py"
log_prefix="/home/ubuntu/logs/selenium_tests/"

run_selenium_test()
{
    selenium_test_file_name="${selenium_test_prefix}${file_name}${selenium_test_suffix}"
    log_suffix="_${file_name}.log"
    log_file_name="${log_prefix}${today_date_hour}${log_suffix}"
    python "$selenium_test_file_name" >"$log_file_name" 2>&1
    cat "$log_file_name" | mail -s "$test_name - `TZ='Asia/Tel_Aviv' date +"%Y-%m-%d %H:%M:%S"`" <my_email_address>
}

file_name="chrome_inbox_without_extension_test"
test_name="Chrome Inbox Without Extension Test"
run_selenium_test

file_name="chrome_inbox_1_with_extension_test"
test_name="Chrome Inbox 1 With Extension Test"
run_selenium_test

.....

sudo killall Xvfb >/dev/null 2>&1

if [ $start_hour = "08" ]; then
    cd /home/ubuntu
    /home/ubuntu/scripts/send_daily_report_html.sh
fi

Selenium tests code :

def get_default_caps(self):
    caps = {}
    caps['name'] = 'Base Selenium Test'
    caps['build'] = '1.0.1'
    if (self.browser == "chrome"):
        self.browser_version = '41.0.2272.118'
        caps['browser'] = 'Chrome'
    elif (self.browser == "firefox"):
        self.browser_version = '37.0.1'
        caps['browser'] = 'Firefox'
    elif (self.browser == "safari"):
        caps['browser'] = 'Safari'
        caps['os'] = 'OS X'
        self.browser_version = "{}.0".format(randint(7, 8))
        if (self.browser_version == "7.0"):
            caps['os_version'] = 'Mavericks'
        elif (self.browser_version == "8.0"):
            caps['os_version'] = 'Yosemite'
    caps['browser_version'] = self.browser_version
    caps['resolution'] = '1920x1080'
    caps['browserstack.debug'] = 'true'
    return caps

def start_selenium_webdriver(self, caps, chrome_options=None, firefox_profile=None):
    print("Starting test \"{}\" with {} {}, resolution {}.".format(caps['name'], caps['browser'], caps['browser_version'], caps['resolution']))
    if (self.browser == "chrome"):
        self.driver = webdriver.Chrome(chrome_options=chrome_options)
    elif (self.browser == "firefox"):
        self.driver = webdriver.Firefox(firefox_profile=firefox_profile)
    else:
        self.driver = webdriver.Remote(
            command_executor='http://username:password@hub.browserstack.com:80/wd/hub',
            desired_capabilities=caps
        )
    self.driver.implicitly_wait(time_to_wait=5)
    self.driver.maximize_window()

Firefox window:火狐窗口 Chrome window:铬窗

Solution :

I removed self.driver.maximize_window() and instead I added the following lines:

    self.driver.set_window_size(1920, 1080)
    size = self.driver.get_window_size()
    print("Window size: width = {}px, height = {}px.".format(size["width"], size["height"]))

The results :

With Chrome: Window size: width = 1919px, height = 1079px.

With Firefox: Window size: width = 1920px, height = 1080px.

The tests pass now.

For Chromejust add before "webdriver.Chrome" init:

chrome_options.add_argument("--window-size=1920x1080")

For all browsers:

There is an official selenium python binding for that: http://selenium.googlecode.com/git/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html#selenium.webdriver.remote.webdriver.WebDriver.set_window_size

driver.set_window_size(1920, 1080)

or

driver.maximize_window()

or with javascript

driver.execute_script("window.resizeTo(1920,1080)")
driver.set_window_size(width, height) 

如果您不知道宽度和高度,请使用我的浏览器网站有多大

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM