Trying to run a simple Selenium signup test ERROR - java

Getting this error while running a simple test.
> java -version
java version "1.8.0_102"
> compiler version javac -version
javac 1.8.0_102
Exception in thread "main" java.lang.UnsupportedClassVersionError: org/openqa/selenium/WebDriver : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getMethod0(Class.java:2856)
at java.lang.Class.getMethod(Class.java:1668)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Here's the code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CustomerSignUpTest {
public static void main(String[] args) {
WebDriver selenium = new ChromeDriver();
selenium.get("http://www.cvs.com");
WebElement signuplink = null;
signuplink.findElement(By.partialLinkText("singup"));
WebElement Clicklink = null;
Clicklink.click();

I am getting "Access Denied" error after website open. Still try if below code works for you -
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.IOException;
public class test {
#SuppressWarnings("null")
public static void main(String[] args) {
String Browser_Full_path = Driver.APP_PATH + "\\Support JAR\\32 bit\\BrowserDrivers\\" + "chromedriver.exe";
System.out.println(" browser full path => " + Browser_Full_path);
System.setProperty("webdriver.chrome.driver", Browser_Full_path);
ChromeDriverService cds = ChromeDriverService.createDefaultService();
try {
cds.start();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.cvs.com");
WebElement signuplink = null;
signuplink.findElement(By.partialLinkText("signup"));
WebElement Clicklink = null;
Clicklink.click();
}
}

Had same issue. Removed older Java JDKs from the system, set to build with Java8 and worked like magic.

Related

org.testng.TestNGException : Cannot instantiate class

I am facing this 'Cannot insantiate class' error on running one of my test cases in selenium webdriver using java(Using Maven project).
Below is the parent class where I defined driver and properties
package com.pvi.qa.base;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class TestBase {
public static WebDriver driver;
public static Properties prop ;
public TestBase() {
try {
prop = new Properties();
FileInputStream ip = new FileInputStream(System.getProperty("C:\\Users\\RxLogix\\eclipse-workspace\\PviIntake\\src\\main\\java\\com\\pvi\\qa\\config\\config.properties"));
prop.load(ip);
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void initialization () {
String BrowserName = prop.getProperty("browser");
if(BrowserName.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32 (1)\\chromedriver.exe");
driver = new ChromeDriver();}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
}
}
Below is the test cases class which I am running through TestNG
package com.pvi.qa.testcases;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.pvi.qa.base.TestBase;
import com.pvi.qa.pages.HomePage;
import com.pvi.qa.pages.LoginPage;
public class LoginPageTest extends TestBase{
LoginPage loginPage;
HomePage homePage;
public LoginPageTest() {
super();
}
#BeforeMethod
public void setUp() {
initialization();
loginPage = new LoginPage();
}
#Test
public void logintest() {
homePage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
}
#AfterMethod
public void tearDown() {
driver.quit();
}
}
And below is the error I am getting -
[RemoteTestNG] detected TestNG version 6.14.3
org.testng.TestNGException:
Cannot instantiate class com.pvi.qa.testcases.LoginPageTest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:423)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:190)
at org.testng.TestClass.getInstances(TestClass.java:95)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:81)
at org.testng.TestClass.init(TestClass.java:73)
at org.testng.TestClass.<init>(TestClass.java:38)
at org.testng.TestRunner.initMethods(TestRunner.java:389)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
... 25 more
Caused by: java.lang.NullPointerException
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at java.base/java.io.FileInputStream.<init>(Unknown Source)
at com.pvi.qa.base.TestBase.<init>(TestBase.java:20)
at com.pvi.qa.testcases.LoginPageTest.<init>(LoginPageTest.java:17)
... 30 more
I have tried everything but not worked for me, Please let me know why I am getting this error. Thanks
The lowest 'caused by' section points you to the underlying exception in your base class constructor: you are calling System.getProperty with a file path; that will probably return null. To me it looks like the whole call shouldn't be there, and you just want to pass the file path to the FileInputStream constructor (or read it from a system property with some key)
Looking at the code, I understand the issue is in the constructor of the base class.
LoginPageTest calls super() which invokes TestBase.
FileInputStream needs a file path. I suspect FileInputSteam object created is coming as null.
Why are you calling System.getProperty? Is the config.properties file available?
Just Remove System.getproperty from your base class. Use this lines of code
prop = new Properties();
FileInputStream ip = new FileInputStream(("C:\\Users\\RxLogix\\eclipse-workspace\\PviIntake\\src\\main\\java\\com\\pvi\\qa\\config\\config.properties"));
prop.load(ip);

Creating object throws NullPointerException

I've the below code in Java that throws java.lang.NullPointerException at the line where I create new instance.
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(final String... args) {
Properties seleniumProperties = new Properties();
seleniumProperties.setProperty("webdriver.gecko.driver", "<PATH_TO_DRIVER>");
System.setProperties(seleniumProperties);
WebDriver driver = new FirefoxDriver();
}
}
Line no. 14 is WebDriver driver = new FirefoxDriver(); and here's the stack trace of the exception:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.startsWith(String.java:1405)
at java.lang.String.startsWith(String.java:1434)
at java.util.jar.JarFile.isKnownNotToHaveSpecialAttributes(JarFile.java:594)
at java.util.jar.JarFile.checkForSpecialAttributes(JarFile.java:552)
at java.util.jar.JarFile.hasClassPathAttribute(JarFile.java:518)
at java.util.jar.JavaUtilJarAccessImpl.jarFileHasClassPathAttribute(JavaUtilJarAccessImpl.java:37)
at sun.misc.URLClassPath$JarLoader.getClassPath(URLClassPath.java:1186)
at sun.misc.URLClassPath.getLoader(URLClassPath.java:522)
at sun.misc.URLClassPath.getNextLoader(URLClassPath.java:484)
at sun.misc.URLClassPath.getResource(URLClassPath.java:238)
at java.net.URLClassLoader$1.run(URLClassLoader.java:365)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Test.main(Test.java:14)
I think I found the problem. Using System.setProperties probably removes all crucial properties used by JVM. I replaced the code with System.setProperty as shown below and now it works fine.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(final String... args) {
/*
Properties seleniumProperties = new Properties();
seleniumProperties.setProperty("webdriver.gecko.driver", "<PATH_TO_DRIVER>");
System.setProperties(seleniumProperties);
*/
System.setProperty("webdriver.gecko.driver", "<PATH_TO_DRIVER>");
WebDriver driver = new FirefoxDriver();
}
}

