java.lang.NullPointerException error while running test ng case - selenium

I am trying to run this code with TestNG but all it gives me is java.lang.NullPointerException error. Firefox does get opened but after that error comes up. What's going wrong. Please help!
Also is it mandatory to run testNG with XML.
public class GuestCheckout
{
public WebDriver driver ;
public String baseURL = "http://www.google.com";
#BeforeTest
public void setBaseURL(){
WebDriver driver = new FirefoxDriver();
driver.get(baseURL);
}
#Test(priority = 0)
public void EmailPasswordEntry() {
// Entering Email and Password values
driver.findElement(By.id("loginRegister")).click();
WebElement email = driver.findElement(By.id("head_username"));
email.clear();
email.sendKeys("abcd#gmail.com");
WebElement password = driver.findElement(By.id("head_password"));
password.clear();
password.sendKeys("123456");
driver.findElement(By.name("loginsubmit")).click();
}
#Test(priority = 1)
void AssertionVerification() {
// Verifying "My Account" text after login
String bodyText = driver.findElement(By.xpath("//span[#id='loginHeader']/descendant::a[text()='My Account']")).getText();
Assert.assertTrue(bodyText.contains("My Accountii"), "Not matched");
}
}
Error:
--------
FAILED: EmailPasswordEntry
java.lang.NullPointerException
at package_1.GuestCheckout.EmailPasswordEntry(GuestCheckout.java:31)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at `enter code here`org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
and same for the second Test method

Since you have instantiated WebDriver in local way and trying use that as a global instance so you are getting Null Pointer Exception. Do this in this way:
public WebDriver driver ;
public String baseURL = "http://www.ggolge.com";
#BeforeTest
public void setBaseURL(){
driver = new FirefoxDriver(); // Now this is a global instance for this class
driver.get(baseURL);
}
#Test(priority = 0)
public void EmailPasswordEntry() {
// Entering Email and Password values
driver.findElement(By.id("loginRegister")).click();
WebElement email = driver.findElement(By.id("head_username"));
email.clear();
email.sendKeys("abcd#gmail.com");
WebElement password = driver.findElement(By.id("head_password"));
password.clear();
password.sendKeys("123456");
driver.findElement(By.name("loginsubmit")).click();
}
#Test(priority = 1)
void AssertionVerification() {
// Verifying "My Account" text after login
String bodyText = driver.findElement(By.xpath("//span[#id='loginHeader']/descendant::a[text()='My Account']")).getText();
Assert.assertTrue(bodyText.contains("My Accountii"), "Not matched");
}
No it is not mandatory to run using TestNg xml, if you are using some intelligent IDE like Eclipse then you can run your tests without xml.
XMLs are beneficial when you have multiple classes to run all together as a suite.

since using webdriver... try using public static
public static webdriver driver
in each class files

Related

After executing code inside #BeforeTest its giving null pointer exception for #Test

