org.testng.testngexception cannot instantiate class cucmber-testng error - selenium

I am trying to use testNG framework in cucumber. But getting below error.
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD FAILURE
[INFO] Total time: 8.032 s
[INFO] Finished at: 2016-08-02T16:11:29+05:30
[INFO] Final Memory: 18M/220M
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test (default-test) on project TestCaseAPI: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: There was an error in the forked process
[ERROR] org.testng.TestNGException:
[ERROR]
[ERROR] Cannot instantiate class com.XXX.automation.feature.RunCuke1Test
[ERROR] at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:40)
My runner class is as follow:
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import cucumber.api.testng.AbstractTestNGCucumberTests;
#RunWith(Cucumber.class)
#CucumberOptions (features = "src/test/resources/feature"
,glue="com.XXX.automation.feature"
,monochrome=true
)
public class RunCuke1Test extends AbstractTestNGCucumberTests {
}
My testNG.xml is like below
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test name="Test diary">
<classes>
<class name="com.XXX.automation.feature.RunCuke1Test"/>
</classes>
</test>
</suite>
My project structure is as above:
I am running it through Maven
Can anyone tell me what I am doing wrong here?

I did recheck on cucumber-testNG maven dependency. Earlier I was using older version. I am now using the latest version and now my program is running successfully now.

Related

Unknown Option: -protocol error while using selenium webdriver

