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.
Related
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 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.
I am using selenium 3.2.0 standalone, chrome latest version and chrome driver latest version which is giving not able to connect exemption on running.
Issue is with chrome driver please help
org.openqa.selenium.SessionNotCreatedException:
my code look like this:
System.setProperty("webdriver.chrome.driver","E:/WorkSpace/Econsys_Auto/Jarfiles/chromedriver.exe");
System.setProperty("webdriver.chrome.driver","E:/WorkSpace/Econsys_Auto/Jarfiles/chromedriver.exe");
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
capabilities.setBrowserName("chrome");
capabilities.setPlatform(Platform.XP);
System.out.println("*******");
driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);
driver.get(baseUrl);
Got selenium 3.2.0 with chromedriver 2.28, but I got Win10.
Try this code:
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");//use your path
try{
driver = new ChromeDriver();
}
catch (Exception e) {
System.out.println(e);
}
I would suggest that you please try again using Selenium 3.3.0 (which is released just now)
There was an issue in Selenium (versions previous to 3.3.0) which was preventing the actual reason for the new session failure from being relayed to the test. That explains why you are not able to identify the root cause.
This now stands fixed in Selenium 3.3.0 as part of this commit
Now coming back to the possible reasons why your tests are not spawning a chrome browser.
I notice that you are using
System.setProperty("webdriver.chrome.driver","E:/WorkSpace/Econsys_Auto/Jarfiles/chromedriver.exe");
in your test code.
This sets up the JVM argument webdriver.chrome.driver ONLY for the current JVM. The Grid and the node are running in a different JVM (remember you spawned them using java -jar command ?).
I would instead recommend that you add "E:/WorkSpace/Econsys_Auto/Jarfiles/" to your PATH variable. This is a one time exercise you would be doing on a "per machine" basis and this will ensure that Selenium finds the chromedriver binary's location (I think that's why your tests are failing and you should be able to confirm this, if you looked at the node's console logs)
I'm trying to run my Java selenium tests using Opera (version 31). I'm using last version of Selenium Webdriver (2.47.1) and last version of OperaChromiumDriver (0.2.2).
I've tried to use next method to instantiate Opera:
System.setProperty("webdriver.chrome.driver", "\\path\\to\\my\\operadriver.exe");
WebDriver driver = new ChromeDriver();
And i've tried another method with RemoteWebdriver:
DesiredCapabilities capabilities = DesiredCapabilities.opera();
ChromeOptions options = new ChromeOptions();
options.setBinary("/path/to/opera");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"),capabilities);
(these methods are described in answers to this question: How to use OperaChromiumDriver for opera version >12.X)
Both methods have the same problem.
Opera opens, but then crushes with next exception:
org.openqa.selenium.SessionNotCreatedException: session not created exception from disconnected: Unable to receive message from renderer
(Session info: Opera with embedded Chromium 0.1889.230)
(Driver info: OperaDriver=0.2.0 (ba47709ed9e35ce26dbd960fb5d75be104290d96),platform=Windows NT 6.1 x86_64
(WARNING: The server did not provide any stacktrace information)
Firefox, Chrome and IE drivers work as it should be, i have such problem only with OperaChromiumDriver.
Can anyone help me with this issue?
Try to instantiate OperaDriver like this instead:
File operaFile = new File("\\path\\to\\my\\operadriver.exe");
System.setProperty("webdriver.opera.driver", operaFile.getAbsolutePath());
WebDriver driver = new OperaDriver();
In my application, .getAbsolutePath() works but just specifying the path in .setProperty does not. No idea why, since the string output of either is identical.
Unfortunately I am still unable to use OperaDriver in my tests because it becomes unresponsive after loading a few pages. This occurs on 3 different machines running different versions of Windows and returns only this error:
[SEVERE]: Timed out receiving message from renderer:
FirefoxDriver, ChromeDriver, and InternetExplorerDriver all work fine with my tests, so, whatever.
My Selenium Grid is showing an error:
org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.chrome.driver system property;
but I have specified it perfectly (according to my knowledge)
System.out.println("googlechrome");
capability = DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
System.setProperty("webdriver.chrome.driver", "D:\\testing\\zip file\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
driver = new ChromeDriver();
I don't know what went wrong. This same code worked perfectly last week but now it doesn't.
if you are running the Grid, you need to set up Chromedriver executable in the node:
java -jar selenium-server-standalone-2.31.0.jar -role node -hub http://localhost:4444/grid/register -maxSession 15 -browser browserName="chrome",version=ANY,platform=WINDOWS,maxInstances=15 -Dwebdriver.chrome.driver=lib\chromedriver.exe
the most important part is the -D switcher which goes right after the chrome browser setup.
also, if you are running more than one nodes, that path must direct to the chromedriver executable on the concrete computer (node). Thats why I have it as relative path and not as a absolute path...
Is this not what you need?
File file = new File("D:\testing\zip file\chromedriver_win_26.0.1383.0\chromedriver.exe");
system.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver(capability);