I want to use xvfb to open browser headless for my client-side performance test using jmeter. I am using selenium [Junit sampler] to run the test. How do I override display variable in jmeter so that it does not open browser.?
Option 1
If you're using Firefox you can utilize FirefoxBinary.setEnvironmentProperty() method
FirefoxBinary bin = new FirefoxBinary(new File("/opt/firefox28/firefox"));
bin.setEnvironmentProperty("DISPLAY",":1"); // or where you have Xvbf running
FirefoxDriver driver = new FirefoxDriver(bin,new FirefoxProfile());
Option 2
Consider switching to PhantomJS which is headless by design
Option 3
Consider switching to WebDriver Sampler's HTMLUnitDriver support which is also totally headless.
Related
I am using jp#gc - Firefox Driver Config plugin to hit some URL and assert some element in UI.
But I am not able to launch firefox driver as I cant see an option to enable to run firefox as a headless browser .
JP#GC - WebDriver Sampler:
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait
def wait = new WebDriverWait(WDS.browser,5000);
WDS.sampleResult.sampleStart(); WDS.browser.get('https://google.com/'); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[#name='q']"))); WDS.sampleResult.sampleEnd()
How about checking Headless box in the jp#gc - Firefox Driver Config?
Going forward be aware that you can always bypass any JMeter limitation by implementing whatever you want/need using JSR223 Test Elements and Groovy language, example code to kick off a headless Firefox would be something like:
System.setProperty('webdriver.gecko.driver','c:/apps/webdriver/geckodriver.exe')
def options = new org.openqa.selenium.firefox.FirefoxOptions()
options.addArguments('--headless')
def driver = new org.openqa.selenium.firefox.FirefoxDriver(options)
driver.get('http://example.com')
log.info('Current page title: ' + driver.getTitle())
driver.quit()
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It
I tried to use (jp#gc - HtmlUnit Driver Config) to create a headless browser test using jmeter, but I get this error
Response message: com.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "getComputedStyle" is not defined.
I read online and it suggest that jp#gc - HtmlUnit Driver Config doesn't support javascript. Is there a way I can fix this via jmeter? or is there any other option to do headless browser testing. I have linux server as load injector
Update:
I have a webdriver sampler to open google page
WDS.sampleResult.sampleStart() WDS.browser.get('http://google.com')
WDS.sampleResult.sampleEnd()
and have downloaded Phanton JS, but when I run it it doesn't show anything on the report. Should I add any other config?
HtmlUnit do not support very well JS.
I done many tests and used each one and i can say that PHANTOMJS is the best one with good support of all JS/CSS... have a beautiful renderer to have nice screenshots.
by code you can use it like this (you can download it from here http://phantomjs.org/download.html (phantomjs-1.9.8 is very stable)):
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
((DesiredCapabilities) caps).setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"your custom path\\phantomjs.exe"
);
WebDriver driver = new PhantomJSDriver(caps);
If you want to do that via JMeter GUI, you need to add before your Logic Controller an element JSR223 Sampler JSR223_Sampler
and inside the script panel :
org.openqa.selenium.Capabilities caps = new org.openqa.selenium.remote.DesiredCapabilities();
((org.openqa.selenium.remote.DesiredCapabilities) caps).setJavascriptEnabled(true);
((org.openqa.selenium.remote.DesiredCapabilities) caps).setCapability("takesScreenshot", true);
((org.openqa.selenium.remote.DesiredCapabilities) caps).setCapability(
org.openqa.selenium.phantomjs.PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"your custom path\\phantomjs.exe");
org.openqa.selenium.WebDriver driver = new org.openqa.selenium.phantomjs.PhantomJSDriver(caps);
org.apache.jmeter.threads.JMeterContextService.getContext().getCurrentSampler().getThreadContext()
.getVariables().putObject(com.googlecode.jmeter.plugins.webdriver.config.WebDriverConfig.BROWSER, driver);
Do not hesitate if you need more informations.
Is there any way to dynamically change the proxy being used by Firefox when using selenium webdriver?
Currently I have proxy support using a proxy profile but is there a way to change the proxy when the browser is alive and running?
My current code:
proxy = Proxy({
'proxyType': 'MANUAL',
'httpProxy': proxy_ip,
'ftpProxy': proxy_ip,
'sslProxy': proxy_ip,
'noProxy': '' # set this value as desired
})
browser = webdriver.Firefox(proxy=proxy)
Thanks in advance.
This is a slightly old question.
But it is actually possible to change the proxies dynamically thru a "hacky way"
I am going to use Selenium JS with Firefox but you can follow thru in the language you want.
Step 1: Visiting "about:config"
driver.get("about:config");
Step 2 : Run script that changes proxy
var setupScript=`var prefs = Components.classes["#mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
prefs.setIntPref("network.proxy.type", 1);
prefs.setCharPref("network.proxy.http", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.http_port", "${proxyUsed.port}");
prefs.setCharPref("network.proxy.ssl", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.ssl_port", "${proxyUsed.port}");
prefs.setCharPref("network.proxy.ftp", "${proxyUsed.host}");
prefs.setIntPref("network.proxy.ftp_port", "${proxyUsed.port}");
`;
//running script below
driver.executeScript(setupScript);
//sleep for 1 sec
driver.sleep(1000);
Where use ${abcd} is where you put your variables, in the above example I am using ES6 which handles concatenation as shown, you can use other concatenation methods of your choice , depending on your language.
Step 3: : Visit your site
driver.get("http://whatismyip.com");
Explanation:the above code takes advantage of Firefox's API to change the preferences using JavaScript code.
As far as I know there are only two ways to change the proxy setting, one via a profile (which you are using) and the other using the capabilities of a driver when you instantiate it as per here. Sadly neither of these methods do what you want as they both happen before as you create your driver.
I have to ask, why is it you want to change your proxy settings? The only solution I can easily think of is to point firefox to a proxy that you can change at runtime. I am not sure but that might be possible with browsermob-proxy.
One possible solution is to close the webdriver instance and create it again after each operation by passing a new configuration in the browser profile
Have a try selenium-wire, It can even override header field
from seleniumwire import webdriver
options = {
'proxy': {
"http": "http://" + IP_PORT,
"https": "http://" + IP_PORT,
'custom_authorization':AUTH
},
'connection_keep_alive': True,
'connection_timeout': 30,
'verify_ssl': False
}
# Create a new instance of the Firefox driver
driver = webdriver.Firefox(seleniumwire_options=options)
driver.header_overrides = {
'Proxy-Authorization': AUTH
}
# Go to the Google home page
driver.get("http://whatismyip.com")
driver.close()
My issue is caused by incorrect 'proxy' settings in Firefox so I want to disable the proxy in the profile that Selenium uses for my tests.
Currently my profile looks for the local proxy settings file by default:
file:///C:/Users/%username%/AppData/Local/Temp/customProfileDir536e1d9817834e4e838cad55697fc909/proxy.pac
That file contains these 3 lines:
function FindProxyForURL(url, host) {
return 'PROXY localhost:4444; DIRECT';
}
If during the tests I open the settings and set 'no proxy', the app starts working fine. How can I make the tests always launch with the 'no proxy' setting?
I tried to use the -avoidProxy flag when running the Selenium server but that hasn't helped. I also tried using a separate profile for tests but Selenium overrides its settings as well.
Create a new firefox browser profile & set the preferences in it as per your requirement.
Start selenium server with this profile using the switch -firefoxProfileTemplate <path_to_firefox>
I'm trying to start the selenium server by passing custom firefox profile to the DefaultSelenium constructor. It opens the browser with specified URL.
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*custom \"C:/Program Files/Mozilla Firefox/firefox.exe\"",ReadConFile.readcoFile("serverName"));
selenium.start();
the log is
16:39:19.246 INFO - Allocated session 4eb63d37a4ba4d2fb4e351f8f59e3ea6 for https://<myURL>, launching...
then it stays like that and server doesn't start.
however, this works fine if I don't use custom profile.
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*chrome",ReadConFile.readcoFile("serverName"));
selenium.start();
I need the launch custom profile as I've saved some site certificates necessary for https. Also, I'm executing this from eclipse.
I think my server isn't configured to launch custom profile. Please help me with this.
The start command is not really starting your selenium server per se, it's connecting your selenium object to an already running server with the browser of your choice.
To actually start the selenium [Jetty Web] server that sends / receives commands to your application under test via your specified browser, use a batch file and the switch rs79 is referring to. The contents of your batch file should include his line:
java -jar selenium-server-standalone-2.0a5.jar -firefoxProfileTemplate C:\custom-firefox-profile
Now you have a true selenium server running on your dev machine (localhost) with the default "4444" port. This will specify that any Firefox browser testing will use this profile.
Now your DefaultSelenium constructor, assignment, and other calls can look like this:
DefaultSelenium selenium = new DefaultSelenium("localhost", 4444, "*firefox","http://www.server.com");
selenium.start()
selenium.open("myApp/")
Firefox will start using the custom profile specified in the batch file that starts the Selenium server, with your desired base URL, and then navigate into your desired application [URL]. If you are beginning your test from "http://www.server.com/" and not "http://www.server.com/myApp", you can omit the last open line.
When you invoke the Selenium RC server, specify the path using the additional -firefoxProfileTemplate clause.
For example -
java -jar selenium-server-standalone-2.0a5.jar -firefoxProfileTemplate C:\custom-firefox-profile
This will enable you to use all the bindings you have saved within the custom profile.
If you want to have Fifefox profile as default in your test:
a) Download latest selenium-server: http://selenium-release.storage.googleapis.com/index.html
b) Download latest Firefox
c) Create FF profile (best in your custom directory) - in my case named "atf" https://support.mozilla.org/en-US/kb/profile-manager-create-and-remove-firefox-profiles
Default directory where profiles are saved:
C:\Users\johndoe\AppData\Roaming\Mozilla\Firefox\Profiles
d) In my case I use FF 36 and selenium-server-standalone-2.45.0.jar
Run selenium server:
java -jar C:\driver\selenium-server-standalone-2.45.0.jar -Dwebdriver.firefox.profile=atf
Then refer to it in your code:
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX)
If you want to refer to particular profile in your code (here I use default generated folder for profile named "myProfile"):
profile_path = C:/Users/johndoe/AppData/Roaming/Mozilla/Firefox/Profiles/2zvl3dxx.myProfile"
fp = webdriver.FirefoxProfile(profile_path)
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.FIREFOX,
browser_profile=myProfile)
You can add certificates to custom profile
a) Run browser with custom profile
b) Add certificate
c) Remember to tick option in Firefox Preferences/Advanced/Certificates
Select one automatically
to avoid asking for accepting certificate every time as you access tested page
d) Restart browser
e) Navigate to page what will be tested and accept User Identification Request
f) Close Firefox and enjoy custom profile with certificates available from selenium server :)
You can also start the Selenium server in java see here.