Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

Kritika Gupta
Below is my code , where I am using @BeforeMethod to setup IE instance and passing driver to @Test using dependsOnMethods , also passing data provider to same @Test.

But its giving me exception, data provider mismatch.

What I am doing wrong?

Also If dataprovider returns 3 values that mean @Test will run 3 times, then @Before method should also run 3 times, so 3 different driver instances will get create for each @test, Is my understanding right?

@BeforeMethod


@Parameters({ "Environment", "userName", "Password" })


public static void main(String Environment, String userName, String Password) {




String service = "D:\\IE Driver\\3.6.0\\IEDriverServer_Win32_3.6.0\\IEDriverServer.exe";


System.setProperty("webdriver.ie.driver", service);


InternetExplorerDriver driver = new InternetExplorerDriver();


driver.get(Environment);

driver.findElement(By.id("username")).sendKeys(userName);


driver.findElement(By.id("password")).sendKeys(Password);


driver.findElement(By.id("submitButton")).click();




}




@Test(dataProvider = "returnTestcaseRow", dependsOnMethods = "Main" )

public static void maintrade(WebDriver driver, String flag, String TestCaseName...multiple columns are in testObjArray){

 
try {

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");


EATradeCreation.CreationEATrade(driver,flag, TestCaseName...multiple columns in testObjArray);


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*

* @Test public static void callScreen(String TestCaseName) throws Exception

* { while(true) { EAScreenshot.captureScreenShot(TestCaseName);

* Thread.sleep(10000); }

*

*

* }

*/

@DataProvider(name = "returnTestcaseRow")

public Object[][] EOT_testcasedata() throws Exception {

Object[][] testObjArray;

testObjArray = EOTdata.getTableArray("D:\\Automation\\EAAUTO\\src\\testData\\EOT_TestData_Updated.xlsx");

return testObjArray;

}


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

Todd Bradley-2
I see four things here that look wrong. There may be more.

1. TestNG is going to pass in each row of testObjArray from EOT_testcasedata into your method maintrade, which means trying to cast the first element to an instance of WebDriver. Can whatever EOTdata.getTableArray returns be cast to a WebDriver? I doubt it.

2. Why is maintrade a static method? Something seems fishy about that.

3. Java properties are global in scope. So System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger"); only needs to be once, but it is also going to affect any other tests that are running at the same time. Is that what you want? I don't see that your test sets that property back when it's done, so your test isn't cleaning up after itself very well. If you really want to set that property globally and never set it back, don't put it inside the test method. Same with System.setProperty("webdriver.ie.driver", service);

4. Naming your @BeforeMethod method main seems like a bad idea. It's very deceptive, at the very least.




On Wed, Nov 1, 2017 at 11:12 PM, Kritika Gupta <[hidden email]> wrote:
Below is my code , where I am using @BeforeMethod to setup IE instance and passing driver to @Test using dependsOnMethods , also passing data provider to same @Test.

But its giving me exception, data provider mismatch.

What I am doing wrong?

Also If dataprovider returns 3 values that mean @Test will run 3 times, then @Before method should also run 3 times, so 3 different driver instances will get create for each @test, Is my understanding right?

@BeforeMethod


@Parameters({ "Environment", "userName", "Password" })


public static void main(String Environment, String userName, String Password) {




String service = "D:\\IE Driver\\3.6.0\\IEDriverServer_Win32_3.6.0\\IEDriverServer.exe";


System.setProperty("webdriver.ie.driver", service);


InternetExplorerDriver driver = new InternetExplorerDriver();


driver.get(Environment);

driver.findElement(By.id("username")).sendKeys(userName);


driver.findElement(By.id("password")).sendKeys(Password);


driver.findElement(By.id("submitButton")).click();




}




@Test(dataProvider = "returnTestcaseRow", dependsOnMethods = "Main" )

public static void maintrade(WebDriver driver, String flag, String TestCaseName...multiple columns are in testObjArray){

 
try {

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");


EATradeCreation.CreationEATrade(driver,flag, TestCaseName...multiple columns in testObjArray);


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*

* @Test public static void callScreen(String TestCaseName) throws Exception

* { while(true) { EAScreenshot.captureScreenShot(TestCaseName);

* Thread.sleep(10000); }

*

*

* }

*/

@DataProvider(name = "returnTestcaseRow")

public Object[][] EOT_testcasedata() throws Exception {

Object[][] testObjArray;

testObjArray = EOTdata.getTableArray("D:\\Automation\\EAAUTO\\src\\testData\\EOT_TestData_Updated.xlsx");

return testObjArray;

}


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

Krishnan Mahadevan
In reply to this post by Kritika Gupta

Kritika,

 

Not sure what you are trying to do again. Please help clarify your use case to start off with. It sounds like you are trying to put things together in a haphazard fashion.

 

  • If you define a method by annotating it with @Parameter, then its expected that the method is going to receive some values from the suite xml file and NOT from a data provider.
  • You have your @Test method depend on a @BeforeMethod. That’s not how TestNG works. @Test methods are expected to depend on other @Test methods. Any @Before/@After methods are classified as configuration methods, which TestNG automatically runs before and after the @Test methods are executed.

 

So I am going to re-iterate.

Please help call out what is the use-case you are trying to solve.

That should clarify a lot of things!

 

 

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/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

From: <[hidden email]> on behalf of Kritika Gupta <[hidden email]>
Reply-To: <[hidden email]>
Date: Thursday, November 2, 2017 at 10:42 AM
To: testng-users <[hidden email]>
Subject: [testng-users] Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

 

Below is my code , where I am using @BeforeMethod to setup IE instance and passing driver to @Test using dependsOnMethods , also passing data provider to same @Test.

 

But its giving me exception, data provider mismatch.

 

What I am doing wrong?

 

Also If dataprovider returns 3 values that mean @Test will run 3 times, then @Before method should also run 3 times, so 3 different driver instances will get create for each @test, Is my understanding right?

 

@BeforeMethod

 

@Parameters({ "Environment", "userName", "Password" })

 

public static void main(String Environment, String userName, String Password) {

 

 

 

String service = "D:\\IE Driver\\3.6.0\\IEDriverServer_Win32_3.6.0\\IEDriverServer.exe";

 

System.setProperty("webdriver.ie.driver", service);

 

InternetExplorerDriver driver = new InternetExplorerDriver();

 

driver.get(Environment);

driver.findElement(By.id("username")).sendKeys(userName);

 

driver.findElement(By.id("password")).sendKeys(Password);

 

driver.findElement(By.id("submitButton")).click();

 

 

 

}

 

 

 

@Test(dataProvider = "returnTestcaseRow", dependsOnMethods = "Main" )

public static void maintrade(WebDriver driver, String flag, String TestCaseName...multiple columns are in testObjArray){

 

try {

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");

 

EATradeCreation.CreationEATrade(driver,flag, TestCaseName...multiple columns in testObjArray);

 

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*

* @Test public static void callScreen(String TestCaseName) throws Exception

* { while(true) { EAScreenshot.captureScreenShot(TestCaseName);

* Thread.sleep(10000); }

*

*

* }

*/

@DataProvider(name = "returnTestcaseRow")

public Object[][] EOT_testcasedata() throws Exception {

Object[][] testObjArray;

testObjArray = EOTdata.getTableArray("D:\\Automation\\EAAUTO\\src\\testData\\EOT_TestData_Updated.xlsx");

return testObjArray;

}

 

 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

Kritika Gupta
In reply to this post by Kritika Gupta
Apology  this is my first project for which I am building a framework.

I have 100s of test cases, I want to test all in below manner:

1) I have one test data excel sheet which will have test data corresponding to test case name


2) My main method will depend on dataprovider, DP will return total no. of rows , for that many time only mainmethod will run and after each run it will quit the driver again for next row it will establish a session.
Foreach row i want to establish a new session and at the end want to quit it.

3) I want to make it environment independent ( SIT, UAT ), for that i want to prepate testng.xml and want to give URL/Credentials there as parameter so user will chnage URL/credentials only there not in code.

4) I want to take screenshot of each and everypage and want to store it in mainfolder (Named as todaysdatetimestamp) inside that folder , another folder named as test case name like TC001, TC002 etc. inside TC001 all the screenshots of execution
DATETIMESTAPFOLDER>> Testcasename folder>> screenhsots of particular testcase

Please guide me how can I achieve this.

Regards,
Kritika

On Thursday, November 2, 2017 at 10:42:46 AM UTC+5:30, Kritika Gupta wrote:
Below is my code , where I am using @BeforeMethod to setup IE instance and passing driver to @Test using dependsOnMethods , also passing data provider to same @Test.

But its giving me exception, data provider mismatch.

What I am doing wrong?

Also If dataprovider returns 3 values that mean @Test will run 3 times, then @Before method should also run 3 times, so 3 different driver instances will get create for each @test, Is my understanding right?

@BeforeMethod


@Parameters({ "Environment", "userName", "Password" })


public static void main(String Environment, String userName, String Password) {




String service = "D:\\IE Driver\\3.6.0\\IEDriverServer_Win32_3.6.0\\IEDriverServer.exe";


System.setProperty("webdriver.ie.driver", service);


InternetExplorerDriver driver = new InternetExplorerDriver();


driver.get(Environment);

driver.findElement(By.id("username")).sendKeys(userName);


driver.findElement(By.id("password")).sendKeys(Password);


driver.findElement(By.id("submitButton")).click();




}




@Test(dataProvider = "returnTestcaseRow", dependsOnMethods = "Main" )

public static void maintrade(WebDriver driver, String flag, String TestCaseName...multiple columns are in testObjArray){

 
try {

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");


EATradeCreation.CreationEATrade(driver,flag, TestCaseName...multiple columns in testObjArray);


} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*

* @Test public static void callScreen(String TestCaseName) throws Exception

* { while(true) { EAScreenshot.captureScreenShot(TestCaseName);

* Thread.sleep(10000); }

*

*

* }

*/

@DataProvider(name = "returnTestcaseRow")

public Object[][] EOT_testcasedata() throws Exception {

Object[][] testObjArray;

testObjArray = EOTdata.getTableArray("D:\\Automation\\EAAUTO\\src\\testData\\EOT_TestData_Updated.xlsx");

return testObjArray;

}


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

Krishnan Mahadevan

You basically should be looking at coupling a Factory with a data provider to get this usecase done.

 

Here’s a sample

 

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.ITestContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

public class TestClassSample {
   
private String param1, param2;
   
private WebDriver driver;

    
@Factory(dataProvider = "dp")
   
public TestClassSample(String a, String b) {
       
this.param1 = a;
       
this.param2 = b;
    }

   
@BeforeMethod
   
public void setup(ITestContext context) {
        String environment = context.getCurrentXmlTest().getParameter(
"environment");
        String username = context.getCurrentXmlTest().getParameter(
"username");
        String password = context.getCurrentXmlTest().getParameter(
"password");
       
driver = new FirefoxDriver();
       
driver.get(environment);
    }

   
@Test
   
public void testMethod() {
       
//Include logic here for the test that works with testData
       
System.err.println("Test data [" + param1 + ", " + param2 + "]");
    }

   
@AfterMethod
   
public void cleanup() {
       
driver.quit();
    }

   
@DataProvider(name = "dp")
   
public static Object[][] getData() {
       
return new Object[][]{
                {
"x", "y"},
                {
"b", "c"}
        };
    }
}

 

Here’s the suite xml :

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="sample_suite" parallel="false" verbose="2">
    <
test name="test_1" verbose="2">
        <
parameter name="environment" value="http://www.google.com"/>
        <
parameter name="username" value="user1"/>
        <
parameter name="password" value="pass1"/>
       <
classes>
            <
class name="com.rationaleemotions.googleforums.kritika.TestClassSample"/>
        </
classes>
    </
test>

    <
test name="test_2" verbose="2">
        <
parameter name="environment" value="http://www.facebook.com"/>
        <
parameter name="username" value="user2"/>
        <
parameter name="password" value="pass2"/>
        <
classes>
            <
class name="com.rationaleemotions.googleforums.kritika.TestClassSample"/>
        </
classes>
    </
test>

</
suite>

 

 

PS : If you are not familiar with all these concepts, I would strongly recommend that you please spend time getting yourself familiarized with them, before building a framework using them.

 

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/

My Technical Scribbings @ http://rationaleemotions.wordpress.com/

 

From: <[hidden email]> on behalf of Kritika Gupta <[hidden email]>
Reply-To: <[hidden email]>
Date: Monday, November 6, 2017 at 11:35 AM
To: testng-users <[hidden email]>
Subject: [testng-users] Re: Can I pass dependsOnMethods parameter and dataprovider parameter together to @Test

 

Apology  this is my first project for which I am building a framework.

 

I have 100s of test cases, I want to test all in below manner:

 

1) I have one test data excel sheet which will have test data corresponding to test case name

 

2) My main method will depend on dataprovider, DP will return total no. of rows , for that many time only mainmethod will run and after each run it will quit the driver again for next row it will establish a session.
Foreach row i want to establish a new session and at the end want to quit it.

3) I want to make it environment independent ( SIT, UAT ), for that i want to prepate testng.xml and want to give URL/Credentials there as parameter so user will chnage URL/credentials only there not in code.