noClassDefFoundError with the Selenium webDriver

i just start using selenium and i've made this little test to try it.
Here the code :
package selenium.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SeleniumTest {
static WebDriver driver;
static Wait<WebDriver> wait;
public static void main(String[] args) {
driver = new FirefoxDriver();
wait= new WebDriverWait(driver, 30);
driver.get("http://www.google.com/");
boolean result;
try {
driver.findElement(By.name("q"))
.sendKeys("J'aime bien les tests");
driver.findElement(By.name("btnG")).click();
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
System.out.println("Searching ....");
return webDriver
.findElement(By.id("resultStats")) != null;
}
});
result=driver.findElement(By.tagName("body"))
.getText()
.contains("https://en.wikipedia.org
/wiki/Software_testing");
} finally {
driver.close();
}
System.out.println("The test " +
(result ? "succed ! " : "failed"));
}
}
But when i try to run it i have this noClassDefFoundException :
Exception in thread "main" java.lang.NoClassDefFoundError:
org/open/selenium/WebDriver
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getMethod0(Class.java:2856)
at java.lang.Class.getMethod(Class.java:1668)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper
.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more
I'm using eclispe mars, and i don't anderstand because i have added the jar to the buildpahth of the project. Here the jars i've added : selenium jars
I took the selenium-java-2.53.0.jar and the selenium-java-2.53.0-srcs.jar and all the jar in the libs directory.
To add the jars i've done:
Right click on the project
Build Path
Add external jars
Select all the jars mentioned above.
Apply
I can compile the class in eclipse and run it (only in eclipse) but when i want to do it with a terminal it's not working.
What am i doing wrong ?
EDIT :
I managed to compile the program by using the command :
javac -cp C:\selenium-2.53.0\*;C:\selenium-2.53.0\libs\* src\selenium\test\SeleniumTest.java
But then when i want to run it with the command :
java -cp C:\selenium-2.53.0\*;C:\selenium-2.53.0\libs\*;. selenium.test.SeleniumTest
It tells me that my class can't be found.

