JMeter WebDriver Sampler - headless Firefox - selenium

Is it possible to run Firefox headless with WebDriver Sampler? I have used the "Use Chrome headless mode" option with Chrome before, but I don't see that option in Firefox Driver Config.
Perhaps this can be done in the actual sampler, by setting options to 'browser' in the code below?
WDS.sampleResult.sampleStart()
WDS.browser.get('http://jmeter-plugins.org')
WDS.sampleResult.sampleEnd()
Thank you.

There is no easy/GUI way of doing this using WebDriver Sampler (unless you patch FirefoxDriverConfig to include FirefoxBinary and pass to it --headless argument like:
FirefoxBinary firefoxBinary = new FirefoxBinary();
firefoxBinary.addCommandLineOptions("--headless");
Another option would be switching to JSR223 Sampler to initialise the FirefoxDriver class yourself from the scratch.
More information: Firefox - Headless Mode
And last but not the least, if you're about to execute your Selenium tests on i.e. Linux machine which doesn't have GUI you can create a virtual display using Xvfb so Firefox would run attached to this virtual desktop. See Headless Execution of Selenium Tests in Jenkins for more details on implementing this on different operating systems.

Related

What is the difference between Selenium Grid and pytest-xdist plugin?

I'm new to parallel testing and I was wondering what the difference is between them.
Apparently, pytest-xdist does not need Selenium Grid to run. It can be used with Selenium alone.
Does anybody have any clue or resource where I can learn the difference?
Thanks and kind regards.
Just in case this is useful for anybody I will write down what I learnt:
pytest-xdist: It is used for running parallel tests in the browsers that are installed in the local machine where tests will be run. This means that each test has a browser configured (e.g.: Firefox, Chrome) such as:
driver = webdriver.Firefox()
or
driver = webdriver.Chrome()
And so each test will run with the driver that was specified in the code. Obviously, the local machine needs the browser drivers to be available in any PATH location, so that tests can be run with them.
Selenium Grid: It allows the execution of tests in different browsers, browser versions and operating systems configurations.
Selenium Grid combined with pytest-xdist allows the execution of parallel tests in different browser-OS environments (configured with capabilities, I think).
An execution command example would be:
pytest -n5 -v -s -m "test or ready" --capability browserName firefox
-n5: (pytest parameter) means that 5 instances of the browser will be launched simultaneously.
test or ready: these are the markers that can be combined to execute tests that have these markers.
browserName firefox: It is a capability that indicates that tests must be run in the specified browser, in this case, Firefox. Some possible values are: chrome, firefox, internet explorer, safari.
Some other capabilities are:
version: The browser version to use.
platform: The platform in which the browser will be executed. Some possible values are: WINDOWS, XP, VISTA, MAC, LINUX, UNIX, ANDROID.
To set up the Selenium Grid environment go along the following steps:
Download selenium-server-standalone-[version].jar from
https://www.selenium.dev/downloads/.
Initiate the HUB:
java -jar selenium-server-standalone-[version].jar -role hub
Initiate as many NODES as you want with:
java -jar selenium-server-standalone-[version].jar -role node -hub http://[URL_HUB]/wd/hub
Configure a RemoteDriver in the tests, for example: driver = webdriver.Remote( desired_capabilities = DesiredCapabilities.CHROME, command_executor = 'http://[URL_HUB]:4444/wd/hub')
I still need to learn how to pass the capabilities args as parameters for the RemoteDriver. In conftest.pyfile I can get the capabilities as a list with config.getoption('--capability')). I still need to figure out how to pass the capabilities I want to the setUp method of all my tests.
If someone knows, I will really appreciate a hint on this.
I hope this helps someone who is as lost as I was at the beginning :)

ChromeDriver doesn't open in jenkins

