Connection refused thrown when instantiating a new chrome driver - selenium

I have a selenium project with cucumber on my local Jenkins server.
DesiredCapabilities capability = DesiredCapabilities.chrome();
try {
baseUtil.driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
} catch (MalformedURLException e) {
e.printStackTrace();
}
This is how i instantiate my driver. I have the chrome plugin on jenkins as well.
When i try to build, i get this error:
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.
....
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to
localhost:4444 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1]
failed: Connection refused
Any idea what am i doing wrong?

If you are running locally try switching to chromedriver
DesiredCapabilities capability = DesiredCapabilities.chrome();
try {
baseUtil.driver = new ChromeDriver(capability));
} catch (MalformedURLException e) { e.printStackTrace(); }
the URL in your previous call was set to point to the hub which you aren't using.
you might need to point it at the local driver location such as:
System.setProperty("webdriver.chrome.driver", "<pathtoyourchromedriver>/chromedriver.exe");

Related

Selenium Chrome Browser org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start

public class Dem {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
String url = "https://www.google.com";
System.setProperty("webdriver.chrome.driver","C:\\Users\\Jim\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.get(url);
driver.close();
}
}
Selenium webdriver and Chrome driver are the latest versions
Selenium webdriver :3.14.0
chromedriver: 2.42
java:1.8.0_181
And this is the error after running the code. Google chrome doesn't launch at all.
SEVERE: org.apache.commons.exec.ExecuteException: Process exited with an error: -1073741792 (Exit value: -1073741792) Exception in
thread "main" org.openqa.selenium.WebDriverException: Timed out
waiting for driver server to start. Build info: version: '3.14.0',
revision: 'aacccce0', time: '2018-08-02T20:13:22.693Z' Driver info:
driver.version: ChromeDriver at
org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:193)
at
org.openqa.selenium.remote.service.DriverService.start(DriverService.java:179)
at
org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:79)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at
org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
at
org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:130)
at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:181)
at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:168)
at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123)
at news.Dem.main(Dem.java:17) Caused by:
org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting
for [http://localhost:32986/status] to be available after 20002 ms at
org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:100)
at
org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:188)
... 9 more
Debugging Steps
It may be either of the two below issue.
1. Chrome Driver not started for some reason:
Run the chrome driver in terminal maually
C:\Users\Jim> C:\Users\Jim\Downloads\chromedriver_win32\chromedriver.exe
Now we have to get the following message denoting server started sucessfully.
If you are not getting above message and your server is not started, then check what is error you are getting and respond accorsingly? May downloaded corrupted binary, the download it again.
2. Unable to access the chrome driver with hostname as localhost
If you able to start chromedriver sucessfully as in above steps, now try to access the below url with respective port.
http://localhost:{port}/status
For e.g., in above message, the server started on port 9515. Access the url in browser, http://localhost:9515/status
You should get above message indicating server is accessible with hostname as localhost
If your are not able access, then open C:\Windows\System32\drivers\etc\hosts in editor with administrator privilege and check localhost DNS is mapped to 127.0.0.1
public class Demo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","\PATH OF THE DRIVER");
WebDriver driver = new Chrome driver();
String url = "https://www.google.com";
driver.get(url);
Thread.sleep(2000);
driver.close();
}
}

Browsers are not launching from command prompt. Same testng File is working while running through eclipse

I am trying to run my testng test cases from command prompt. I can see browser's instance are created in task manager but it is not launching with given URL. I debugged the code and found it is failing on new ChromeDriver() below line but there is no exception.
On command line, I can see below message :
Starting ChromeDriver 2.18.343845 (73dd713ba7fbfb73cbb514e62641d8c96a94682a) on port 7108
Only local connections are allowed.
Browser instance will create in task manager but it will not launch and will not go to next line.
This issue is coming from all browsers on same line.
same code worked well in Eclipse.
Using Selenium 2.53.0 and testng 6.9.13 versions
Try this solution:
ChromeDriverService service = null;
try
{
service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(chromeDriver))
.usingAnyFreePort()
.build();
service.start();
}catch(IOException io){}
if(service.isRunning())
{
DesiredCapabilities cap = DesiredCapabilities.chrome();
try{
driver = new RemoteWebDriver(service.getUrl(), cap);
}catch (SessionNotCreatedException e)
{
}
}

Exception on Selenium Grid program

Have started the hub and registered the node. Then wrote a program in eclipse:
after running the program i am getting below exception:-
Exception in thread "main" java. lang.Class Cast Exception: java.lang.String cannot be cast to java.util.Map.
the remote web driver is :
Remote Web Driver driver = new Remote Web Driver(client URL,capability);
I've had the same error. The problem was in class org.openqa.selenium.remote.RemoteWebDriver (75):
Map<String, Object> rawCapabilities = (Map<String, Object>) response.getValue();
It's happened because I've given the wrong URL parameter for RemoteWebDriver():
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), capability);
That's why response had a String type.
Please check your url. It should be:
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
I hope it will help you :)

Running safari from webdriver

