TestNG configuration failure - selenium

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.

Related

Running feature cucumber in parallel

I want to run a cucumber feature in different browsers;
So, now I'm able to open the 3 browsers in parallel chrome, ff and ie but they can't continue the other steps in features !
My method is :
#Parameters("myBrowser")
#BeforeClass
#Given("^openaaaBrowser<myBrowser>$")
public void openaaaBrowser(#Optional("optional value") String myBrowser) throws InterruptedException {
WebDriver driver;
if (myBrowser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver","C:\\Driver\\IEDriverServer\\IEDriverServer_32bits.exe");
driver = new InternetExplorerDriver();
}
if (myBrowser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver","D:\\Drive\\chromedriver_win32\\chromedriver.exe");
driver= new ChromeDriver();
}
if (myBrowser.equalsIgnoreCase("firefox")){
System.setProperty("webdriver.gecko.driver","D:\\Drive\\geckodriver-v0.20.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
}}
My testng.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SuiteSopraHR" parallel="tests">
<test name="testff">
<parameter name="myBrowser" value="firefox" />
<classes>
<class name="com.soprahr.foryou.automation.steps.StepDefinitionConnect"/>
</classes>
</test> <!-- Test -->
<test name="testie">
<parameter name="myBrowser" value="ie" />
<classes>
<class name="com.soprahr.foryou.automation.steps.StepDefinitionConnect"/>
</classes>
</test> <!-- Test -->
<test name="testchrome">
<parameter name="myBrowser" value="chrome" />
<classes>
<class name="com.soprahr.foryou.automation.steps.StepDefinitionConnect"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
and I have those methods
#Test(priority=1)
#When("^Open browser$")
public void openBrowser() throws InterruptedException {
StepDefinition.DRIVER.get(URL);
Thread.sleep(N_3000);
StepDefinition.waitForJQueryProcessing(StepDefinition.DRIVER, N_30);
}
#Test(priority=2)
#Then("^Se connecter à l'environnement via ID '(.*)'$")
public void letThisOneConnect(final String Id) throws Throwable {
Thread.sleep(N_3000);
Utilities utilities = new Utilities();
TestCase testCase = utilities.getMyTestCase(Id);
StepDefinition.deleteAndEnterTextById(ID_LOGIN_INPUT_4YOU, testCase.getLogInId());
StepDefinition.deleteAndEnterTextById(ID_PASSWORD_INPUT_4YOU, testCase.getLogInPassword());
StepDefinition.clickButtonById(ID_LOGIN_BUTTON_4OU);
}
The problem here and I don't understand why it can't the #test methods
If you want to run a scenario with different browsers you have to run the scenario multiple times. i.e. if you have 3 browsers then you end up with 3 scenario instances.
You can't do is run one scenario in 3 browsers.
The simplest way to get your parallelism do this to take it out of Cucumber. If you ran in series you might have
cucumber features/my_feature BROWSER=chrome
cucumber features/my_feature BROWSER=firefox
cucumber features/my_feature BROWSER=ie
Now you could use your CI platform to run each of these commands in a separate instance. Then you'll get your parallelism, and all you have to do with Cucumber is get it to use an environment variable to control which driver and browser to use.
You won't succeed in getting Cucumber to work with more than one browser for a particular scenario instance.

How to use testNG #Parameters in #BeforeSuite to read value from testng.xml

I am using testNG with Selenium webdriver 3.4. I want to perform the test on for different browser at the same time.
In my testNG.xml I have
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Demo">
<test name="Demo on Chrome">
<parameter name="browser" value="Chrome" />
<classes>
<class name="catalogueMemberPortal.CatalogueTest"/>
</classes>
</test>
<test name="Demo on Firefox">
<parameter name="browser" value="Firefox" />
<classes>
<class name="catalogueMemberPortal.CatalogueTest"/>
</classes>
</test>
</suite>
My Code
public class BaseClass {
public static WebDriver driver;
public WebDriver getDriver() {
return driver;
}
#BeforeSuite
#Parameters({"browser"})
public void launchBrowser(String browser) throws Exception {
if (browser.equalsIgnoreCase("Firefox")){
String ffDriverPath = ((System.getProperty("user.dir")+"/browser_drivers/firefox/geckodriver.exe")) ;
System.setProperty("webdriver.gecko.driver",ffDriverPath);
driver = new FirefoxDriver();
logger.info("Firefox Initialized");
}
// same goes for other type of browsers
#AfterSuite
public void afterSuite() {
driver.close();
driver.quit();
}
}
Error I am getting
org.testng.TestNGException:
Parameter 'browser' is required by BeforeSuite on method launchBrowser but has not been marked #Optional or defined
in C:\testng.xml
Your browser parameter is located into the <test> node, not in the <suite> node.
At the suite level, the parameter doesn't exist.
Just replace #BeforeSuite and #AfterSuite by #BeforeTest and #AfterTest.

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">

How can I print in log the number of tests running as part of one group in TestNG?

I need to print this before actual execution of tests start. Anyways, we get the count at last of execution.
Say for example if my testng.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
<test name="test1">
<groups>
<run>
<include name="functest" />
</run>
</groups>
<classes>
<class name="GroupTestExample" />
</classes>
</test>
</suite>
And there are 10 methods in class GroupTestExample which comes under group functest, it should print 10 in log as soon as it starts running.
Create a custom Listener which extends the org.testng.TestListenerAdapter. After that override the following method:
public class MyCustomListener extends TestListenerAdapter {
#Override
public void onStart(ITestContext testContext) {
super.onStart(testContext);
System.out.println("Number of Test Methods: " + testContext.getAllTestMethods().length);
}
}
You can add your custom listener by using #Listeners annotation on your main class of your test, like
#Listeners({MyCustomListener.class})
public class MyMainTestClass { ... }
You can find more information in TestNG doc:
TestNG Listeners -
http://testng.org/doc/documentation-main.html#testng-listeners

will testng initiate as many webdriver instance as test cases?

I am facing a problem of Selenium Grid and TestNG. My test scripts will NOT quite the browser until all the test class are executed.
I've configed 5 Chrome instance running on my Selenium Grid, then below is my xml file
<suite name="BrowserTest">
<test name="Test1" >
<parameter name="browserName" value="chrome"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
<test name="Test2" >
<parameter name="browserName" value="firefox"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
<test name="Overview Page Test on ie, English" >
<parameter name="browserName" value="ie"/>
<classes>
<class name= "Pagetest" />
</classes>
</test>
</suite>
Here is my test scripts
public class Pagetest {
private WebDriver browser;
public PateTest( String browser){
// create a browser instance.
}
#AfterClass (or #afterTest)
public void afterTest() throws Exception {
System.out.println("this test is done");
this.browser.quit();
}
#Test
public void test1(){
this.browser.get("www.google.com");
// doing some login stuff
}
}
Now when I am running my test, testNG will create 3 chrome webdirver instance one by one (with running the test). but after the 1st test is done, the browser instance of 1st test is not quited until all 3 tests are done, then all the 3 chrome instance are quited almost at the same time. What I want is after the 1st test is done, it's browser instance is quited, then the 2nd test case is started, did I do something wrong or how should I change my code?
Try using threadlocal it would help when you are even running tests in parallel
public ThreadLocal<WebDriver> driver=null;
#beformethod()
public void setUp
{
driver = new ThreadLocal<WebDriver>();
driver.set(new ChromeDriver());
return driver.get();
}
#AfterMethod
public void closeBrowser() {
{
driver.get().quit();
}