I get the below exception while trying to execute testng.xml using chromedriver
Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -protocol
I have chromedriver.exe in my local and works fine for non-testng tests. Has someone faced a similar issue.
This is nowhere related to the Chromedriver. It is the issue with the parameter named protocol` that you are not passing to the test method through the testng.xml file. For ex.
#Parameter("protocol")
#Test
void sampleTest(String protocol){
//your code
}
Expected testng.xml file
<suite name="Sample Suite">
<test name="Sample Test">
<parameter name="protocol" value="expectedvalue">
</test> <!-- test -->
</suite>
Some time to avoid this situation, we use #Optional attribute
#Parameter("protocol")
#Test
void sampleTest(#Optional("http")String protocol){
//your code
}

org.testng.TestNGException: Parameter 'browser' is required by #Configuration on method MethodSetup

ENV: Seleniuim web driver, testng, eclipse, java 1.8
I have a test. I can run it just fine from testng suite (right click suite > run as > testng suite). If I run the test directly (right click the test > run as > testng test), i get an error:
org.testng.TestNGException: Parameter 'browser' is required by
#Configuration on method MethodSetup but has not been marked #Optional
or defined in
C:\Users\m\AppData\Local\Temp\testng-eclipse--1694993462\testng-customsuite.xml
BaseTest:
public class BaseTest {
protected WebDriver driver;
#Parameters ({"browser"})
#BeforeMethod
protected void MethodSetup(String browser){
System.out.println("method set up");
driver = Browsers.getDriver(browser);
}
//....
}
Test:
public class LoginTest extends BaseTest{
#Parameters({ "browser" , "ch" })
#SuppressWarnings("deprecation")
#Test
public void positiveLoginTest(){
String expectedPageTitle = "Seeker Dashboard - Profile";
String Expectedprofilename = "md";
//...
}
TestNG suite:
<suite name="TestAll">
<test name="ie1">
<parameter name="browser" value="ch"/>
<classes>
<class name="com.dice.LoginTest">
<methods>
<include name="positiveLoginTest"/>
</methods>
</class>
</classes>
</test>
</suite>
You are specifying as a parameter in your suite xml. When you run as suite, that particular xml is used. But when you select a single test and run it, testng creates a custom testng file (with bare minimums - no parameters, no listeners - you can take a look at the file on the path that you mention).
You need to specify your xml as a template xml in eclipse if that is a standard xml that you use. Go to project properties -> testng -> Set the xml as a template xml.

but has not been marked #Optional in testng.xml

I am trying to run the below code using TestNG,
package Framework;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
public class XMLtestng {
WebDriver d;
#Test
#Parameters({"fname"})
public void reg(String fname) throws InterruptedException {
d.findElement(By.linkText("REGISTER")).click();
d.findElement(By.name("firstName")).sendKeys(fname);
d.findElement(By.name("register")).click();
Thread.sleep(3000);
}
#BeforeClass
public void beforeClass() {
d = new FirefoxDriver();
d.manage().window().maximize();
d.get("http://newtours.demoaut.com/");
}
#AfterClass
public void afterClass() {
d.close();
}
}
And this is the corresponding xml file,
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
<test name="reg">
<Parameter name="fname" value="Rachel"/>
<classes>
<class name="Framework.XMLtestng"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
when I run my xml file as TestNg suite,only the website's url is being opened and close. Actions to be performed (typing the firstname for registration), is not being executed. This is the message being printed in the console,
Suite
Total tests run: 1, Failures: 0, Skips: 1
===============================================
Can anybody please help me in resolving this.
Thanks in advance.
Your test is skipped because you didn't provide it the expected parameter and it is not declared as #Optionnal.
The problem is you used <Parameter... instead of <parameter....
Use the DTD to validate your file:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

Not able to run TestNG Suite (Selenium Webdriver)

I am new to TestNg , Selenium.
My problem is I am not able to run my TestNG suite. When I click my testNg suite (By right clicking crossBrowser.xml and click TestNG Suite), It is not running, It does not show any error in the console.
Please find the code I am using.
VerifyTitle.java
package testNGTestPack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class VerifyTitle {
WebDriver driver;
#Test
#Parameters("browserName")
public void verifyTitle(String browserName){
if(browserName.equalsIgnoreCase("firebox")) {
driver=new FirefoxDriver(); }
else if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\driver\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver", "D:\\driver\\IEDriverServer.exe");
driver=new InternetExplorerDriver();
}
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
//driver.quit();
}
}
crossBrowser.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testNGTestPack">
<test name="Test">
<parameter name="browserName" value="firebox"/>
<classes>
<class name="testNGTestPack.VerifyTitle"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Eclipse Version : : Luna Service Release 2 (4.4.2)
When right clicking against crossBrowser.xml which is showing Run As TestNg Suite. But clicking on it, it does not trigger any action. Even in console, it show any error.
Do I need to include any jar file? Please give me a solution
Yes you need to create a new configuration for this.
Select your testng.xml file > Run as > Run Configuration > Click on TestNG on left panel > Right click and and CLick on new > Click on Suite > Browse xml and click on run.
Please try and let me know.
Thanks
Mukesh

TestNG configuration failure

I am trying to run a simple TestNG test from the command line, it errors with a configuration failure. The same TestNG test will run from Eclipse IDE correctly. From command line it does not work. This is a severe limitation of this TestNG framework making it not very useable in a Continuous integration theme. If you plan on working with TestNG ensure you can get it running from the command line before committing to using it as your testing framework. Having to run it from Eclipse IDE is a severe limitation.
TestSuite_Bollosk
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
command line syntax is:
java -cp "C:\correctclpath1\*;C:\correctclpath2\*" org.testng.TestNG "C:\anotherpath\TS_simpletest.xml"
the test looks like this:
package pkgTSBollosk;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TC_Bollosk
{
WebDriver driver;
#Parameters({"param1","param2"})
#Test
public void bollosktestmethod(String param1, String param2) throws InterruptedException
{
assert(true);
System.out.println("test output for bollosk test:");
}
#BeforeTest
public void beforeTest() throws IOException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(2,TimeUnit.SECONDS);
}
#AfterMethod
public void afterTest() {
driver.quit();
driver = null;
}
}
the TS_simpletest.xml file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Bollosk Test Suit" parallel="false">
<test name="Bolluks test name">
<parameter name="Param1" value="not - used"></parameter>
<parameter name="Param2" value="not - used"></parameter>
<classes>
<class name="pkgTSBollosk.TC_Bollosk">
<methods>
<include name = "bollosktestmethod"></include>
</methods>
</class>
</classes>
</test>
</suite>
Substituting Assert.assertTrue for assert and importing org.testNG.assert fixed the problem. A trap for the newbie..... I retract my previous statement about severe limitation.
public class test1 {
public WebDriver d;
#Parameters({"browsername","url"})
#BeforeMethod
public void browserselections(String broname,String URL) {
System.out.println(broname);
System.out.println(URL);
if (broname.equalsIgnoreCase("ff")) {
d = new FirefoxDriver();
} else if (broname.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "F:\\All jars\\chromedriver.exe");
d = new ChromeDriver();
}
d.manage().window().maximize();
d.get(URL);
}
#Test
public void tc1() {
d.findElement(By.id("lst-ib")).sendKeys("testing");
}
}
below is my xml code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" configfailurepolicy="skip">
<test name="Test1">
<parameter name="browsername" value="ff"/>
<parameter name="URL" value="https://www.google.com"/>
<classes>
<class name="testng.test1" />
</classes>
</test>
<test name="Test2">
<parameter name="browsername" value="chrome"></parameter>
<classes>
<class name="testng.test1" />
</classes>
</test>
</suite> <!-- Suite -->enter code here
I was facing the same issue and i noticed that all of my jar files were in different sub folders. Move all of them in one library folder and execute with same command.
java -cp "%projectLocation%/Library/*";"%projectLocation%/target/classes/" org.testng.TestNG testng.xml
Try TestNG config policy. Add parameter configfailurepolicy="continue" if you wish to proceed with config failures else, configfailurepolicy="skip". Example
<suite name="Bollosk Test Suit" configfailurepolicy="continue">
If you remove driver.quit() from public void afterTest() method,
the Configuration Failures will not come.