Issues converting docx to PDF in java

I am trying to convert a word docx file to a pdf file, all using java, and without any user interaction.
This is my code so far, I am using the docx4j library.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package richard.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.docx4j.convert.out.pdf.PdfConversion;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
/**
*
* #author Richard
*/
public class pdfConverter {
public static void main(String[] args) {
createPDF();
createPDF();
}
private static void createPDF() {
try {
long start = System.currentTimeMillis();
// 1) Load DOCX into WordprocessingMLPackage
InputStream is = new FileInputStream(new File(
"D:/HelloWorld.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(is);
// 2) Prepare Pdf settings
PdfSettings pdfSettings = new PdfSettings();
// 3) Convert WordprocessingMLPackage to Pdf
OutputStream out = new FileOutputStream(new File(
"D:/HelloWorld.pdf"));
PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
wordMLPackage);
converter.output(out, pdfSettings);
System.err.println("Generate pdf/HelloWorld.pdf with "
+ (System.currentTimeMillis() - start) + "ms");
} catch (Throwable e) {
e.printStackTrace();
}
}
}
However I am getting this error when I try to run, it compiles just fine
run:
java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at org.docx4j.openpackaging.Base.<clinit>(Base.java:42)
at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:32)
at richard.fileupload.pdfConverter.main(pdfConverter.java:21)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 3 more
java.lang.NoClassDefFoundError: Could not initialize class org.docx4j.openpackaging.packages.WordprocessingMLPackage
at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:32)
at richard.fileupload.pdfConverter.main(pdfConverter.java:22)
BUILD SUCCESSFUL (total time: 0 seconds)
any idea what the error is and what is the cause of this?
EDIT I am now getting this error
java.lang.NoClassDefFoundError: org/apache/fop/apps/FopFactory
at org.docx4j.convert.out.pdf.viaXSLFO.Conversion.output(Conversion.java:231)
at richard.fileupload.pdfConverter.createPDF(pdfConverter.java:43)
at richard.fileupload.pdfConverter.main(pdfConverter.java:22)

NoClassDefFoundError when executing a Neo4j Cypher query in Java

I tried the following basic example about executing Cypher queries from Java in embedded mode as is, but it shows the errors below:
Code:
package test;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class Test {
public static void main(String[] args) {
GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase("D:/MI/Tools/neo4j-community-1.9.M02/test2");
// add some data first, keep id of node so we can refer to it
long id;
Transaction tx = db.beginTx();
try {
Node refNode = db.createNode();
id = refNode.getId();
refNode.setProperty("name", "reference node");
tx.success();
} finally {
tx.finish();
}
// let's execute a query now
ExecutionEngine engine = new ExecutionEngine(db);
ExecutionResult result = engine.execute("start n=node(" + id + ") return n, n.name");
System.out.println(result.toString());
}
}
Output:
Exception in thread "main" java.lang.NoClassDefFoundError: com/googlecode/concurrentlinkedhashmap/ConcurrentLinkedHashMap$Builder
at org.neo4j.cypher.internal.LRUCache.<init>(LRUCache.scala:30)
at org.neo4j.cypher.ExecutionEngine$$anon$1.<init>(ExecutionEngine.scala:91)
at org.neo4j.cypher.ExecutionEngine.<init>(ExecutionEngine.scala:91)
at org.neo4j.cypher.javacompat.ExecutionEngine.<init>(ExecutionEngine.java:54)
at org.neo4j.cypher.javacompat.ExecutionEngine.<init>(ExecutionEngine.java:44)
at test.Test.main(Test.java:27)
Caused by: java.lang.ClassNotFoundException: com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap$Builder
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 6 more
Java Result: 1
BUILD SUCCESSFUL (total time: 5 seconds)
Is there any problem in the code?
(I use neo4j-community-1.9.M02 and NetBeans IDE 7.2.1)
Thanks.
The problem disappeared after adding the following Java library to the project
http://concurrentlinkedhashmap.googlecode.com/files/concurrentlinkedhashmap-lru-1.3.1.jar

Categories

Resources