TestNG - java.net.SocketException [duplicate] - java

This question already has answers here:
Official reasons for "Software caused connection abort: socket write error"
(14 answers)
Closed 7 years ago.
My settings :
TestNG 6.9.10 - Java 1.8 - Eclipse Mars.1 Release (4.5.1)
Windows 10
Every time I run a TestNG test suite I have this error :
java.net.SocketException: Software caused connection abort: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
at java.io.ObjectOutputStream.<init>(Unknown Source)
at org.testng.remote.strprotocol.SerializedMessageSender.sendMessage(SerializedMessageSender.java:24)
at org.testng.remote.strprotocol.MessageHub.sendMessage(MessageHub.java:43)
at org.testng.remote.RemoteTestNG$RemoteSuiteListener.onStart(RemoteTestNG.java:257)
at org.testng.SuiteRunner.invokeListeners(SuiteRunner.java:208)
at org.testng.SuiteRunner.run(SuiteRunner.java:266)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
Even if my test class is empty :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class sample {
public WebDriver driver;
#Test
public void f() {
}
#BeforeTest
public void beforeTest() {
driver = new FirefoxDriver();
driver.get("http://localhost:8081/login.html?profile=desktop");
}
#AfterTest
public void afterTest() {
driver.close();
}
}
The test is running, so it doesn't interfere with the test case, but I really would like to understand and remove this Exception.
Thank you for any idea.

I'm thinking somewhere in WebDriver, java.net is used. Your localhost webserver is closing the socket or it was closed prematurely. Instead of using Before and After test, try to write it all in one Test method and debug. Unfortunately, there's no mention of your class in the stack trace to pinpoint which call is causing the problem.
Step through the following and see where the exception is thrown. If it's on the driver.get call, then your http server is likely rejecting the connections. If it's thrown at the close(), the connection has already been dropped (probably rejected by the server).
#Test
public void test() {
driver = new FirefoxDriver();
driver.get("http://localhost:8081/login.html?profile=desktop");
driver.close();
}

Related

Beginner Selenium code not understanding the error it is throwing [duplicate]

This question already has answers here:
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap error using GeckoDriver Firefox through Selenium in Java
(1 answer)
Selenium Webdriver + Java - Eclipse: java.lang.NoClassDefFoundError
(7 answers)
Closed 2 years ago.
Hi i'm rather new to selenium and understand the basics but cant understand why my basic code it just open a web page is throwing the error its throwing. The error its throwing ill be listed at the bottom.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Selenium1 {
public static void main(String[] args) {
WebDriver Driver = new ChromeDriver ();
String web = "https://www.facebook.com/";
Driver.get(web);
System.out.println(Driver.getTitle());
}
}
Exception in thread "main" java.lang.NoClassDefFoundError:
com/google/common/collect/ImmutableMap at
org.openqa.selenium.remote.service.DriverService$Builder.(DriverService.java:259)
at
org.openqa.selenium.chrome.ChromeDriverService$Builder.(ChromeDriverService.java:101)
at
org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123)
at Selenium1.main(Selenium1.java:10) Caused by:
java.lang.ClassNotFoundException:
com.google.common.collect.ImmutableMap at
java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at
java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 5 more

Not Able to open Chrome browser in Selenium

Not Able to open Chrome browser in Selenium.
Here is the code:
package toolsqa;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\MR049860\\Documents\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.facebook.com");
driver.manage().window().maximize();
}
}
I'm getting the following Exception:
Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at toolsqa.TestChrome.main(TestChrome.java:14)
The error says it all :
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V
While mentioning the Key and Value within System.setProperty() you have to escape the back slashes (\\). Hence, you need to change the line :
System.setProperty("webdriver.chrome.driver","C:Path\chromedriver.exe");
To :
System.setProperty("webdriver.chrome.driver","C:\\absolute_path\\to\\chromedriver.exe");

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.io.FileHandler.unzip(Ljava/io/InputStream;)Ljava/io/File;

