I have 2 groups of test cases as mentioend below.
#Test(groups="one", dataProvider = "TestData")
public void firstTest(String data){
//Code
}
#Test(groups="one", dataProvider = "TestData")
public void secondTest(String data){
//Code
}
#Test(groups="two", dataProvider = "TestData")
public void thirdTest(String data){
//Code
}
Below is the XML file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Test-Automation" parallel="methods" thread-count="2" verbose="1">
<test name="Suite Test" parallel="methods" thread-count="2" verbose="1">
<listeners>
<listener class-name="GroupByInstanceEnabler"></listener>
</listeners>
<classes>
<class name="SampleTest">
<methods>
<include name="firstTest"/>
<include name="secondTest"/>
<include name="thirdTest"/>
</methods>
</class>
</classes>
</test>
</suite>
Below is the pom.xml build details.
<build>
<finalName>Automation</finalName>
<filters>
<filter>profiles/${build.profile.id}/config.properties</filter>
</filters>
<resources>
<resource>
<filtering>true</filtering>
<directory>${basedir}/src/test/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>${project.basedir}/testng.xml</suiteXmlFile>
</suiteXmlFiles>
<parallel>method</parallel>
<threadCount>2</threadCount>
</configuration>
</plugin>
</plugins>
</build>
My Question:
Using Maven, how do I run the group "one" and group "two" separately.
I tried "mvn test -Dgroups=two" but it only runs as normal(all tests).
Note: I use 2 different profiles to run the group "one" twice with different values. This is the reason you see profile configuration in the pom.xml file.
You can make use of a beanshell expression for getting this done.
You first add a beanshell expression as below to your suite xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<test name="Test">
<method-selectors>
<method-selector>
<script language="beanshell">
<![CDATA[whatGroup = System.getProperty("groupToRun");
groups.containsKey(whatGroup);
]]>
</script>
</method-selector>
</method-selectors>
<classes>
<class name="organized.chaos.GroupsPlayGround" />
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
This way you can make use of the suite xml from your IDE and still choose which groups to be executed. You can enrich this beanshell to run everything by default if no value is provided via the JVM argument -DgroupToRun=one
For more information on beanshell execution refer:
Official TestNG documentation here
My blog post link
Related
Good morning all.
Quick question: when I do a "maven update project" my application.xml file is updated.
I have abc-ear/META-INF/application.xml like this :
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" version="5">
<description>Application abc construite avec le framework Sphinx.</description>
<display-name>def</display-name>
<module id="WebModule_1301067741013">
<web>
<web-uri>def-web.war</web-uri>
<context-root>def</context-root>
</web>
</module>
<module id="WebModule_1306335826653">
<web>
<web-uri>ghi-web.war</web-uri>
<context-root>jkl_bo</context-root>
</web>
</module>
<module id="WebModule_1330011848203">
<web>
<web-uri>mno-web.war</web-uri>
<context-root>mno_bp</context-root>
</web>
</module>
</application>
And my abc-ear/src/dev/application/META-INF/application.xml contains this after "maven update projects" :
<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6">
<display-name>abc-ear</display-name>
</application>
I'm using maven-ear-plugin :
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<configuration>
<!-- <fileNameMapping>full</fileNameMapping> -->
<!-- <defaultLibBundleDir>.</defaultLibBundleDir> -->
<skinnyWars>true</skinnyWars>
<applicationXml>${basedir}/META-INF/application.xml</applicationXml>
<generateApplicationXml>false</generateApplicationXml>
<archive>
<manifestEntries>
<Date>${maven.build.timestamp}</Date>
<Version>${project.version}</Version>
<!-- <Baseline>${baseline.number}</Baseline> -->
</manifestEntries>
</archive>
<!-- On doit faire en sorte que le numero de version du war soit retiré
sinon, chaque sous version va demander de reconfigurer le serveur -->
<modules>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>abc-web</artifactId>
<contextRoot>/abc</contextRoot> <!-- Voir application XML -->
<bundleFileName>abc-web.war</bundleFileName>
</webModule>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>def-web</artifactId>
<contextRoot>/ghi_bo</contextRoot> <!-- Voir application XML -->
<bundleFileName>def-web.war</bundleFileName>
</webModule>
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>ghi-web</artifactId>
<contextRoot>/ghi_bp</contextRoot> <!-- Voir application XML -->
<bundleFileName>ghi-web.war</bundleFileName>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</build>
Before mvn clean install, I need to
git checkout application.xml
If i don't do it, it doesn't work.
Can you tell me why ?
maven version : apache-maven-3.0.4
Thank you
I would like to run my selenium test suite which contains 2 tests(test1 and test2) on 2 different browsers.
I want to run the 2 tests in order so i used dependsOnMethods to specify the order
I created a data provider containing the browsers as parameters (i used 2 browser names as strings) but i could not pass it to #BeforeSuite
when i passed the data provider to test1 ,test1 ran 2 times then test2 ran 1 time.
This is not what i need , i need test1 and test2 to run in order on each browser.
Any suggestion how i can achieve this?
Thanks In advance
#Test( groups = { "login-positive"},dataProvider="browserList")
public void loginTest(String browser)throws MalformedURLException{
PageUtils.initializeBrowser(browser);
loginPage= PageUtils.getHomePage().goToLoginPage(PageUtils.getWebDriver());
String username="raghda";
String password="I123123";
loginPage.fillUserName(username);
loginPage.fillPassword(password);
loggedInPage=loginPage.clickSubmit();
Assert.assertEquals("raghda", loggedInPage.getDisplayedUserName());
}
#Test( groups = { "login- positive"},dataProvider="browserList",dependsOnMethods= {"loginTest"})
public void ValidateGeneralDonation(String browser)throws MalformedURLException{
generalDonPage=loggedInPage.clickGeneralDonation();
generalDonPage.selectCharity(1);
generalDonPage.checkDonationItem(0);
assertTrue(generalDonPage.getDonateButton());
}
POM.xml
<profile>
<id>RegressionTest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>testngregression.xml</suiteXmlFile>
</suiteXmlFiles>
<properties>
<property>
<name>parallel</name>
<value>methods</value>
</property>
<property>
<name>threadCount</name>
<value>1</value>
</property>
</properties>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
testngregression.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1" verbose="1">
<test name="regression">
<parameter name="loggedIn" value="false"></parameter>
<groups>
<define name="login-all">
<include name="login-positive" />
<include name="login-negative" />
</define>
<define name="login-positive-only">
<include name="login-positive" />
</define>
<run>
<include name="login-positive-only" />
</run>
</groups>
<classes>
<class name="basicportal.automation.TestDriver" />
</classes>
</test>
</suite>
I'm exporting my mapping (hbm.xml) to pojo classes using ant task.
It generates the POJO files in the mapped directory. But the Classes miss the Package statement. It simply creates all files in default package
// default package
// Generated Aug 23, 2012 12:34:40 PM by Hibernate Tools 3.2.2.GA
Here is my ant build file for the task.
<project name="Hibernate Tools for Ant - hbm2java" default="gensrc">
<path id="tools">
<path location="lib/hibernate-tools-3.2.3.GA.jar"/>
<path location="lib/hibernate3.6.10.jar"/>
<path location="lib/freemarker-2.3.8.jar"/>
<path location="lib/hsqldb-2.2.4.jar"/>
<path location="lib/commons-logging.jar"/>
<path location="lib/dom4j-1.6.1.jar"/>
<path location="lib/slf4j-api-1.6.1.jar"/>
<path location="lib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
</path>
<taskdef name="gen-src" classname="org.hibernate.tool.ant.HibernateToolTask"
classpathref="tools"/>
<target name="gensrc">
<gen-src destdir="src/main/java">
<configuration
configurationfile="src/main/resources/hibernate.cfg.xml">
<fileset dir="src/main/java/com/kee/example/domain/maps">
<include name="Event.hbm.xml"/>
</fileset>
</configuration>
<hbm2java destdir="src/main/java/com/kee/example/domain"/>
</gen-src>
</target>
</project>
the default Pojo.ftl (inside hibernate-tools.jar) has the declaration as below
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}
what should i be changing in order to have correct package declaration in the generated POJO.
Update:
Here is my Mapping file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.kee.example.domain">
<meta attribute="generated-class">EventBase</meta>
<meta attribute="implement-equals">true</meta>
<meta attribute="scope-field">protected</meta>
<class name="com.kee.example.domain.Event" table="event">
<id name="id" type="java.lang.Long">
<generator class="native"/>
</id>
<property name="eventDate" type="timestamp"/>
<property name="eventString" type="java.lang.String"/>
</class>
</hibernate-mapping>
I was getting the same problem in maven and not sure about the reason but it was resolved once I started using annotationconfiguration instead of jdbcconfiguration.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2ddl</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>${db.src.dir}</outputDirectory>
</component>
<component>
<name>hbm2java</name>
<implementation>annotationconfiguration</implementation>
<outputDirectory>src/main/java</outputDirectory>
</component>
</components>
<componentProperties>
<drop>true</drop>
<create>true</create>
<export>false</export>
<format>true</format>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
<outputfilename>${ddl.file}</outputfilename>
<templatepath>src/main/resources/hibernate-template</templatepath>
<delimiter>;</delimiter>
<configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>${jdbc.groupId}</groupId>
<artifactId>${jdbc.artifactId}</artifactId>
<version>${jdbc.version}</version>
</dependency>
</dependencies>
</plugin>
I did not want the annotated pojo classes so I had commented codes in *.ftl files.
I have a question in regards to consuming a web service based on a third party wsdl file.
I've taken the given wsdl and generated the 120+ java files required. This process was done by using xjc. Within the Sping environment, I was able to successfully create a couple of JUnit tests by calling a couple of the exposed services.
But, in order to successfully test those services I had to add the #XmlRootElement annotation to the generated java files. Otherwise, I would encounter an error stating
"com.sun.istack.SAXException2: unable to marshal type
"com.beam.services.client.UserGetRequestData" as an element because it
is missing an #XmlRootElement annotation"
.
I've exhausted my search… I have no control as to how the wsdl file is created/structured. How can I go about generating the java files to ensure that the #XmlRootElement annotation is included or go about writing the client side code in way to avoid the error above?
Thank you.
If you really need the #XmlRootElement you could use simple binding mode if your type is used for only one element. The reason why JAXB does not include the annotation by default and how to use simple binding is explained here: https://community.oracle.com/blogs/kohsuke/2006/03/03/why-does-jaxb-put-xmlrootelement-sometimes-not-always:
your schema might be used by other schemas that XJC isn't compiling
right now
and
Such notion isn't defined in the spec, but as an experiment we have
such aggressive optimization mode in XJC, tentatively called
"simple-minded binding mode".
The sample seems to got lost when the moved the blog, but it looked like this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="1.0" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jaxb:extensionbindingprefixes="xjc">
<xs:annotation>
<xs:appinfo>
<jaxb:globalbindings>
<xjc:simple/>
</jaxb:globalbindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="foo" type="bar"/>
<xs:complextype name="bar"/>
</xs:schema>
The other possibilty is to wrap it in a JAXBElement. The ObjectFactory should include a method for creating these wrapped objects.
If you are using mavem then check this link it worked for me.
Create Maven project. Below you can see the POM:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.zmeu</groupId>
<artifactId>zmeu-blog-maven-jaxb</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ZMEU Blog Maven JAXB</name>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.0</version>
<configuration>
<schemaDirectory>src/main/resources/schema</schemaDirectory>
<bindingDirectory>src/main/resources/schema</bindingDirectory>
<generatePackage>org.zmeu.blog.jaxb</generatePackage>
<strict>false</strict>
<extension>true</extension>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.6.2</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-annotate</artifactId>
<version>0.6.2</version>
</plugin>
</plugins>
<args>
<arg>-Xannotate</arg>
<arg>-XtoString</arg>
</args>
</configuration>
<executions>
<execution>
<id>generate</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics-runtime</artifactId>
<version>0.6.2</version>
</dependency>
</dependencies>
</project>
Write XML Schema (schema.xsd):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="user" type="user" />
<xs:element name="userList" type="userList" />
<xs:complexType name="user">
<xs:all>
<xs:element name="id" type="xs:long" minOccurs="0" />
<xs:element name="name" type="xs:string" />
<xs:element name="registrationDate" type="xs:dateTime" />
</xs:all>
</xs:complexType>
<xs:complexType name="userList">
<xs:sequence>
<xs:element name="user" type="user" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Customize JAXB Bindings (binding.xjb):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
version="2.1">
<jaxb:globalBindings>
<!-- Use java.util.Calendar instead of javax.xml.datatype.XMLGregorianCalendar for xs:dateTime -->
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
<!-- Force all classes implements Serializable -->
<xjc:serializable uid="1" />
</jaxb:globalBindings>
<!-- Annotate the following classes with XmlRootElement -->
<jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[#name='user']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="user" />
</annox:annotate>
</jaxb:bindings>
<jaxb:bindings node="xs:complexType[#name='userList']">
<annox:annotate>
<annox:annotate annox:class="javax.xml.bind.annotation.XmlRootElement" name="userList" />
</annox:annotate>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Run the build using mvn clean install command. Build must be successful. Generated classes will be located in target/generated-sources/xjc directory. Below is a snippet from generated User class:
#XmlAccessorType(XmlAccessType.FIELD)
#XmlType(name = "user", propOrder = {})
#XmlRootElement(name = "user")
public class User implements Serializable, ToString {
private final static long serialVersionUID = 1L;
protected Long id;
#XmlElement(required = true)
protected String name;
#XmlElement(required = true, type = String.class)
#XmlJavaTypeAdapter(Adapter1 .class)
#XmlSchemaType(name = "dateTime")
protected Calendar registrationDate;
}
You are done!
I have some slow tests that rely on the database that I don't want run every time I build my project with Maven. I've added the excludedGroups element to my pom file as explained http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#excludedGroups but I can't seem to get it working.
I've created a minimal project. Here is the pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>exclude</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<excludedGroups>db</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>5.14</version>
</dependency>
</dependencies>
</project>
And these are the two test classes:
public class NormalTest {
#Test
public void fastTest() {
Assert.assertTrue(true);
}
}
and
public class DatabaseTest {
#Test(groups={"db"})
public void slowTest() {
Assert.assertTrue(false);
}
}
However both tests still run. I can't figure out what I'm doing wrong.
I ended up creating external test suits:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
<test name="standard">
<groups>
<run>
<exclude name="slow" />
<exclude name="external" />
<exclude name="db" />
</run>
</groups>
<packages>
<package name="com.test.*" />
</packages>
</test>
</suite>
and
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
<test name="full">
<packages>
<package name="com.test.*" />
</packages>
</test>
</suite>
and specifyied which to run in a profile:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.11</version>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/suites/standard.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
...
<profile>
<id>fulltest</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/suites/full.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</profile>
From my experience, the excluded groups feature works only when you have a set of included groups. So in order to do what you want, you need to add all the tests to at least one group (you can do this "easily" by annotating the class rather than methods).
For example (just changing the NormalTest)
#Test( groups = "fast")
public class NormalTest {
#Test
public void slowTest() {
Assert.assertTrue(true);
}
}
and in your configuration
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4.2</version>
<configuration>
<groups>fast</groups>
<excludedGroups>db</excludedGroups>
</configuration>
</plugin>
I know that this is not obvious, but it's the way that testng works :S. As a side note, I've always used an external configuration file for testng rather that the embedded configuration in the pom, so the parameter groups might not be correct.