I am trying to use my Selenium scripts in java with JMeter's WebDriver Sampler.
Inside the webdriver sampler, the language is seleced to java, and the following code added:
package automationFramework;
public class FirstTestCase {
public static void main(String[] args) {
// Create a new instance of the Firefox driver
WebDriver driver = new ChromeDriver();
//Launch the Online Store Website
driver.get("www.google.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.google.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
driver.quit();
}
}
I am facing the following error:
java.net.MalformedURLException: unknown protocol: data
at java.net.URL.<init>(URL.java:600)
at java.net.URL.<init>(URL.java:490)
at java.net.URL.<init>(URL.java:439)
at com.googlecode.jmeter.plugins.webdriver.sampler.WebDriverSampler.sample(WebDriverSampler.java:80)
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:475)
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:418)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:249)
at java.lang.Thread.run(Thread.java:745)
However, tests written in javascripts work just fine.
What is the problem here? How to solve it?
You should not be instantiating WebDriver instance, JMeter does it for you given you add Chrome Driver Config element to your Test Plan and configure path to the ChromeDriver executable.
Once done you should be able to use WDS.browser shorthand like:
WDS.sampleResult.sampleStart();
WDS.browser.get("http://google.com");
WDS.log.info("Successfully opened the website www.google.com");
Thread.sleep(5000);
WDS.sampleResult.sampleEnd();
Also don't call quit() method, the WebDriver instance(s) will be shut down when test will be finished.
See Using Selenium with JMeter's WebDriver Sampler guide to get started with Selenium and JMeter integration.
If you are using Chrome driver 2.28 with Selenium 3.x.x you have to set the path of the Chrome driver before you open the browser.
Add this line:
System.setProperty("webdriver.chrome.driver", "C:\\your_folder\\chrome.exe");
Next, WebDriver driver;
Let me know if this helps you.
Another way you could do this is extract you webdriver test in a jar file then run in using a junit test in JMeter.
Related
My environment:
linux ubuntu
selenium-html-runner-3.4.0.jar
selenium-java-3.4.0
selenium-server-standalone-3.4.0.jar
to build I do:
javac -cp "/usr/share/java/junit.jar:/home/me/ushare/hobo/selenium/selenium-html-runner-3.4.0.jar:." TestHobo2.java
to run selenium-server:
java -jar selenium-server-standalone-3.4.0.jar
to run the test I do:
java junit.textui.TestRunner TestHobo2
and I get:
java.lang.RuntimeException: Could not start Selenium session:
at
com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:114)
at
com.thoughtworks.selenium.SeleneseTestBase.setUp(SeleneseTestBase.java:139)
at
com.thoughtworks.selenium.SeleneseTestBase.setUp(SeleneseTestBase.java:108)
at
com.thoughtworks.selenium.SeleneseTestCase.setUp(SeleneseTestCase.java:113)
at TestHobo2.setUp(TestHobo2.java:10) at
com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:289)
Caused by: com.thoughtworks.selenium.SeleniumException:
at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:111)
at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:105)
at com.thoughtworks.selenium.HttpCommandProcessor.getString(HttpCommandProcessor.java:277)
at com.thoughtworks.selenium.HttpCommandProcessor.start(HttpCommandProcessor.java:239)
at com.thoughtworks.selenium.DefaultSelenium.start(DefaultSelenium.java:105)
... 15 more
Here is my test case:
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class TestHobo2 extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.example.com/", "*chrome");
}
public void testGetLink() throws Exception {
selenium.type("name=p_loc", "groove");
selenium.click("css=input[type=\"Submit\"]");
selenium.waitForPageToLoad("30000");
}
}
Edit 2017/07/20 More info:
I am using Firefox version 52.0.2 (64-bit).
Should I be getting the following after the build?
Note: TestHobod2.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
geckodriver -v
1500554646753 geckodriver INFO geckodriver 0.18.0
1500554646753 webdriver::httpapi DEBUG Creating routes
1500554646764 geckodriver ERROR Address in use (os error 98)
Are you sure that you have properly defined on which ip address and port is selenium running? Run your selenium server and put http://127.0.0.1:4444/wd/hub/ link into your browser. Try to create new session manually (click on create session and select browser, see image below), new browser blank window should appear. If this is working correctly than selenium server is ok. Then there can be problem with connection between server and your runner.
screen of selenium hub with create session option
Do you have correctly setup selenium driver? For example I'm using
new RemoteWebDriver(new URL("http://127.0.0.1:4444/wd/hub/"),DesiredCapabilities.firefox())
EDIT1: Show example of setUp method which create instance of RemoteDriver, create new browser session and fill url.
WebDriver driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), DesiredCapabilities.firefox());
driver.manage().window().maximize()//maximaze window as possible
driver.get("www.google.com"); //navigate to google, i.e. fill url into opened session
Looking at the codebase, and trying to map your shared test code, seems to suggest that you will end up invoking a Firefox browser. The underlying implementation in Selenium has ensured that this will cause your test code to resort to perhaps using firefox.
Can you please ensure that you have made geckodriver downloaded and made available in your PATH variable ?
If that doesn't fix the problem (which could mostly be due to mismatch between the firefox version that you have in your desktop and the geckodriver version ), you can try switching to using Google Chrome.
You can switch to google chrome by
Changing : setUp("http://www.example.com/", "*chrome");
To : setUp("http://www.example.com/", "*googlechrome");
And see if that spins off the browser (For google chrome you would need to ensure you have chromedriver downloaded and made available in your local machine)
This is the code :
And error :
I am trying to run the script in headless browser but it gives the error as mentioned in screenshot.
I will suggest to use phantomjs instead of HTMLUnit driver for headless automation
Now if you want to use headless with phantomjs. download the stable build of phantomjs for your headleass jobs. download it from below link :-
http://phantomjs.org/download.html
Now add System.setPropertybefore driver instance
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);
refer the link below for more info :-
http://seleniumworks.blogspot.in/2013/03/headless-browser-testing-using.html
I have this java class code in package ChromeBrowser (That I made)
package ChromeBrowser;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchChrome{
public static void main(String[] args){
String url = "<<<The URL I want to open>>>";
WebDriver driver = setUp();
launch(driver, url);
}
static void launch(WebDriver driver, String url) {
driver.navigate().to(url);
}
static WebDriver setUp() {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
return driver;
}
}
But when I run it, I get the error:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Steps I've taken:
I have chrome installed.
I have downloaded the webdriver, and checked it is in C:\Selenium\chromedriver.exe
java jdk is in environment path
webdriver is in environment path, added in attempt to solve, didnt work
The code compiles and runs on my colleague's machine
Expected result:
Chrome browser opens at The URL I want to open.
Do I need to define the path to the chrome executable at C:\Program Files (x86)\Google\Chrome\Application?
Help me please thanks in advance.
EDIT:: I've tried most of the other stack overflow questions with the name of the error, but they haven't helped.
I am not really sure what the issue is but you can try below suggestions-
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");
This could be a compatibility issue between the 'selenium', 'Chrome browser version' and 'chrome driver' version that you are using. If
you are using Selenium 2.53, then using chrome driver 2.25 should work
for you.
Download latest chrome driver from seleniumhq.org
Add 127.0.0.1 localhost to C:\Windows\System32\drivers\etc\hosts.
Seems like there is some issue with creating session with Firefox. Try the following code and test using Chrome browser.
You need to download the executable driver from: https://sites.google.com/a/chromium.org/chromedriver/downloads
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver",
"/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver = new ChromeDriver();
//Puts an Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.get("http://www.calculator.net/");
//Maximize the browser
driver.manage().window().maximize();
or Check the network settings (proxy, firewall, antivirus software), something is blocking
connections between selenium and the browser.
I have Java project with TestNG tests and Selenium WebDriver. At the moment, I can run them locally from my IDE only.
How can I implement a system that allows me to run a test with a post/get request on server?
For example, a page with button: on button click, a test should start executing on server, with all WebDriver manipulations, and when it's finished, I would receive an answer.
At first, I tried to call my testng.xml from command line. It worked, but it looks like command line is not, what I can use remotely with simple FirefoxDriver, so I started researching the "RemoteWebDriver" feature.
Then I downloaded "selenium-server-standalone-3.4.0" and started the server and the node, but each time the script ends with an exception:
org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.gecko.driver system property
I added a required property, but it still doesn't work(the same problem). Here's a piece of code, which causes the exception:
#Test
public static void main() throws Exception {
URL server = new URL("http://127.0.0.1:4444/wd/hub");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
System.out.println("Connecting to " + server);
WebDriver driver = new RemoteWebDriver(server, capabilities);
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
driver.get("http://www.google.com");
driver.quit();
}
As you see, the system property is set (geckodriver.exe exists in directory and works fine with FirefoxDriver, but doesn't work with RemoteWebDriver).
I don't understand, what I'm doing wrong, and not even sure, that I'm working in right direction. Can you help me,please?
Tried adding parameter, proposed by #ekostadinov. Here's how I start hub and node:
java -Dwebdriver.gecko.driver="geckodriver.exe" -jar lib\selenium-server-standalone-3.4.0.jar -role hub
java -jar lib\selenium-server-standalone-3.4.0.jar -role node -hub http://localhost:4444/grid/register
Still doesn't work, but exception is different:
org.openqa.selenium.SessionNotCreatedException: Unable to find a matching set of capabilities
Update
Changed this
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
To this
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");
And it worked. Thanks everyone, who helped!
There is no need to
implement a system that allows me to run a test with a post/get request on server
except in the case, you really want to. Continuous_integration servers do all this and much more for you, out of the box. For example Jenkins (very suitable for JAVA based solutions) provides very convenient Selenium plugin, which helps you to manage the Grid.
One thing to note here - you should pass additional parameters when starting the Hub on your server, like so:
java -Dwebdriver.chrome.driver="/full/path/to/chromedriver" -Dwebdriver.gecko.driver="/full/path/to/geckodriver" -jar selenium-server-standalone-3.4.0.jar -role hub
Try moving this line of code above where you instantiate the driver:
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
Like this
#Test
public static void main() throws Exception {
URL server = new URL("http://127.0.0.1:4444/wd/hub");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
System.out.println("Connecting to " + server);
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
WebDriver driver = new RemoteWebDriver(server, capabilities);
driver.get("http://www.google.com");
driver.quit();
}
Also make sure the geckodriver is in the same directory where you are starting the hub/node.
I'm trying to launch chrome with Selenium Webdriver and used the following code:
System.setProperty("webdriver.chrome.driver",
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
Chrome browser opens but is not proceeding further. What could be the reason for the following error:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
You are incorrectly starting up the driver
webdriver.chrome.driver is supposed to be the path to the driver that you've downloaded and not Chrome's physical location.
First you need to download chrome driver file from this link and than import its JAR to the package in eclipse.
Download the link from here
Then you will have to import it in your program.
import org.openqa.selenium.chrome.ChromeDriver;
and than make a driver instance
driver = new ChromeDriver();
Download the external JAR of chrome
In eclipse :: right click on the respective package(in the package explorer) and click on the properties. Go to Java build path and add external jars. Now add the jar file of chrome . and than follow the steps i wrote in the ans which was to import the chrome driver and creating an instance
Follow these steps in the photograph.
1)
select your file from here and right click
Below snippet shows how you can open chrome browser using selenium webdriver.
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting the webdriver.chrome.driver property to its executable's location
System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");
//Instantiating driver object
driver = new ChromeDriver();
//Using get() method to open a webpage
driver.get("https://stackoverflow.com");
//Closing the browser
driver.quit();
}
Use the latest versions of ChromeDriver.
Source|
http://chromedriver.storage.googleapis.com/index.html
You need to setup your browser settings first. Try below-mentioned code if it helps:
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "C:\\**PATH**\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
}
You'll need to import files by hovering on error lines.