I'm trying to run selenium webdriver program, getting following error :
Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.io.FileHandler.unzip(Ljava/io/InputStream;)Ljava/io/File;
Firefox version : 47.0.1
Selenium version : 2.53.1
Eclipse : Oxygen Release (4.7.0)
import org.apache.xpath.XPathContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PG1
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://demo.guru99.com/selenium/newtours/");
System.out.println("The title of page is : " + driver.getTitle());
driver.close();
}
}
This program was working fine in another laptop, but not working on new laptop/setup.
Can someone please help.
Error I'm getting :
Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.io.FileHandler.unzip(Ljava/io/InputStream;)Ljava/io/File;
at org.openqa.selenium.firefox.internal.FileExtension.obtainRootDirectory(FileExtension.java:82)
at org.openqa.selenium.firefox.internal.FileExtension.writeTo(FileExtension.java:59)
at org.openqa.selenium.firefox.internal.ClasspathExtension.writeTo(ClasspathExtension.java:64)
at org.openqa.selenium.firefox.FirefoxProfile.installExtensions(FirefoxProfile.java:443)
at org.openqa.selenium.firefox.FirefoxProfile.layoutOnDisk(FirefoxProfile.java:421)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:85)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:271)
at org.openqa.selenium.remote.RemoteWebDriver.startClient(RemoteWebDriver.java:303)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:125)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:157)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:218)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:120)
at bitbfw.PG1.main(PG1.java:11)
Found what was wrong..
it was that in "Java Build Path", the JRE system library was not added earlier. I went to configure Build path-->Add Jars selected "jRE system library", Applied & Closed.
Then on all the programs worked fine.

SocketException caught in Selenium HtmlUnitDriver program

I have written a small script that takes the default IP address for Epson printers we receive at my company and changes them automatically according to the requirement. This is done using Selenium HtmlUnitDriver.
The script gets the page, inserts the new IP, and than submits it. Because the IP changes once we submit a second time, the page is no longer 192.168.192.168, and the script does not want to complete.
Below is the script:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Main {
public static void main(String[] args) {
// Creating a new instance of the HTML unit driver.
WebDriver driver = new HtmlUnitDriver();
driver.get("http://192.168.192.168/ctcpip.htm");
// Find and change the IP Address field.
WebElement element = driver.findElement(By.name("IpAddress"));
element.clear();
element.sendKeys("192.168.192.169");
element.submit();
// Reset the printer. This changes it's IP as well, causing the initial driver page to no longer exist.
WebElement reset = driver.findElement(By.name("Submit"));
reset.submit();
// The script never gets this far.
driver.quit();
}
}
The script times out before it can complete. When the reset element is clicked, the initial URL of http://192.168.192.168/ctcpip.htm is effectively nonexistent, since we have changed it to 192.169.192.169. This is expected behavior, and the entire point of the program.
The console reads:
Nov 03, 2016 10:36:52 AM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.net.SocketException) caught when processing request to {}->http://192.168.192.168:80: Operation timed out
Nov 03, 2016 10:36:52 AM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {}->http://192.168.192.168:80
Exception in thread "main" java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connect to 192.168.192.168:80 [/192.168.192.168] failed: Operation timed out
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:739)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.processPostponedActions(JavaScriptEngine.java:820)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1325)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1268)
at com.gargoylesoftware.htmlunit.html.HtmlElement.click(HtmlElement.java:1216)
at org.openqa.selenium.htmlunit.HtmlUnitWebElement.submit(HtmlUnitWebElement.java:175)
at Main.main(Main.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 192.168.192.168:80 [/192.168.192.168] failed: Operation timed out
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:140)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:318)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:363)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:219)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:86)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at com.gargoylesoftware.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:178)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1313)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponse(WebClient.java:1230)
at com.gargoylesoftware.htmlunit.WebClient.getPage(WebClient.java:338)
at com.gargoylesoftware.htmlunit.WaitingRefreshHandler.handleRefresh(WaitingRefreshHandler.java:92)
at com.gargoylesoftware.htmlunit.html.HtmlPage.executeRefreshIfNeeded(HtmlPage.java:1446)
at com.gargoylesoftware.htmlunit.html.HtmlPage.initialize(HtmlPage.java:306)
at com.gargoylesoftware.htmlunit.WebClient.loadWebResponseInto(WebClient.java:475)
at com.gargoylesoftware.htmlunit.WebClient.loadDownloadedResponses(WebClient.java:2074)
at com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.doProcessPostponedActions(JavaScriptEngine.java:733)
... 11 more
Caused by: java.net.ConnectException: Operation timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.socket.PlainConnectionSocketFactory.connectSocket(PlainConnectionSocketFactory.java:72)
at org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:123)
... 29 more
Process finished with exit code 1
How do I tell my driver instance that it's totally cool the page changed so the process can exit properly?
The script needs to reach the driver.quit(); line.
Have you tried to call
driver.quit();
before
reset.submit();
? You no longer use the driver to retrieve any element, so you can quit before submitting the page.
I hope it helps.
If something doesn't work with findelement in Selenium, everyone uses js :).
How about this ?
driver.executeScript("window.document.getElementsByName("Submit")[0].click()");
Index could be wrong and need to be adjusted
Please try With the following Code :
It quits the driver in case of the provided Host or Host IPaddress is not available within the network.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Main {
public static void main(String[] args) {
// Creating a new instance of the HTML unit driver.
WebDriver driver = new HtmlUnitDriver();
try{
driver.get("http://192.168.192.168/ctcpip.htm");
WebElement element = driver.findElement(By.name("IpAddress"));
element.clear();
element.sendKeys("192.168.192.169");
element.submit();
// Reset the printer. This changes it's IP as well, causing the initial driver page to no longer exist.
WebElement reset = driver.findElement(By.name("Submit"));
reset.submit();
}
catch(HttpHostConnectException e){
System.out.println("Host Not Found : "+ e.getMessage())
}
finally{
driver.quit();
}
}
}

