Why is my TestNG xml file not targeting my Java Class? - java

Why is my TestNG xml file not targeting my Runner Class?
My XML File:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Cucumber Framework" verbose="2">
<test name="Cucumber Tests">
<classes>
<class name="runners.MainRunner"></class>
</classes>
</test>
</suite>
location of the runner class:
CucumberFramework\src\test\java\CucumberFramework\runners
CucumberFramework\src\test\java\CucumberFramework\runners\MainRunner.java
Please note: if i execute the runner class directly, everything works accordingly, it seems the xml file is unable to locate the runner class, any ideas?
Runner class code:
package CucumberFramework.runners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions (
features = {"src/test/java/CucumberFramework/featureFiles/"},
glue = {"CucumberFramework.steps"},
monochrome = true,
tags = {},
plugin = {"pretty", "html:target/cucumber","json:target/cucumber.json", "com.cucumber.listener.ExtentCucumberFormatter:target/report.html"}
)
public class MainRunner {
}

You can execute the junit classes with testng xml file by define the property junit="true" as given below
<suite name="Cucumber Framework" >
<test name="Cucumber Tests" junit="true">
<classes>
<class name="CucumberFramework.runners.MainRunner" />
</classes>
</test>
</suite>

Related

How to run a single test from TestNG.XML file in command line from Gradle Kotlin

I have testNG file which has multiple methods.
In this testNG file I added the parameters to pass the parameter on code from xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="LocalIosTestSuite" parallel="tests" thread-count="1" >
<test name="iphonetest">
<parameter name="platformName" value="iphonetest" />
<classes>
<class name="tests.TestKotlin">
</class>
</classes>
</test>
<test name="androidtest">
<parameter name="platformName" value="androidtest" />
<classes>
<class name="tests.TestKotlin">
</class>
</classes>
</test>
</suite>
Kotlin Class
import org.testng.annotations.Parameters
import org.testng.annotations.Test
class TestKotlin {
#Parameters("platformName")
#Test()
fun Testmethod(platformName:String)
{
println("TestKotlin")
println(platformName)
}
}
Command to run a single test from testng file
./gradlew test -Psuite5 -testnames iphonetest
This is not working for me please help me any solution to run a specific test on gradle

Batch files launches and disappear without executing Selenium tests

