I want to understand how do we execute multiple test cases using testNg. Suppose my web application consists of 10 pages.Then how do we execute the below test cases.
1) 1st TC- Traversing or navigating to page 1,2,3,4,5&6.
2) 2nd TC- Traversing or navigating to page 1,2,3,8,9&10.
3) 3rd TC- Traversing or navigating to page 1,2,6,7,8.
all pages have corresponding priorities. Page 1 has priority 1, Page 2 has priority 2 etc.
Is it that we need to call related methods(methods defined in each page.) in each #Test annotations.
Thanks!
It seems like you have one #Test annotated method for each page. If they can traverse correctly logically as you have mentioned, then to run them for your test cases , I would remove priorities from TC's and use this xml with preserve-order="true" , so they run in same order. You can add more TC to below in same manner. Have a look at this
Below xml will call methods in that order , you need to make sure they can go to your pages correctly
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="yourSuiteName" >
<test name="1stTC" preserve-order="true">
<classes>
<class name="yourPackage.YourClass" >
<methods>
<include name="method1" />
<include name="method2" />
<include name="method3" />
<include name="method4" />
<include name="method5" />
<include name="method6" />
</methods>
</class>
<classes>
</test>
<test name="2ndTC" preserve-order="true">
<classes>
<class name="yourPackage.YourClass">
<methods>
<include name="method1" />
<include name="method2" />
<include name="method3" />
<include name="method8" />
<include name="method9" />
<include name="method10" />
</methods>
</class>
</classes>
</test>
</suite>
If you have multiple pages then you can have Page object class for each web page. that class can have multiple methods which implemented as operation that could be performed on that page and may return object of next page it is navigating to. Using such objects of Page object classes and their methods, you can design your test method that will be considered as Test case.
E.g.
- for Login page, define Login.java with all the required elements on the page and define method as below
public Homepage loginAction(String Username, String Password){
// write code to perform login opeartion
// it returns Homepage object that you can store in Homepage type variable and you can call other operations of Homepage on that object.
}
Once, you have all the Page object classes are ready to consume, you can write test cases by calling tat methods.
E.g.:
#Test
public void TestCaseOne(){
Login loginpage = new Login();
Homepage homepage = loginpage.loginAction("ABC","XYZ");
homepage.selectAcc(1);
}
Related
This is the testNG.xml File:
This is the day4.java File
]
This is the console Result.
Why I am getting this issue?
I also tried with #optional method but in the that way I am getting the NULL value.
You are not passed any parameter value in the testng.xml file.Your xml file should look like
<suite name="Parameter test Suite" verbose="1">
<!-- This parameter will be passed to every test in this suite -->
<parameter name="suite-param" value="suite level parameter" />
<test name="Home loan">
<classes>
<class name="test">
<methods>
<include name="demo" />
</methods>
</class>
</classes>
</test>
</suite>
Here parameter name and value will be your URL
Few more details about xml file
https://developers.perfectomobile.com/display/TT/TestNG+-User+specified+TestNG+xml+parameters
So you have to make 2 changes.
Your code should look like this:
#Parameter("URL")
#Test
public void webloginHomeloan(#Optional String urlname){
System.out.println("WebloginHome");
System.out.println("Parameterized value is:"+ URL)
}
Add #Optional in parameter as you are not passing any value from String urlname and change "urlname" to "URL"
I am trying to run the test parallel using dataprovider. I have mentioned dataproviderthreadcount=3 in testng xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="3"parallel="methods">
<test name="Test">
<classes>
<class name="com.sample.test">
</class>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
Test methods:
#Test(dataProvider = "dp1", threadPoolSize=3,invocationCount=1)
public void Testsuitesample(String url, String add1, String add2){}
Result: 3 browser instances get opened and all three data is passing to only browser. Other browser's are still idle. Is it a way to resolve this?
You may need to set parallel to true in your data provider method like,
#DataProvider(parallel = true)
public Object[][] dp1() {
}
Also, the invocation count should be equal or greater than the thread pool size.
I have 15 test methods in 3 Java classes (Selenium Script). I want to run each Test class with new window. I am using TestNg framework.
Here is the code of TestNG:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Fanfight Test" thread-count="10" parallel="methods">
<listeners>
<listener class-name="com.fanfight.test_case.ListenerClass">
</listener>
</listeners>
<test name="User Login" parallel="false">
<classes>
<class name="com.fanfight.test_case.UserLogin"></class>
</classes>
</test>
<test name="Contest Creation" parallel="false" >
<classes>
<class name="com.fanfight.test_case.ContestCreation"></class>
</classes>
</test>
<test name="User Profile Test" parallel="false" >
<classes>
<class name="com.fanfight.test_case.UserProfileTest"></class>
</classes>
</test>
<test name="Menu Bar Test" parallel="false">
<classes>
<class name="com.fanfight.test_case.MenuBarTest">
</class>
</classes>
</test>
<test name="Home Page Elements" parallel="false" >
<classes>
<class name="com.fanfight.test_case.HomePageElementTest"></class>
</classes>
</test>
</suite>
Without using parallel="false" my script is running in alphabetical order due to which selenium unable to find the path and execution got stuck.
Also please suggest how to make execution continue even after getting an exception during execution.
Add a setup and tear down method in each of the 3 test classes. The setup method should launch the browser and teardown method should close that browser instance.
class TestOne {
WebDriver driver;
#BeforeClass
public void setup(){
driver = new ChromeDriver();
}
#Test
public void testCase1(){
}
//.... Other test methods
#AfterClass
public void tearDown(){
driver.quit();
}
You can also create a parent class having just the setup and tear down methods , pseudo coded above. All 3 of your test class shall extend this parent class. It will be an optimised approach as the driver instantiation and destruction is now centralised to a single class.
And finally , change the parallel attribute in the suite tag of your testNG xml, in order to make them run parallel.
<suite name="Fanfight Test" thread-count="10" parallel="classes">
I am using Page object pattern with selenium webdriver and testng.
I want to access instance of one page object class in another class across multiple <test>.
for e.g.
<test name="Login scenario">
<classes>
<class name="sanitytests.LoginTests">
<methods>
<include name="validLogin"/>
</methods>
</class>
</classes>
</test>
<test name="scenario2" preserve-order="true" parallel="false">
<classes>
<class name="sanitytests.HomePageTests">
<methods>
<include name="clickOnMyAccountFromHome"/>
</methods>
</class>
</classes>
</test>
in my LoginTests class I am using instance of homePage class
#Test()
public void validLogin(ITestContext context) throws Exception {
loginPage.loginDetails(username,password);
homePage = loginPage.loginAsValidUser();
context.setAttribute("homePage",homePage);
}
my HomePageTests class
#Test()
public void clickOnMyAccountFromHome(ITestContext context) throws Exception {
homePage = (HomePage) context.getAttribute("homePage");
myAccountPage = homePage.navigateToMyAccountPage();
context.setAttribute("myAccountPage", myAccountPage);
}
i am getting null pointer exception because ItestContext is used to share parameter between methods not between tests. Is there any alternative?
From what i have read about "ITestContext", it is related to TestNG classes/objects/methods. You can create the HomePage as a static and it will be available for entire suite run.
The page object pattern does not state that objects can only be created once. Create new instances in each test.
I have a module A which runs through pom.xml and accesses a testng.xml which in turn calls a specific class.This class displays a list of things for the user to choose from.
I am taking the user value through bufferRead.The bufferRead does not detect the value entered by the User.
ie
Enter the test u want to run
1.Test1
2.Test2
3.Test3
1
(control never goes to the next line)
There goes my pom.xml
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>`
This is my testng.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><suite allow-return-values="false" configfailurepolicy="skip" data-provider-thread-count="10" group-by-instances="false" junit="false" name="Suite" parallel="false" preserve-order="true" skipfailedinvocationcounts="false" thread-count="5">
<test allow-return-values="false" group-by-instances="false" junit="false" name="Test" preserve-order="true" skipfailedinvocationcounts="false">
<classes>
<class name="com.org.Console1"<methods>
<include name="main" />
</methods>
</class>
</classes>
</test> <!-- Test -->
This is my java code that runs
System.out.println("Which tests do you want to run");
String input = bufferRead.readLine();