I created a selenium cucumber framework that has a test.
The test needs to open chromeDriver, and it is working when I run it in my IDE without any problems.
When I run the project in Jenkins, although the test runs, the chromeDriver doesn't open at all.
The console log is:
17:32:14 Starting ChromeDriver 89.0.4389.23
(61b08ee2c50024bab004e48d2b1b083cdbdac579-refs/branch-heads/4389#{#294})
on port 4816
17:32:14 Only local connections are allowed.
17:32:14 Please see
https://chromedriver.chromium.org/security-considerations for
suggestions on keeping ChromeDriver safe.
17:32:14 ChromeDriver was started successfully.
Of course, this log is shown in my ide with the difference that chromeDriver opens in IDE mode and doesn't open in Jenkins mode.
What can I do?
First of all you should add more description, like:
what language do you use?
how start driver in your code?
do you use local driver or remote one?grid or selenoid?
do you use --headless mode on jenkins?
does jenkins have Xs for running browser?
There's bunch of solving, but each of them is based on more particular description:
Best way, but a bit tricky/expensive:
launch own Grid/Seleloid server on ec2 instance on any cloud platform you like: GCP, AWS, Azure, DigitalOcean etc
install docker and launch selenoid intance
launch selenoid application by downloading from github release page:
chmod +x cm
./cm selenoid start --vnc
get its IP and provide it to your driver creation command
which looks like that:
java:
WebDriver driver = new RemoteWebDriver(new URL(SelenoidIP/wd/hub), firefoxOptions);
driver.get("http://www.google.com");
driver.quit();
ruby:
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :remote, url: SelenoidIP/wd/hub
driver.get "http://www.google.com"
driver.close
python:
from selenium import webdriver
driver = webdriver.Remote(
command_executor=SelenoidIP/wd/hub)
driver.get("http://www.google.com")
driver.quit()
Update:
I found a better solution.
Start Jenkins as a process from command line, not as a service, to get Chrome to open.
https://stackoverflow.com/a/41457823/1691651
command line commands:
Jenkins.exe stop
java -jar Jenkins.war
To save your Jenkins configurations, create an Environment Variable for JENKINS_HOME that points to location listed on the "Configure Jenkins" setting.
Old answer:
This also happened to me in webdriver.io when using the "chromedriver" service. To solve it, I had to manually download the chromedriver at https://chromedriver.chromium.org/downloads and then run it via a terminal--I don't know why that fixed it.
I don't need to run chromedriver separately in a terminal when testing via command line on the same EC2 instance.

Do headless web browser need selenium WebDriver?

I am trying to use headless web browser(such as headless chrome) for our selenium tests.Should I have to use selenium WebDriver(for python or c# bindings)?
Headless Chrome
As per Getting Started with Headless Chrome the Headless Chrome is the server environment where you don't need a visible UI shell.
If you've got Chrome 59+ installed, you start Chrome with the --headless flag as follows:
chrome \
--headless \ # Runs Chrome in headless mode.
--disable-gpu \ # Temporarily needed if running on Windows.
chrome should always point to your installation of Chrome. The exact location of-coarse varies from platform to platform.
ChromeDriver
As per ChromeDriver - WebDriver for Chrome, in simple words WebDriver is an open source tool for automated testing of webapps across many browsers which provides capabilities for navigating to web pages, user input, JavaScript execution, and much more. ChromeDriver is the standalone server which implements WebDriver's wire protocol for Chromium.
Conclusion
If you intend to use Chrome Browser in Headless Mode(i.e. Headless Chrome) for your selenium tests you have to mandatorily use ChromeDriver
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
Before we set up a Chrome web driver instance, we have to create an Options object that allows us to specify how exactly we want to launch Chrome. Let’s tell it that we want the browser to launch headless and that the window size should be set to 1920x1080. We also need ChromeDriver to be able to run Chrome at all 
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
# download the chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads and put it in the
# current directory
chrome_driver = os.getcwd() +"\\chromedriver.exe"
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
driver.get("https://www.google.com")

What is difference between Xvfb and Chromedriver and when to use them

Hi I am planning to setup selenium to test my web application.
I have read that both chromedriver and Xvfb can be used to run the tests.
I have also read that Xvfb can be configured to use chromdriver.
So that got me confused. What role does chromedriver and Xvfb in runnning the selenium tests.
Thanks
chromedriver - to run tests on chrome browser (with GUI).
Xvfb - to run tests in headless mode. can be any browser including chrome (Browser GUI won't be displayed, so you can use the machine for some other operations).
code snippets (python):
Chrome Driver (download here):
browser = webdriver.Chrome() // to launch tests in Chrome browser.
Xvfb - using pyvirtualdisplay (python wrapper for Xvfb) :
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
# now Chrome will run in a virtual display.
# you will not see the browser.
browser = webdriver.Chrome()
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()
References:
How do I run Selenium in Xvfb?

Not able to run webdriver scripts from jmeter

I am trying to execute webdriver script from jmeter.
I have installed webdriver plugin.
Created firefox config element and web driver sampler.
Added script under webdriver sampler -
WDS.sampleResult.sampleStart()
WDS.browser.get('http://google.com')
WDS.sampleResult.sampleEnd()
But while executing, browser is not opening.
I'm pretty much sure that you have Firefox version which is not supported by underlying Selenium libraries.
WebDriver Sampler plugin 1.2.1 supports Firefox 33
WebDriver Sampler plugin 1.2.0 supports Firefox 26
If you need the latest Firefox for any other reason you can have Firefox 26 or 33 installed somewhere else. Just add the following line to system.properties file (lives under /bin folder of your JMeter installation)
webdriver.firefox.bin=/path/to/your/firefox/directory
See The WebDriver Sampler: Your Top 10 Questions Answered guide for more WebDriver sampler tips and tricks.
Jmeter only supports some of the versions of Firefox to run Jmeter-webdriver script. Use firefox 45.0 it will work like pro.
The Installation of firefox should be in the default directory, do not custome the path of Firefox 45.0.