JMeter WebDriver Sampler doesn't work and doesnt have headless - jmeter

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

Related

Jmeter - ERROR c.g.j.p.w.s.WebDriverSampler: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: org for class

I just upgraded to version 4.5.0 of WebDriver, using JMeter 5.5, and can't perform any actions.
Based on the official docs: https://jmeter-plugins.org/wiki/WebDriverSampler/
should be using like:
var pkg = JavaImporter(org.openqa.selenium) but I faced exception.
Is there any way how to resolve this exception, so I can use it for sendKeys or click actions?
You're using groovy as the language
JavaImporter object is specific for JavaScript
This mix of languages is not supported, you either need to switch to javascript in the "Language" dropdown or re-write your code in Groovy using import statement and replace var occurrences with def
Also according to Top 15 UI Test Automation Best Practices
NEVER use Thread.sleep() unless there are specific test requirements
so consider migrating to Explicit Wait instead
thanks # Dmitri T for your direction. Since javaScript is droppped after java15, here is some sample code that I found usefull using: Jmeter5.5 + Java17 + selenium version 4.5.0 + groovy as language
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
WDS.sampleResult.sampleStart();
WDS.browser.get("http://gmail.com/");
JMeterVariables vars = JMeterContextService.getContext().getVariables();
java.lang.Thread.sleep(3000);
def userIdElement = WDS.browser.findElement(By.xpath("//input[#id='identifierId']"));
userIdElement.sendKeys("gest#gmail.com");
java.lang.Thread.sleep(3000);
WDS.sampleResult.sampleEnd()

Selenium get http request ajax url after button click

I need to get the http request URL when a button is clicked in a webpage programatically.
I am using selenium to trace the button and I am performing click on the button. on click of the button it makes a http request and the same can be traced in the network tab of the browser.
How can i get the request URL programatically once I trigger the button click using selenium.
Any other tools or libraries that I can use to achieve the same functions is also ok for me. I just need to be able to get the URL after button click programatically. This is a dynamic URL which changes periodically and the objective is to automate the download process through code.
Thanks in advance for any help!
You can use JavaScript executor to get network data. Please refer https://stackoverflow.com/a/45859018/5966329
get request/response data from there.
DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry le : les) {
System.out.println(le.getMessage());
}
Python Equivalent:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import
DesiredCapabilities
caps = DesiredCapabilities.CHROME
caps['goog:loggingPrefs'] = {'performance': 'ALL'}
driver = webdriver.Chrome(desired_capabilities=caps)
driver.get('https://stackoverflow.com')
for entry in driver.get_log('performance'):
print(entry)
Please refer thread for more info python

JMeter - client-side for mobile in Safari

I have Safari browser on Win with installed webdriver on it and following code in JSR223 for Safari:
import org.openqa.selenium.safari.SafariOptions;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
Map<String, Object> mobileEmulation = new HashMap<>();
mobileEmulation.put("userAgent", "vars.get("userAgent")");
Map<String, Object> safariOptions = new HashMap<>();
safariOptions.put("mobileEmulation", mobileEmulation);
SafariOptions safari = new SafariOptions();
options.setExperimentalOption("mobileEmulation", mobileEmulation);
SafariDriver driver = new SafariDriver(options);
driver.get("url");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath")));
driver.findElement(By.xpath("xpath")).click();
vars.putObject("driver", driver);
Error messages are folllowing:
Response code:500
Response message:javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script19.groovy: 2: unable to resolve class org.openqa.selenium.safari.SafariOptions
# line 2, column 1.
import org.openqa.selenium.safari.SafariOptions;
^
Script19.groovy: 3: unable to resolve class org.openqa.selenium.safari.SafariDriver
# line 3, column 1.
import org.openqa.selenium.safari.SafariDriver;
^
2 errors
Could you please help me to find, what I'm missing?
With regards to the error you're getting it seems that you don't have selenium-safari-driver under JMeter Classpath so you need to download the appropriate .jar, drop it to "lib" folder of your JMeter installation and restart JMeter to pick the library up.
With regards to your "Safari browser on Win" stanza, are you absolutely sure this is something you really want to be doing? Windows version of Safari EOL was in 2012 and I don't think you should be investing into automated testing of 8-years-old browser.
Going forward I think this line:
mobileEmulation.put("userAgent", "vars.get("userAgent")");
should look like:
mobileEmulation.put("userAgent", vars.get("userAgent"));
I also fail to see a valid use case for JMeter there, if you need to simulate different browsers doing a performance test - you can easily send the relevant User-Agent header using HTTP Header Manager and conduct the load using normal JMeter's HTTP Request Samplers and if you're just creating automated tests - you don't need JMeter at all

Headless browser using jmeter

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.

How to override Display variable in jmeter.sh

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.

Resources