我们为Chrome,Firefox和Safari创建了扩展程序,我们希望使用Selenium测试扩展程序。我们有一个Ubuntu服务器,我在其中安装了Firefox和Chrome(使用ChromeDriver)。测试工作正常,但问题是Chrome窗口太小,并且由于窗口的大小,Chrome中的许多测试都失败了。使用Firefox,窗口很好,测试不会失败。如何使窗口尺寸更大?我们希望窗口大小为1920x1080。这是我们的代码:
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测试代码:
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窗口: Chrome窗口:
解决方案:
我删除了self.driver.maximize_window()
,而是添加了以下行:
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"]))
结果:
使用Chrome:Window size: width = 1919px, height = 1079px.
使用Firefox:Window size: width = 1920px, height = 1080px.
测试现在通过了。
答案 0 :(得分:15)
对于Chromejust添加之前&#34; webdriver.Chrome&#34; INIT:
chrome_options.add_argument("--window-size=1920x1080")
适用于所有浏览器:
有一个官方的selenium python绑定: 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)
或
driver.maximize_window()
或使用javascript
driver.execute_script("window.resizeTo(1920,1080)")
答案 1 :(得分:0)
driver.set_window_size(width, height)
如果您不知道宽度和高度,请使用How Big Is My Browser网站。