RMI tutorial errors with ComputeEngine

So I've spent quite awhile trying to find an answer from other people having trouble with the RMI tutorial, but I'm completely stumped on this. I'm doing this tutorial through eclipse.
My ComputeEngine Class. This is just copied from the tutorial, so I don't think there's anything wrong with it.
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import compute.Compute;
import compute.Task;
public class ComputeEngine implements Compute {
public ComputeEngine() {
super();
}
public <T> T executeTask(Task<T> t) {
return t.execute();
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "Compute";
Compute engine = new ComputeEngine();
Compute stub = (Compute) UnicastRemoteObject.exportObject(engine, 0);
Registry registry = LocateRegistry.getRegistry();
registry.rebind(name, stub);
System.out.println("ComputeEngine bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception:");
e.printStackTrace();
}
}
}
I start the rmiregistry in the command line with
set classpath=
start rmiregistry
My VM arguments in eclipse are:
-Djava.rmi.server.codebase=file:/C:/Users/Kevin/workspace/RMI/bin/
-Djava.rmi.server.hostname=Compute
-Djava.security.policy=server.policy
I have the compute.jar file and the the server.policy files in the bin folder. I granted all permissions for the policy file.
grant{
permission java.security.AllPermission;
};
After all that, I run ComputeEngine and get the following errors:
ComputeEngine exception:
java.security.AccessControlException: access denied (java.net.SocketPermission 127.0.0.1:1099 connect,resolve)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at engine.ComputeEngine.main(ComputeEngine.java:31)
It seems like it has some kind of problem with rebind, but I don't understand what. I also don't understand the AccessControlException, when I have the policy file. I've checked to make sure the rmiregistry is still running, and I don't close the empty window that comes up after starting it.
So yeah, I'm lost.
Clearly your security policy file isn't being found. It would need to be in the current working directory when you execute the program. Run your program with -Djava.security.debug=access,failure to see exactly what's going on.
The exception clearly says that your code base does not have permission to creat sockets/ for network communication. The reason could be with your security policy specification alone. Do not specify the policy file explicitly and allow the JVM to use the default security policy. The default policy specifies the right permissions so you should be fine.

Categories

Resources