XVFB解決方案:
獨立硒服務器
下面是使用一個獨立的硒服務器工作解決方案主要來自www.tothenew.com。
安裝:
#Provision Java JDK
sudo apt-get install default-jdk -y
#Provision Protractor
sudo npm install protractor -g
#Webdriver update
sudo webdriver-manager update
#Exposing chromedriver
sudo ln /usr/lib/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.29 /usr/bin/chromedriver
sudo apt-get install libxpm4 libxrender1 libgtk2.0-0 libnss3 libgconf-2-4 -y
#Provision Google Chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt-get update
sudo apt-get install google-chrome-stable -y
sudo apt-get install xvfb gtk2-engines-pixbuf -y
sudo apt-get install xfonts-cyrillic xfonts-100dpi xfonts-75dpi xfonts-base xfonts-scalable -y
sudo apt-get install imagemagick x11-apps dbus-x11 -y
創建腳本:/etc/init.d/selenium
config.vm.provision "shell", inline: <<-SHELL
echo -n > /etc/init.d/selenium
echo '#!/bin/sh' >> /etc/init.d/selenium
echo 'Xvfb -ac :99 -screen 0 1280x1024x16 &' >> /etc/init.d/selenium
echo '#disown $1' >> /etc/init.d/selenium
echo 'export DISPLAY=:99' >> /etc/init.d/selenium
echo 'webdriver-manager start /dev/null 2>&1' >> /etc/init.d/selenium
sudo chmod +x /etc/init.d/selenium
SHELL
更新protactor.conf.js使用seleniumAddress:
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome'
},
seleniumAddress: 'http://localhost:4444/wd/hub',
directConnect: false,
baseUrl: 'http://localhost:4200/',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function() {}
},
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
},
onPrepare() {
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
};
要運行:
下面
- 運行/etc/init.d/selenium
- 運行NG E2E
基於從Sudharsan建議量角器啓動Selenium服務器
是另一種可行的解決方案。量角器在這種情況下啓動硒服務器。
安裝:
#Provision Java JDK
sudo apt-get install default-jdk -y
#Provision Protractor
sudo npm install protractor -g
#Webdriver update
sudo webdriver-manager update
#Exposing chromedriver
sudo ln /usr/lib/node_modules/protractor/node_modules/webdriver-manager/selenium/chromedriver_2.29 /usr/bin/chromedriver
#Provision Google Chrome
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list'
sudo apt-get update
sudo apt-get install google-chrome-stable -y
#Provision xvfb
sudo apt-get install -y xvfb
sudo apt-get install -y xfonts-100dpi xfonts-75dpi xfonts-cyrillic dbus-x11
量角器配置:
exports.config = {
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome'
},
directConnect: true,
//getPageTimeout: 60000,
//allScriptsTimeout: 60000,
baseUrl: 'http://localhost:4200/',
framework: 'jasmine2',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function() {}
},
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
},
onPrepare() {
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
}
};
要運行:
xvfb-run -a -e /dev/stdout -s "-screen 0 2920x2580x24" ng e2e
運行單元測試針對Chrome瀏覽器也適用
xvfb-run -a -e /dev/stdout -s "-screen 0 2920x2580x24" ng test --single-run
無頭鉻溶液:
鉻現在有一個無頭選項。 xvfb不再需要。 xvfb安裝可以省略。
因緣
browsers: ['ChromeCI'],
customLaunchers: {
ChromeCI: {
base: 'Chrome',
flags: ['--no-sandbox', '--headless', '--disable-gpu', '--remote-debugging-port=9222']
}
},
量角器
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['no-sandbox', 'headless', 'disable-gpu']
}
},
測試然後可以正常運行:
ng test
ng e2e
其他人來到這裏的信息 - 對於無頭鍍鉻解決方案,sendkeys可以不行。有一個已知問題sendkeys&headless chrome.https://bugs.chromium.org/p/chromedriver/issues/detail?id = 1772 – wizgot
@wizgot sendKeys問題(需要* nix上的xvfb)在chromedriver 2.31中修復,您可以從源代碼構建自己。目前它也正在爲通用版本準備。 – AlwaysLearning