4) I want to take screenshot of each and everypage and want to store it in mainfolder (Named as todaysdatetimestamp) inside that folder , another folder named as test case name like TC001, TC002 etc. inside TC001 all the screenshots of execution
DATETIMESTAPFOLDER>> Testcasename folder>> screenhsots of particular testcase

Please guide me how can I achieve this.

 

Regards,

Kritika

On Thursday, November 2, 2017 at 10:42:46 AM UTC+5:30, Kritika Gupta wrote:

Below is my code , where I am using @BeforeMethod to setup IE instance and passing driver to @Test using dependsOnMethods , also passing data provider to same @Test.

 

But its giving me exception, data provider mismatch.

 

What I am doing wrong?

 

Also If dataprovider returns 3 values that mean @Test will run 3 times, then @Before method should also run 3 times, so 3 different driver instances will get create for each @test, Is my understanding right?

 

@BeforeMethod

 

@Parameters({ "Environment", "userName", "Password" })

 

public static void main(String Environment, String userName, String Password) {

 

 

 

String service = "D:\\IE Driver\\3.6.0\\IEDriverServer_Win32_3.6.0\\IEDriverServer.exe";

 

System.setProperty("webdriver.ie.driver", service);

 

InternetExplorerDriver driver = new InternetExplorerDriver();

 

driver.get(Environment);

driver.findElement(By.id("username")).sendKeys(userName);

 

driver.findElement(By.id("password")).sendKeys(Password);

 

driver.findElement(By.id("submitButton")).click();

 

 

 

}

 

 

 

@Test(dataProvider = "returnTestcaseRow", dependsOnMethods = "Main" )

public static void maintrade(WebDriver driver, String flag, String TestCaseName...multiple columns are in testObjArray){

 

try {

System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Jdk14Logger");

 

EATradeCreation.CreationEATrade(driver,flag, TestCaseName...multiple columns in testObjArray);

 

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/*

* @Test public static void callScreen(String TestCaseName) throws Exception

* { while(true) { EAScreenshot.captureScreenShot(TestCaseName);

* Thread.sleep(10000); }

*

*

* }

*/

@DataProvider(name = "returnTestcaseRow")

public Object[][] EOT_testcasedata() throws Exception {

Object[][] testObjArray;

testObjArray = EOTdata.getTableArray("D:\\Automation\\EAAUTO\\src\\testData\\EOT_TestData_Updated.xlsx");

return testObjArray;

}

 

 

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.