I have created a Selenium project named 'LearnAutomation' in Java and convert it to TestNG tests which created a testng.xml for me. Now i want to run it via batch file. So i created one as below -
cd E:\Workspace - Eclipse\LearnAutomation
set ProjectPath=E:\Workspace - Eclipse\LearnAutomation
echo %ProjectPath%
set classpath=%ProjectPath%\bin;%ProjectPath%\lib\*
echo %classpath%
java org.testng.TestNG %ProjectPath%\testng.xml
and save it as testng.bat file. On double clicking batch file, it gets launched and disappeared. (failed to run test)
My testng.xml looks like this -
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test">
<classes>
<class name="tests.Demo"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
My Demo class looks like this -
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Demo {
#Test
public void batchFile() {
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\srvashishtha\\Downloads\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}

Show all failed tests of multiple test suite in one testNG report

Actually I'm testing multiple website for my company. All the website have the same structure and i'm testing various things on each.
For my test i have a Selenium Java Maven project using TestNG. For each web site, i've created a sub folder and generate a specific testing.xml file to specify each test i'm doing for the website.
Here the structure : http://imgur.com/g0VHfMw
My testing.xml files are like this :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test suite website 1" parallel="tests" thread-count="2">
<test name="Test1">
<classes>
<class name="package.for.mytest1"></class>
</classes>
</test>
<test name="Test2">
<classes>
<class name="package.for.mytest2"></class>
</classes>
</test>
<test name="Test3">
<classes>
<class name="package.for.mytest3"></class>
</classes>
</test>
</suite>
How can I merge this 4 test suite in one testNG report ? And if it's possible, is there a way to only show the unstable or failed test on it ?
Any ideas ? Thanks for your help
Try Allure it could easily generate combined report for several suites and show only failed/broken tests by default.
Either you can Use Suite-XML tag in TestNG.xml file to report all your test in single report or implement a reporter of your own to collect all the results and then at the end flush everything into report of your own format with whatever information you need.
You can create a separate testng.xml file in which you can give the path of other testsuites files.
eg:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name= "Allsuites" verbose="1" preserve-order="true" >
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter"/>
<listener class-name="org.uncommons.reportng.JUnitXMLReporter"/>
</listeners>
<suite-files preserve-order="true">
<suite-file path="testng1.xml" />
<suite-file path="testng2.xml" />
<suite-file path="testng3.xml" />
</suite-files>
</suite>

Testing browsers simultaneously; receiving error

Right now, I'm trying to work on my first test that will test browsers Chrome, Firefox, IE, and Safari in parallel. But the following error that I'm getting is this:
FAILED CONFIGURATION: #BeforeMethod beforeMethod
org.testng.TestNGException:
Parameter 'browser' is required by #Configuration on method beforeMethod but >has not been marked #Optional or defined
I'm using Selenium, TestNG and Maven with the JAVA language. The XML test suite file and the java file are in the same folder in the directory. What I was able to find for the test suite XML file online is as follows (with class name values set to the correct package and class name):
<?xml version="1.0" encoding="UTF-8"?>
<DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="2" parallel="tests">
<test name="ChromeTest">
<parameter name="browser" value="chrome" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="FirefoxTest">
<parameter name="browser" value="firefox" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="IETest">
<parameter name="browser" value="ie" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
<test name="SafariTest">
<parameter name="browser" value="safari" />
<classes>
<class name="com.sqa.ts.multiBrowser.BrowserTest">
</class>
</classes>
</test>
</suite>
Below is my code to just open the browser to make sure that it will run and pass:
package com.sqa.ts.multiBrowser;
import java.net.MalformedURLException;
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.openqa.selenium.safari.SafariDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserTest {
private WebDriver driver;
#Test
public void testCaseOne() {
driver.get("http://www.google.com");
driver.close();
}
#BeforeMethod
#Parameters("browser")
public void beforeMethod(String browser) throws MalformedURLException {
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/chromedriver.exe");
driver = new ChromeDriver();
} else if (browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
} else if (browser.equalsIgnoreCase("ie")) {
System.setProperty("webdriver.ie.driver", "C:/Users/Trevor/workspace/BrowserTest/drivers/IEDriverServer.exe");
driver = new InternetExplorerDriver();
} else if (browser.equalsIgnoreCase("safari")) {
driver = new SafariDriver();
}
}
#AfterMethod
public void afterMethod() {
driver.quit();
}
}
If anyone can give me an insight as to what is causing this issue, I would greatly appreciate it. Thank you.
Looks like you are missing the testNG configuration in the pom.xml
<build>
<plugins>
<!-- Following plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>src/test/java/com/sqa/ts/multiBrowser/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</build>
Running mvn clean install or mvn clean install should run the test cases. Hope this helps you.
It looks like executing the XML file as Run as > Testng Suite. That was the fix to my problem.
It worked with option = Run as > Testng Suite

How to use a test class from a jar in TestNG

I have a JAR(xyz.jar) with test class test.sample.ClassX. I am trying to use this class in testng.xml shown below,
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="SuiteX" verbose="1" >
<test name="TestX" >
<classes>
<class name="test.sample.ClassX" />
</classes>
</test>
</suite>
My pom.xml has dependency(xyz.jar) added. I can execute this suite from eclipse but fails when i try to execute from command-line(mac) with below error,
error: package test.sample does not exist
Any idea what the issue might be ?
My first guess is that you might not have a class defined like:
package test.sample;
public class TestX
{
...

Categories