I'm trying to run Safari from WebDriver. When i start the project it cant build beacouse it cannot find safari. Safari is installed on the machine, i am registered as an developer.
Does anyone have a solution?
Code:
if(browser.equalsIgnoreCase("firefox")) {
capability = DesiredCapabilities.firefox();
capability.setBrowserName("firefox");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
}
if(browser.equalsIgnoreCase("iexplore")) {
capability = DesiredCapabilities.internetExplorer();
capability.setBrowserName("iexplore");
capability.setPlatform(org.openqa.selenium.Platform.WINDOWS);
}
if(browser.equalsIgnoreCase("chrome")) {
capability = DesiredCapabilities.chrome();
capability.setBrowserName("chrome");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
}
if(browser.equalsIgnoreCase("safari")) {
capability = DesiredCapabilities.safari();
capability.setBrowserName("safari");
capability.setPlatform(org.openqa.selenium.Platform.ANY);
}
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
driver.navigate().to(test_data.BASE_URL);
Stacktrace:
FAILED CONFIGURATION: #BeforeClass setup("safari")
org.openqa.selenium.WebDriverException: Error forwarding the new session cannot find : {platform=ANY, browserName=safari, version=}
Command duration or timeout: 203 milliseconds
The new version of selenium i.e. selenium 2.30.0 is released.Grab it as it has built in support for safari driver...Enjoy!!!

selenium grid, creating node programmatically

I have to create a java application that will start a node and connect it to the hub. So far I have been able to do so when the hub and node are on the same computer, but as soon as I try to connect on another machine hub, the registering process hang forever.
I tried different approach. To just call my bat file function in code.
String command = "java -jar selenium-server-standalone-2.26.0.jar -role node -hub http://192.168.0.11:4444/grid/register -port 4449 -Dwebdriver.chrome.driver=data\\driver\\chromedriver.exe -Dwebdriver.ie.driver=data\\driver\\IEDriverServer.exe -nodeConfig data\\configurations.json";
try
{
pr = Runtime.getRuntime().exec(command);
}
catch(Exception e)
{
e.printStackTrace();
}
The command work when called from a bat file, but in code it only works if the node and hub are on the same computer.
I also tried to use the RegistrationRequest.
RegistrationRequest req = new RegistrationRequest();
req.setRole(GridRole.NODE);
Map<String, Object> nodeConfiguration = new HashMap<String,
Object>();
nodeConfiguration.put(RegistrationRequest.AUTO_REGISTER, true);
nodeConfiguration.put(RegistrationRequest.HUB_HOST, "192.168.100.66");
nodeConfiguration.put(RegistrationRequest.HUB_PORT, 4444);
nodeConfiguration.put(RegistrationRequest.PORT, 5555);
URL remoteURL = new URL("http://" + "192.168.100.66" + ":" + 5555);
nodeConfiguration.put(RegistrationRequest.PROXY_CLASS, "org.openqa.grid.selenium.proxy.DefaultRemoteProxy");
nodeConfiguration.put(RegistrationRequest.MAX_SESSION, 1);
nodeConfiguration.put(RegistrationRequest.CLEAN_UP_CYCLE, 2000);
nodeConfiguration.put(RegistrationRequest.REMOTE_HOST, remoteURL);
nodeConfiguration.put(RegistrationRequest.MAX_INSTANCES, 1);
req.setConfiguration(nodeConfiguration);
remote = new SelfRegisteringRemote(req);
remote.startRemoteServer();
remote.startRegistrationProcess();
Same result, when I try to run on another computer hub, it hand at the registering process.
INFO - Registering the node to hub :http://192.168.100.66:4444/grid/register
any idea why? or how to do it.
I figured out my problem, which is really simple to fix. In my code I had
URL remoteURL = new URL("http://" + "192.168.100.66" + ":" + 5555);
I just needed to replace the ip address by my local ip address, not the hub ip address, and it worked. Which is weird cause I am pretty sure I took this code from somewhere online and he had a variable for the ip, and it was the same for remoteURL and the HUB_HOST
well. Not sure whether is suitable for you but I'd like to share approach I use on my project.
I've got remote machine with 192.168.4.52 IP and selenium stanadlone server running on it.
All selenium test suites I got locally.
So to run my selenium test suite on remote machine I simply use these settings in BaseSeleniumTest.java on my local machine:
....
#BeforeClass
public static void firefoxSetUp() throws MalformedURLException {
DesiredCapabilities capability = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(1920, 1080));
}
#Before
public void homePageRefresh() throws IOException {
driver.manage().deleteAllCookies();
driver.get(propertyKeysLoader("login.base.url"));
}
#AfterClass
public static void closeFirefox(){
driver.quit();
}
where string
driver = new RemoteWebDriver(new URL("http://192.168.4.52:4444/wd/hub"), capability);
indicates IP of machine which I want to run my selenium test siute on.
I'm starting server on remote machine with this command:
java -jar selenium-server-standalone-2.26.0.jar in cmd before I run my test suite.
Hope it be helpful for you.