Confusion With Creating TestNG.XML

71 views
Skip to first unread message

Kumar

unread,
Sep 3, 2012, 7:26:22 AM9/3/12
to testng...@googlegroups.com
Hi,

I have written my code in Java Language for selenium webdriver tests.

In my code, I have one public class and multiple methods. Each method is dependent on previous method, so I have dependency as well.
My each method is a test case as well.

However, I want to create an TestNG.XML file and document it so that I can run each test case (method) separately and get results accordingly.

I reviewed the documentation, but still confused.

Please help,
Kumar

Krishnan Mahadevan

unread,
Sep 3, 2012, 7:38:59 AM9/3/12
to testng...@googlegroups.com
AFAIK I don't think you can execute @Test annotated methods independently when they are depending upon some other @Test annotated method.

Have you tried using preserve-order tag and using it in conjunction with <include> tag? Would that help ?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
<test name="Test" preserve-order="true">
<classes>
<class name="testng.samples.DemoTest">
<methods>
<include name="f"></include>
<include name="foo"></include>
<include name="bar"></include>
</methods>
</class>

</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->



Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/wjV8pzq9aa0J.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.

Alok Agarwal

unread,
Sep 3, 2012, 7:43:36 AM9/3/12
to testng...@googlegroups.com
Thanks Krishnan, but my confusion is not with dependent methods.
I am able to run my tests without any problem when I have one public class and many methods in it.

But, I want to invoke the run in an .xml file and thus confused on how to start?
Can I create a new class for each method, remove dependency and then call them from through .xml file?

(If you can share a long test suite with multiple classes and methods might help me.)

Thanks,
Kumar.

Krishnan Mahadevan

unread,
Sep 3, 2012, 7:45:02 AM9/3/12
to testng...@googlegroups.com
The approach is still the same. 

So if you have more than one test classes with each test class having 1 @Test annotated method, then you would basically list out each of them using the 

<class name=""> tag and then work with it.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



Kumar

unread,
Sep 3, 2012, 7:50:04 AM9/3/12
to testng...@googlegroups.com
Ok Krishnan, let me try that and get back to you if needed.

I just tried add public class and move method inside it (with @Test annotation) for a few methods and try to run it, but its doing nothing.
What could be the cause?

Thanks,
Kumar.

Krishnan Mahadevan

unread,
Sep 3, 2012, 8:05:12 AM9/3/12
to testng...@googlegroups.com
You would need to show some code as well as your TestNG suite file for me or anyone else to comment further.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/ffeI8RKm4qUJ.

Kumar

unread,
Sep 3, 2012, 8:27:24 AM9/3/12
to testng...@googlegroups.com
Here is the java code that I have (Per your suggestion, I have removed method dependency as we can maintain the order in .XML file):

