noClassDefFoundError with the Selenium webDriver - java

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.

Related

NullPointerException #FindBy in PageObjectModel using ChromeDriver and Chrome through Selenium

I am trying to implement PageObjectModel in Selenium. But I am getting NullPointer Exception at #FindBy. I hope someone can help me to identify my mistake. I created my Base class which sets config.properties file(it contains url,driver parameters) as well sets up driver. HomePage has object repository as well as actions and PageFactory is initialized. HomePageTest has all the tests. When I run the code, website is launched successfully. First test executes successfully. But at the second test it fails at FindBy.
Here is my code for reference.
Following is my Base class:
package com.seleniumEasy.qa.base;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import com.seleniumEasy.qa.commonUtility.*;
public class Base {
public static WebDriver driver;
public static Properties prop;
#BeforeClass
public void setBaseline()
{
try {
setProperties("\\Devp\\SeleniumEasy_Maven_POM_DataDrivenProject\\src\\main\\java\\com\\seleniumEasy\\qa\\config\\config.properties");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//setProperties("\\Devp\\SeleniumEasy_Maven_POM_DataDrivenProject\\src\\main\\java\\com\\seleniumEasy\\qa\\config\\config.properties");
setDriver();
}
/*#AfterClass
public void tearDown()
{
driver.quit();
}*/
public static void setProperties(String sFilePath) throws IOException
{
prop = new Properties();
FileInputStream fip = new FileInputStream(sFilePath);
prop.load(fip);
}
public static void setDriver()
{
switch(prop.getProperty("browser").toLowerCase())
{
case "chrome":
System.setProperty(prop.getProperty("chromekey"),prop.getProperty("chromedriverpath"));
driver = new ChromeDriver();
break;
case "firefox":
System.setProperty(prop.getProperty("firefoxkey"), prop.getProperty("firefoxdriverpath"));
driver = new FirefoxDriver();
break;
case "ie":
System.setProperty(prop.getProperty("iekey"),prop.getProperty("iedriverpath"));
driver = new InternetExplorerDriver();
break;
case "edge":
System.setProperty(prop.getProperty("edgekey"), prop.getProperty("edgedriverpath"));
driver = new EdgeDriver();
break;
default:
System.out.println("Driver does not exists: "+ prop.getProperty("browser"));
}
driver.manage().timeouts().implicitlyWait(commonUtil.Implicit_Wait, TimeUnit.SECONDS);
//driver.manage().timeouts().pageLoadTimeout(commonUtil.PageLoad_Wait,TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
//driver.manage().window().maximize();
if(driver.findElement(By.partialLinkText("No, thanks!")).isDisplayed())
driver.findElement(By.partialLinkText("No, thanks!")).click();
}
}
Following is my Page file
package com.seleniumEasy.qa.pages;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.seleniumEasy.qa.base.Base;
public class HomePage extends Base {
#FindBy(xpath="//*[#id=\"navbar-brand-centered\"]/ul[1]/li[1]/a")
WebElement InputForm;
#FindBy(css="#navbar-brand-centered > ul:nth-child(1) > li.dropdown.open")
List<WebElement> InputFormLst;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:first-of-type")
WebElement simpleFormDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(2)")
WebElement checkBoxDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(3)")
WebElement radioBtnDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(4)")
WebElement dropDownLstDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(5)")
WebElement inputFrmSubmit;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(6)")
WebElement ajaxForm;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(6)")
WebElement jQuerySelect;
List<WebElement> inputFormList = new ArrayList();
public HomePage()
{
PageFactory.initElements(driver, this);
}
public String validateTitle()
{
return driver.getTitle();
}
public SimpleForm validateSimpleFormDemo()
{
InputForm.click();
//driver.findElement(By.cssSelector("div.navbar-collapse.collapse.in>ul.nav.navbar-nav>li.dropdown.open>a")).click();
//driver.findElement(By.xpath("//*[#id=\"navbar-brand-centered\"]/ul[1]/li[1]/a")).click();
simpleFormDemo.click();
return new SimpleForm();
}
}
Following is my Test Page
package com.seleniumEasy.qa.Test;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import com.seleniumEasy.qa.base.Base;
import com.seleniumEasy.qa.pages.HomePage;
import com.seleniumEasy.qa.pages.SimpleForm;
public class HomePageTest extends Base {
HomePage homePage;
SoftAssert softAssert;
public HomePageTest()
{
homePage = new HomePage();
softAssert = new SoftAssert();
}
#Test(priority=1)
public void validateLoginPage()
{
String sTitle = homePage.validateTitle();
softAssert.assertEquals(sTitle, "Selenium Easy Demo");
}
#Test(priority=1)
public void validateSimpleForm()
{
SimpleForm simpleForm = homePage.validateSimpleFormDemo();
}
}
And here is the stacktrace
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 32000
Only local connections are allowed.
[1599758981.703][WARNING]: Timed out connecting to Chrome, retrying...
Sep 10, 2020 10:59:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite\Default test.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite exists: true
FAILED: validateSimpleForm
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy8.click(Unknown Source)
at com.seleniumEasy.qa.pages.HomePage.validateSimpleFormDemo(HomePage.java:66)
at com.seleniumEasy.qa.Test.HomePageTest.validateSimpleForm(HomePageTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1129)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:746)
at org.testng.TestRunner.run(TestRunner.java:600)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1264)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1189)
at org.testng.TestNG.runSuites(TestNG.java:1104)
at org.testng.TestNG.run(TestNG.java:1076)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
[TestNG] Time taken by org.testng.reporters.EmailableReporter2#42d3bd8b: 8 ms
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\testng-failed.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite\testng-failed.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\Default suite exists: true
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 9 ms
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\toc.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\Default test.properties
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\index.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\main.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\groups.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\classes.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\reporter-output.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\methods-not-run.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite\testng.xml.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\Default suite exists: true
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old\index.html
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\old exists: true
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter#6cd8737: 41 ms
[TestNG] Time taken by org.testng.reporters.jq.Main#6aaa5eb0: 46 ms
[Utils] Attempting to create C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\junitreports\TEST-com.seleniumEasy.qa.Test.HomePageTest.xml
[Utils] Directory C:\Arundhati\Devp\SeleniumEasy_Maven_POM_DataDrivenProject\test-output\junitreports exists: true
[TestNG] Time taken by org.testng.reporters.JUnitReportReporter#3b81a1bc: 6 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter#6b2fad11: 12 ms
Here is the eclipse view
This error message...
Starting ChromeDriver 2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e) on port 32000
Only local connections are allowed.
[1599758981.703][WARNING]: Timed out connecting to Chrome, retrying...
Sep 10, 2020 10:59:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using chromedriver=2.41
Release Notes of chromedriver=2.41 clearly mentions the following :
Supports Chrome v67-69
Possibly you are using the latest chrome=85.0
Release Notes of ChromeDriver v85.0 clearly mentions the following :
Supports Chrome version 85
So there is a clear mismatch between ChromeDriver v2.41 and the Chrome Browser v85.0
Solution
Ensure that:
JDK is upgraded to current levels JDK 8u252.
Selenium is upgraded to current released Version 3.141.59.
ChromeDriver is updated to current ChromeDriver v84.0 level.
Chrome is updated to current Chrome Version 85.0 level. (as per ChromeDriver v85.0 release notes)
If your base Web Client version is too old, then uninstall it and install a recent GA and released version of Web Client.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
Take a System Reboot.
Execute your #Test as non-root user.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
Finally I found a solution to my own problem. NullPointerException is thrown when the driver object is null when the PageFactory is initialized.
The problem was before initializing the driver object I was calling PageFactory Initialization method. Thats why at the HomePage.java page in the constructor, driver was null.
Here is the updated code.
This is my Base class. I am initializing the cofig.properties file through the Base class constructor.
/**
*
*/
package com.seleniumEasy.qa.base;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import com.seleniumEasy.qa.commonUtility.*;
public class Base {
public static WebDriver driver;
public static Properties prop;
public WebDriver getDriver()
{
return driver;
}
public Base()
{
try
{
setProperties("\\Arundhati\\Devp\\SeleniumEasy_Maven_POM_DataDrivenProject\\src\\main\\java\\com\\seleniumEasy\\qa\\config\\config.properties");
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*#AfterClass
public void tearDown()
{
driver.quit();
}*/
public static void setProperties(String sFilePath) throws IOException
{
prop = new Properties();
FileInputStream fip = new FileInputStream(sFilePath);
prop.load(fip);
}
public static void setDriver()
{
if(prop.getProperty("browser").toLowerCase().equals("chrome"))
{
System.setProperty(prop.getProperty("chromekey"),prop.getProperty("chromedriverpath"));
driver = new ChromeDriver();
}
else if(prop.getProperty("browser").toLowerCase().equals("firefox"))
{
System.setProperty(prop.getProperty("firefoxkey"), prop.getProperty("firefoxdriverpath"));
driver = new FirefoxDriver();
}
else if(prop.getProperty("browser").toLowerCase().equals("ie"))
{
System.setProperty(prop.getProperty("iekey"),prop.getProperty("iedriverpath"));
driver = new InternetExplorerDriver();
}
else if(prop.getProperty("browser").toLowerCase().equals("edge"))
{
System.setProperty(prop.getProperty("edgekey"), prop.getProperty("edgedriverpath"));
driver = new EdgeDriver();
}
else
{
System.out.println("Driver does not exists: "+ prop.getProperty("browser"));
}
driver.manage().timeouts().implicitlyWait(commonUtil.Implicit_Wait, TimeUnit.SECONDS);
//driver.manage().timeouts().pageLoadTimeout(commonUtil.PageLoad_Wait,TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
//driver.manage().window().maximize();
if(driver.findElement(By.partialLinkText("No, thanks!")).isDisplayed())
driver.findElement(By.partialLinkText("No, thanks!")).click();
}
public String getProperty(String key)
{
return prop.getProperty(key);
}
}
Following is code for my HomePage.java. No change in this file.
/**
*
*/
package com.seleniumEasy.qa.pages;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.seleniumEasy.qa.base.Base;
public class HomePage extends Base {
#FindBy(xpath="//*[#id=\"navbar-brand-centered\"]/ul[1]/li[1]/a")
WebElement InputForm;
#FindBy(css="#navbar-brand-centered > ul:nth-child(1) > li.dropdown.open")
List<WebElement> InputFormLst;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:first-of-type")
WebElement simpleFormDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(2)")
WebElement checkBoxDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(3)")
WebElement radioBtnDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(4)")
WebElement dropDownLstDemo;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(5)")
WebElement inputFrmSubmit;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(6)")
WebElement ajaxForm;
#FindBy(css="ul.nav.navbar-nav>li.dropdown.open>ul.dropdown-menu>li:nth-child(6)")
WebElement jQuerySelect;
//List<WebElement> inputFormList = new ArrayList();
public HomePage()
{
PageFactory.initElements(driver, this);
}
public String validateTitle()
{
return driver.getTitle();
}
public SimpleForm validateSimpleFormDemo()
{
InputForm.click();
simpleFormDemo.click();
return new SimpleForm();
}
}
Here is my HomePageTest file. I am calling the driver setup method following by creating the HomePage object in the BeforeClass annotation. No constructor in this class.
//package com.seleniumEasy.qa.testdata;
package com.seleniumEasy.qa.Test;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import com.seleniumEasy.qa.base.Base;
import com.seleniumEasy.qa.pages.HomePage;
import com.seleniumEasy.qa.pages.SimpleForm;
public class HomePageTest extends Base {
HomePage homePage;
SoftAssert softAssert;
#BeforeClass
public void setUp()
{
setDriver();
homePage = new HomePage();
//PageFactory.initElements(driver, HomePage.class);
softAssert = new SoftAssert();
}
/*#AfterClass
public void tearDown()
{
driver.quit();
}*/
#Test(priority='b')
public void validateLoginPage()
{
String sTitle = homePage.validateTitle();
softAssert.assertEquals(sTitle, "Selenium Easy Demo");
}
#Test(priority='a')
public void validateSimpleForm()
{
SimpleForm simpleForm = homePage.validateSimpleFormDemo();
}
}

Setting pkcs11 cfg file

I am trying to run an example of how to use pkcs11 using the next code
import java.lang.reflect.Constructor;
import java.security.KeyStore;
import java.security.Provider;
import java.security.Security;
import java.util.Enumeration;
public class SignWithPKCS11
{
public static void main (String [] args)
{
try
{
Class<?> pkcs11Class = Class.forName("sun.security.pkcs11.SunPKCS11");
Constructor<?> construct = pkcs11Class.getConstructor(new Class[] {String.class});
String configName = "pkcs11.cfg";
Provider p = (Provider)construct.newInstance(new Object[] {configName});
Security.addProvider(p);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
}
The code above, calls the file pkcs11.cfg. The content of it is
name = JSignPdf
library = /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
using this command
java -cp . SignWithPKCS11
java -Djava.library.path=".:/home/grados-sanchez/grive/E-CONNECTING/david" -cp . SignWithPKCS11
but I get
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at SignWithPKCS11.main(SignWithPKCS11.java:32)
Caused by: java.security.ProviderException: Initialization failed
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:376)
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:103)
Caused by: java.io.IOException: libopensc.so.6: cannot open shared object file: No such file or directory/home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
Could you help me, to fix it, please? What is wrong?
NOTE: When I did:
ls -l /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
I get
-rw-rw-r-- 1 user user 220232 Jun 19 01:59 /home/grados-sanchez/grive/E-CONNECTING/david/opensc-pkcs11.so
It seems that it's not the opensc-pkcs11.so that is missing, but a dependency of that library. Use ldd to check those dependencies. If they reside in the same directory as the provider, you need to set LD_LIBRARY_PATH=/home/grados-sanchez/grive/E-CONNECTING/david before executing the command (as a prefix, for example).

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);

Trying to run a simple Selenium signup test ERROR

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.

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