After executing the#BeforeTest its showing java.lang.NullPointerException for #Test part.
Error displayed:
Starting ChromeDriver 2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8) on port 34173
Only local connections are allowed.
Sep 08, 2017 10:40:43 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED CONFIGURATION: #AfterTest Aftertest
java.lang.NullPointerException
at SampleTesting.Test1.Aftertest(Test1.java:71)
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:108)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:224)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:146)
at org.testng.TestRunner.afterRun(TestRunner.java:958)
at org.testng.TestRunner.run(TestRunner.java:606)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
FAILED: Test1
java.lang.NullPointerException
at SampleTesting.Test1.Test1(Test1.java:27)
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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
Code Used
package SampleTesting;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.*;
import java.util.Date;
import java.util.concurrent.TimeUnit;
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.ChromeOptions;
import org.openqa.selenium.support.ui.Select;
public class NameClassTest {
WebDriver driver;
ChromeOptions options;
#Test
public void Test1() {
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement tabAgreement = driver.findElement(By.xpath("***Value***"));
tabAgreement.click();
WebElement btnNew = driver.findElement(By.xpath("***Value***"));
btnNew.click();
Select lstRecordType = new Select(driver.findElement(By.id***Value***));
lstRecordType.selectByVisibleText("MSA");
WebElement btnContinue = driver.findElement(By.xpath("***Value***"));
btnContinue.click();
WebElement txtAgreementName = driver.findElement(By.xpath("***Value***"));
txtAgreementName.clear();
txtAgreementName.sendKeys("***Value***");
Date date = new Date();
System.out.println("Date is "+ date);
}
#BeforeTest
public void BeforeTest() {
options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\\\Users\\UserName\\AppData\\Local\\Google\\Chrome\\User Data");
String exepath =
"C:\\\\Users\\UserNAme\\Downloads\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",exepath);
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("***URL***");
WebElement userName = driver.findElement(By.id("username"));
WebElement password = driver.findElement(By.id("password"));
WebElement btnLogin = driver.findElement(By.id("Login"));
userName.sendKeys("****userName****");
password.sendKeys("****password****");
btnLogin.click();
}
#AfterTest
public void Aftertest() {
WebElement drpUserName = driver.findElement(By.id***Value***);
drpUserName.click();
WebElement lnkLogout = driver.findElement(By.xpath(***Value***));
lnkLogout.click();
driver.close();
}
}
TestNG.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite_SuiteName">
<test name="Test_TestName">
<classes>
<class name="SuiteName.ClassName"/>
</classes>
</test> <!-- Test_TestName -->
</suite> <!-- Suite_SuiteName -->
Its executing the block under #BeforeTest successfully but when its coming to #Test part its giving an error message - Null Pointer exception. I am not sure what mistake I made here in my code.
This is because you have mentioned WebDriver driver = new ChromeDriver(options); in #BeforeMethod annotated method. So your webdriver instance limited for that method only.
So if go in #test annotated method it will find driver not initialized so throw the NullPointerException
Just remove WebDriver from here
#BeforeTest
public void BeforeTest()
{
options = new ChromeOptions();
options.addArguments("user-data-dir=C:\\\\Users\\UserName\\AppData\\Local\\Google\\Chrome\\User Data");
String exepath = "C:\\\\Users\\UserNAme\\Downloads\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver",exepath);
driver = new ChromeDriver(options); // remove "WebDriver" from this line
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("***URL***");
Let me know if still have issue. Thanks :)
In your #BeforeTest annotated method you have this line
WebDriver driver = new ChromeDriver(options);
This causes your code to shadow out the class level data member and so when your #Test methods try accessing the driver, you hit a NullPointerException.
To fix this, please change WebDriver driver = new ChromeDriver(options); to driver = new ChromeDriver(options); in your #BeforeTest annotated method.
On a side note, I would recommend that you either use one of the following instead of using the #BeforeTest because #BeforeTest methods are called only once per <test> tag. So if you have two test classes which are relying on the #BeforeTest method, then you are again looking at NullPointerException
#BeforeClass - so that the webdriver is instantiated once per test class and is then shared by all the #Test methods in the class. The issue would be that you would need to resort to sequential execution only, because parallel execution can cause test methods to step on each other's shoes.
#BeforeMethod - so that the webdriver is instatiated once for every #Test method. This causes a browser to be spun off for every test method. The flip side of this would be that you would need to use ThreadLocal if you want run the methods in parallel. If sequential execution is fine, then you don't need to do anything extra.
Try creating object of WebDriver outside #BeforeTests
as mentioned below,
public class nameClassTest {
WebDriver driver = new ChromeDriver(options);
#BeforeTest
public void BeforeTest()
{
}
that will resolve your issue.

Null pointer exception in TestNG

My code is:
package ant;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
public class NewTestNG {
public WebDriver driver;
#BeforeMethod
public void LAunchbrowser() {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void main() {
Actions action = new Actions(driver);
WebElement a= driver.findElement(By.xpath(".//*[#id='gs_htif0']"));
action.moveToElement(a).click().sendKeys("Shirt").build().perform();
driver.findElement(By.xpath("//div[#value='Search']")).click();
}
}
I'm getting NullPointerException:
FAILED: main
java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:44)
at ant.NewTestNG.main(NewTestNG.java:25)
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:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================
What's wrong with my code?
In LaunchBrowser() you declared driver again instead of using the class instance. Change it to
#BeforeMethod
public void LaunchBrowser() {
driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
and it should work. The problem is that when you declared driver in LaunchBrowser(), the scope of that variable is inside the method only so the class variable, driver doesn't get used. So when you are outside of the method and try to reference driver, it's null... thus the exception.
You really need to spend some time learning how to debug your own programs. If you put a break point at the start of the script and stepped through it, you should have been able to find this yourself.

How can I setup Fluentlenium to run in different browser drivers?

I am trying to run Fluentlenium in different browser drivers. I think I need to configure the getDefaultDriver() from Fluentlenium but I am not exactly sure on how to do that. Any exmaple codes would be great. Here is my code and it's not working. The message I am getting in Eclipse is:
"java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:75)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:107)
at com.picklist.tests.PicklistCreate.(PicklistCreate.java:32)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
"
My Code below:
public WebDriver driver = new ChromeDriver();
// Overrides the default driver
#Override
public WebDriver getDefaultDriver() {
System.setProperty("webdriver.chrome.driver", "C:/chromeDriver/chromedriver.exe"); // Set for ChromeDriver
//return driver;
return driver;
}
If I do the following code, it works but then the driver is no longer defined and I got a ton od code that uses driver.xxx:
// Overrides the default driver
#Override
public WebDriver getDefaultDriver() {
return new ChromeDriver();
}
Here is how I solved this:
public WebDriver driver;
// Overrides the default driver
#Override
public WebDriver getDefaultDriver() {
System.setProperty("webdriver.chrome.driver", "C:/chromeDriver/chromedriver.exe"); // Set for ChromeDriver
driver = new ChromeDriver();
return driver;
}
There is not getDefaultDriver in FluentTest, you can use this code:
#Override
public WebDriver newWebDriver() {
System.setProperty("webdriver.chrome.driver", "path-to-chrome-driver/chromedriver");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("binary", "/usr/bin/chromium-browser");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
return new ChromeDriver(capabilities);
}
Have an abstract createDriver() function that is overridden by each type of driver. Each driver should return a correctly configured driver, which is then stored, and returned when you call getDefaultDriver();

Does selenium webdriver support Struts 2

I want to use Selenium WebDriver API using Struts2 Framework. So will it be supported or not.
If not then what should I do.
It means, suppose I have an textfield on my jsp, linked with action class and I want search on www.google.com with my textfield value by sendKeys to element "q".
What am trying to do is
index.jsp :
<s:form action="test">
<s:textfield name="search" label="Enter Search "></s:textfield>
</s:form>
</body>
struts.xml:
<action name="test" class="com.actions.TestAction">
<result name="SUCCESS">/success.jsp</result>
</action>
ActionClass :
package com.actions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport
{
private String search;
public String getSearch() {
return search;
}
public void setSearch(String search) {
this.search = search;
}
private static final long serialVersionUID = -1241657564582564726L;
#Override
public String execute() throws Exception {
WebDriver driver = new HtmlUnitDriver();
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys(getSearch());
element.submit();
System.out.println("Page title is: " + driver.getTitle());
System.out.println(driver.getPageSource());
driver.quit();
return "SUCCESS";
}
}
Error :
SEVERE: Exception starting filter struts2
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.getConstructor(Unknown Source)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.verifyAction(XmlConfigurationProvider.java:428)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:378)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:495)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:286)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:112)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:234)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:193)
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:278)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:259)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:383)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:104)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4650)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5306)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
eagerly waiting for your replies.
Thank You !
Until someone comes along with a better answer... I've never used struts directly but I think I have a basic idea of what it does.
Selenium operates on the front end, as the user would. I believe struts is a back-end only technology, which means that the browser doesn't even know it's receiving something assembled by struts. Struts just helps the server assemble the html, css, and javascript.
Selenium operates well with html, javascript, and css, and since that's what your server is sending out, it should all be good, it should "support it" just fine!

