我们为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:[email protected]: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.
测试通过了
3条答案
按热度按时间8xiog9wr1#
对于Chrome,只需在“webdriver.Chrome”init之前添加:
对于所有浏览器:
有一个官方的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
或
或者使用JavaScript
unhi4e5o2#
如果您不知道宽度和高度,请使用How Big Is My Browser网站。
r6vfmomb3#
Chrome. *
-window-size
和window-size
也可以工作:微软Edge。*
-window-size
和window-size
也可以工作:火狐。*
-width
和-height
也可以工作,而width
和height
不工作:”我的回答解释得更多。