public class SmallTests {

public WebDriver loginpage = new FirefoxDriver();
public WebDriverWait waitforobjecttoload = new WebDriverWait(loginpage, 100);

public class Login
{
@Test
public Login() throws InterruptedException
{
*****
}

public class CreateSurvey
{
@Test

public CreateSurvey() throws InterruptedException
{
*****
}

public class ChangesInProperties
{
@Test
public ChangesInProperties() throws InterruptedException
{
*****
}

public class TextBlock
{
@Test
public TextBlock() throws InterruptedException
{
*****
}
}

Thanks,
Kumar.

Krishnan Mahadevan

unread,
Sep 3, 2012, 8:28:06 AM9/3/12
to testng...@googlegroups.com
So what is the issue here ?

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/-outl5Jscd4J.

Kumar

unread,
Sep 3, 2012, 8:31:03 AM9/3/12
to testng...@googlegroups.com
If you feel that is correct and fine, then its a good news for me.

I tried to run the same code using .XML file, but it is giving following error:
"Cannot find class in classpath: Login.SmallTests.SM_SmokeTest".

Here is my .XML code for two classes:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Survey Manager Smoke Test" >

<test name="Login To the Account">

<classes>

<class name="Login.SmallTests.SM_SmokeTest"/>

</classes>

</test>

<test name="Create Survey">

<classes>

<class name="CreateSurvey.SmallTests.SM_SmokeTest"/>

</classes>

</test>

</suite> 


Thanks,
Kumar.

Krishnan Mahadevan

unread,
Sep 3, 2012, 8:33:03 AM9/3/12
to testng...@googlegroups.com
You would need to give the fully qualified package path of "SmallTests" in your suite file.
The easiest way of doing this is to right click your class, and choose "Copy qualified name" if you were using eclipse.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/9ZcMzEPaoREJ.

Kumar

unread,
Sep 3, 2012, 8:38:21 AM9/3/12
to testng...@googlegroups.com
Still same issue; here is the new xml code:
(First class name is "Login" and next class name is "CreateSurvey")

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Survey Manager Smoke Test" >
<test name="Login To the Account">
<classes>
<class name="Login/SurveyManager/src/SM_SmokeTest/SmallTests.java"/>
</classes>
</test>
<test name="Create Survey">
<classes>
<class name="CreateSurvey/SurveyManager/src/SM_SmokeTest/SmallTests.java"/>
</classes>
</test>
</suite> 

Thanks,
Kumar.

Krishnan Mahadevan

unread,
Sep 3, 2012, 8:40:55 AM9/3/12
to testng...@googlegroups.com
Please change :

<class name="Login/SurveyManager/src/SM_SmokeTest/SmallTests.java"/>

to

the package name that you declared in your .java class.

So suppose your SmallTests.java looks like below :

package com.tests.myTests;

public class SmallTests{

}

then you would refer to it as <class name="com.tests.myTests.SmallTests"/>


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/qs7ysQUIvhIJ.

Kumar

unread,
Sep 3, 2012, 9:12:39 AM9/3/12
to testng...@googlegroups.com
Thanks Krishnan, it seems to have worked if I use the outermost public class in the path. <class name="SM_SmokeTest.SmallTests"/>.

But as shown earlier my program pattern, I have inner classes (one each for a method/test case). How do I run for inner classes?
I tried with <class name="SM_SmokeTest.SmallTests.Login"/>, but it didnt work.

I want to run each inner class separately and get its result appropriately in TestNG.

See below:

public class SmallTests {

public WebDriver loginpage = new FirefoxDriver();
public WebDriverWait waitforobjecttoload = new WebDriverWait(loginpage, 100);

public class Login
{
@Test
public Login() throws InterruptedException
{
*****
}

public class CreateSurvey
{
@Test

public CreateSurvey() throws InterruptedException
{
*****
}

public class ChangesInProperties
{
@Test
public ChangesInProperties() throws InterruptedException
{
*****
}

public class TextBlock
{
@Test
public TextBlock() throws InterruptedException
{
*****
}
}

Thanks,
Kumar

Abraham Lin

unread,
Sep 3, 2012, 9:31:17 AM9/3/12
to testng...@googlegroups.com
If I remember correctly, TestNG (and pretty much any other XML-based configuration engine) expects a binary name, so you'd specify the inner class as "SM_SmokeTest.SmallTests$Login".

-Abraham


To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/P9su5MaJ0TMJ.

Kumar

unread,
Sep 3, 2012, 10:06:48 AM9/3/12
to testng...@googlegroups.com
I created a different classes instead of having inner classes.
Now, in the test suite, when I try to run the classes in a sequential manner in same window, it opens a new window after successfully running first class.
How can I make sure that it doesn't happen.

Basically, it need to login to the account (first class) and perform more tasks (further classes) in the same window instead of opening a new window for each class.

Thanks,
Kumar.

Alok Agarwal

unread,
Sep 3, 2012, 1:11:18 PM9/3/12
to testng...@googlegroups.com
Any help on this please?

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/testng-users/-/ytN1wa41hJYJ.

Yevhen Bilevych

unread,
Sep 3, 2012, 4:12:27 PM9/3/12
to testng...@googlegroups.com
Kumar,

Strictly saying, your question is not related to TestNG. It's stuff of Selenium, which is third-party. I'd recommend you to read documentation on Selenium Webdriver. There, you could find method WebDriver.quit(), which does what you need.

Thanks,
Yevhen 

Sent from my BlackBerry® PlayBook™
www.blackberry.com


From: "Alok Agarwal" <your...@gmail.com>
To: "testng...@googlegroups.com" <testng...@googlegroups.com>
Sent: September 3, 2012 8:11 PM
Subject: Re: [testng-users] Re: Confusion With Creating TestNG.XML

Krishnan Mahadevan

unread,
Sep 3, 2012, 8:43:58 PM9/3/12
to testng...@googlegroups.com
I will have to second Yevhen on that. Until now your query was related to TestNG. But your test openig up multiple instances of browser for each test is NOT TestNG related. Please post this on the selenium-users forum. 

That being said your test seems to create a FirefoxDriver instance for each test class which probably is the reason. 

There are multiple ways of dealing with this. 

1. Having a common base class, instantiating it in a BeforeSuite/BeforeTest. 
2.  Creating the Webdriver instance in a singleton and then just using it in all your tests.  
3.  Creating the webdriver in a separate class via listener invocation and using it. 

Its upto you on how you want to use it. To begin with I suggest that you please spend sometime playing around with TestNG and use ordinary ovjects and see how things can be shared/re-used in your tests. 

--
Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
Reply all
Reply to author
Forward
0 new messages