Encountering error: java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList$Builder

I'm new to Selenium WebDriver using EclipseIDE with TestNG. I'm currently running this sample code in Eclipse via TestNG:
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.List;
public class CheesecakeFactory {
FirefoxDriver driver;
#BeforeTest
public void startDriver() {
driver = new FirefoxDriver();
}
#AfterTest
public void stopDriver() {
driver.close();
}
#Test
public void listCheesecakes() {
driver.get("http://www.thecheesecakefactory.com/");
driver.findElement(By.linkText("Menu")).click();
driver.findElement(By.linkText("Cheesecake")).click();
List<WebElement> cheesecakes = driver.findElements(By.xpath("id('leftNav_levelTwo')//li"));
System.out.println(cheesecakes.size() + " cheesecakes:");
for (int i=0; i<cheesecakes.size(); i++) {
System.out.println(i+1 + ". " + cheesecakes.get(i).getText());
}
}
}
But Eclipse returns this:
[TestNG] Running:
C:\Users\ryan\AppData\Local\Temp\testng-eclipse--616826937\testng-customsuite.xml
FAILED CONFIGURATION: #BeforeTest startDriver
java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableList$Builder
at org.openqa.selenium.os.WindowsUtils.getPathsInProgramFiles(WindowsUtils.java:275)
at org.openqa.selenium.firefox.internal.Executable.locateFirefoxBinaryFromPlatform(Executable.java:148)
at org.openqa.selenium.firefox.internal.Executable.<clinit>(Executable.java:25)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:60)
at org.openqa.selenium.firefox.FirefoxBinary.<init>(FirefoxBinary.java:56)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:78)
at CheesecakeFactory.startDriver(CheesecakeFactory.java:16)
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:80)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at org.testng.TestRunner.beforeRun(TestRunner.java:641)
at org.testng.TestRunner.run(TestRunner.java:609)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)
at org.testng.TestNG.run(TestNG.java:1030)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
I don't understand why I'm getting this error. I've done the following:
Added the guava-12.0.jar file (along with the other jar files in the Selenium-2.25.0 webdriver) as an external jar file in Eclipse. (This jar file contains the ImmutableList$Builder class)
Added the path of this jar file in the CLASSPATH (Environment Variables>System Variables)
Am I missing something? Any help is greatly appreciated.
I guess you are using selenium-java-2.25.0.jar. You should rather use selenium-server-standalone-2.25.0.jar, it will take care of all the dependencies (i.e. required jar files).
Also you dont need to explicitly define the environment variables if the jar files are added in the Eclipse, unless you are running the test outside from eclipse.
Hope this helps... :)