[testng-dev] Factory approach

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

[testng-dev] Factory approach

Christopher Edwards
Hello,

I am new to using TestNG and think it's an excellent framework.
Forgive me if this has been answered elsewhere, but I've searched and searched and tried every way I could find, including @Priority etc...Maybe there is a better approach and I would love to hear suggestions and criticisms. Thanks to you Guru's in advance. I hope I can add valuable content at some point.

Problem: Need to iterate through dated data (through selenium), where the dates to iterate over is constantly changing. Thus, a factory pattern seemed perfect to build up a test set at run time based on a DB query or other dynamic input.
The issue is, I want to mark the last test to allow the test to close the browser, although the order is never preserved.

Here is a quick example of the approach I was hoping to use.

TestNG XML Factory entry:
-----------------------------------------------------------------------------
<suite name="Testing" preserve-order="true" single-threaded="true">
    <test name="Daily Validation" preserve-order="true" single-threaded="true" enabled="true">
        <classes>
            <class name="test.FactoryExampleFactory"/>
        </classes>
    </test>
</suite>


The Factory
-----------------------------------------------------------------------------

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Factory;

import java.util.ArrayList;
import java.util.List;

public class FactoryExampleFactory {

    private WebDriver driver;

    /**
     * The number of days to validate will be determined at runtime and
     * will change as the daily data set grows. For clarity in this example
     * it is hard coded.
     */
    private static final int DAYS_TO_VALIDATE = 5;

    public void setup() {
        /* Create a new instance of the Web driver. I want to use the same driver
         * for the entire set of tests, as it's very slow opening a new browser for
         * every page. Not to mention it does not fit the data validation model
         * I am using to iterate over an indeterminate list of days.
         */
        driver = new FirefoxDriver();
    }

    public void tearDown() {
        // once the test is done.
        driver.quit();
    }

    @Factory
    public Object[] changePage() throws InterruptedException {

        setup();
        final List results = new ArrayList();

        for (int index = 1; index <= DAYS_TO_VALIDATE; index++) {
            if (index == DAYS_TO_VALIDATE) {
                results.add(new FactoryExampleTest(driver, true));
            } else {
                results.add(new FactoryExampleTest(driver, false));
            }
        }

        // This doesn't work as the tests have not run yet.
        //tearDown();
        return results.toArray();
    }
}


The Test
-------------------------------------------------------------------

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class FactoryExampleTest {

    private WebDriver driver;

    private boolean last;

    FactoryExampleTest(final WebDriver driver, final boolean imLast) {
        this.driver = driver;
        last = imLast;
    }

    @Test(singleThreaded = true)
    public void runTest() {

        // navigate to the next day and validate some data.

        if(last) {
            System.out.println("I'm the last test!!!");
            // Close the browser
        } else {
            System.out.println("I'm NOT the last test!!!");
            // Don't close the browser as other test need to use it
        }
    }
}

The output of multiple runs:
-----------------------------------

Run 1:
Running TestSuite
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm the last test!!!
I'm NOT the last test!!!

Run 2:
Running TestSuite
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm the last test!!!

Run 3:
Running TestSuite
I'm NOT the last test!!!
I'm the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!

As you can see the ordering is non-deterministic as I had hoped it would be.

Any Help?

Thanks,
Chris



--
You received this message because you are subscribed to the Google Groups "testng-dev" 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 http://groups.google.com/group/testng-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply | Threaded
Open this post in threaded view
|

Re: [testng-dev] Factory approach

Cédric Beust ♔-2
Hi Christopher,

You might want to send this to testng-users, this is the dev mailing-list (my mistake for approving it here).


-- 
Cédric



On Fri, Feb 1, 2013 at 7:11 AM, Christopher Edwards <[hidden email]> wrote:
Hello,

I am new to using TestNG and think it's an excellent framework.
Forgive me if this has been answered elsewhere, but I've searched and searched and tried every way I could find, including @Priority etc...Maybe there is a better approach and I would love to hear suggestions and criticisms. Thanks to you Guru's in advance. I hope I can add valuable content at some point.

Problem: Need to iterate through dated data (through selenium), where the dates to iterate over is constantly changing. Thus, a factory pattern seemed perfect to build up a test set at run time based on a DB query or other dynamic input.
The issue is, I want to mark the last test to allow the test to close the browser, although the order is never preserved.

Here is a quick example of the approach I was hoping to use.

TestNG XML Factory entry:
-----------------------------------------------------------------------------
<suite name="Testing" preserve-order="true" single-threaded="true">
    <test name="Daily Validation" preserve-order="true" single-threaded="true" enabled="true">
        <classes>
            <class name="test.FactoryExampleFactory"/>
        </classes>
    </test>
</suite>


The Factory
-----------------------------------------------------------------------------

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Factory;

import java.util.ArrayList;
import java.util.List;

public class FactoryExampleFactory {

    private WebDriver driver;

    /**
     * The number of days to validate will be determined at runtime and
     * will change as the daily data set grows. For clarity in this example
     * it is hard coded.
     */
    private static final int DAYS_TO_VALIDATE = 5;

    public void setup() {
        /* Create a new instance of the Web driver. I want to use the same driver
         * for the entire set of tests, as it's very slow opening a new browser for
         * every page. Not to mention it does not fit the data validation model
         * I am using to iterate over an indeterminate list of days.
         */
        driver = new FirefoxDriver();
    }

    public void tearDown() {
        // once the test is done.
        driver.quit();
    }

    @Factory
    public Object[] changePage() throws InterruptedException {

        setup();
        final List results = new ArrayList();

        for (int index = 1; index <= DAYS_TO_VALIDATE; index++) {
            if (index == DAYS_TO_VALIDATE) {
                results.add(new FactoryExampleTest(driver, true));
            } else {
                results.add(new FactoryExampleTest(driver, false));
            }
        }

        // This doesn't work as the tests have not run yet.
        //tearDown();
        return results.toArray();
    }
}


The Test
-------------------------------------------------------------------

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class FactoryExampleTest {

    private WebDriver driver;

    private boolean last;

    FactoryExampleTest(final WebDriver driver, final boolean imLast) {
        this.driver = driver;
        last = imLast;
    }

    @Test(singleThreaded = true)
    public void runTest() {

        // navigate to the next day and validate some data.

        if(last) {
            System.out.println("I'm the last test!!!");
            // Close the browser
        } else {
            System.out.println("I'm NOT the last test!!!");
            // Don't close the browser as other test need to use it
        }
    }
}

The output of multiple runs:
-----------------------------------

Run 1:
Running TestSuite
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm the last test!!!
I'm NOT the last test!!!

Run 2:
Running TestSuite
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm the last test!!!

Run 3:
Running TestSuite
I'm NOT the last test!!!
I'm the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!

As you can see the ordering is non-deterministic as I had hoped it would be.

Any Help?

Thanks,
Chris



--
You received this message because you are subscribed to the Google Groups "testng-dev" 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 http://groups.google.com/group/testng-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "testng-dev" 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 http://groups.google.com/group/testng-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply | Threaded
Open this post in threaded view
|

Re: [testng-dev] Factory approach

Christopher Edwards
Thanks Cedric

On Friday, February 1, 2013 12:02:39 PM UTC-5, Cédric Beust ♔ wrote:
Hi Christopher,

You might want to send this to testng-users, this is the dev mailing-list (my mistake for approving it here).


-- 
Cédric



On Fri, Feb 1, 2013 at 7:11 AM, Christopher Edwards <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Qaqghhf_oOkJ">chri...@...> wrote:
Hello,

I am new to using TestNG and think it's an excellent framework.
Forgive me if this has been answered elsewhere, but I've searched and searched and tried every way I could find, including @Priority etc...Maybe there is a better approach and I would love to hear suggestions and criticisms. Thanks to you Guru's in advance. I hope I can add valuable content at some point.

Problem: Need to iterate through dated data (through selenium), where the dates to iterate over is constantly changing. Thus, a factory pattern seemed perfect to build up a test set at run time based on a DB query or other dynamic input.
The issue is, I want to mark the last test to allow the test to close the browser, although the order is never preserved.

Here is a quick example of the approach I was hoping to use.

TestNG XML Factory entry:
-----------------------------------------------------------------------------
<suite name="Testing" preserve-order="true" single-threaded="true">
    <test name="Daily Validation" preserve-order="true" single-threaded="true" enabled="true">
        <classes>
            <class name="test.FactoryExampleFactory"/>
        </classes>
    </test>
</suite>


The Factory
-----------------------------------------------------------------------------

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Factory;

import java.util.ArrayList;
import java.util.List;

public class FactoryExampleFactory {

    private WebDriver driver;

    /**
     * The number of days to validate will be determined at runtime and
     * will change as the daily data set grows. For clarity in this example
     * it is hard coded.
     */
    private static final int DAYS_TO_VALIDATE = 5;

    public void setup() {
        /* Create a new instance of the Web driver. I want to use the same driver
         * for the entire set of tests, as it's very slow opening a new browser for
         * every page. Not to mention it does not fit the data validation model
         * I am using to iterate over an indeterminate list of days.
         */
        driver = new FirefoxDriver();
    }

    public void tearDown() {
        // once the test is done.
        driver.quit();
    }

    @Factory
    public Object[] changePage() throws InterruptedException {

        setup();
        final List results = new ArrayList();

        for (int index = 1; index <= DAYS_TO_VALIDATE; index++) {
            if (index == DAYS_TO_VALIDATE) {
                results.add(new FactoryExampleTest(driver, true));
            } else {
                results.add(new FactoryExampleTest(driver, false));
            }
        }

        // This doesn't work as the tests have not run yet.
        //tearDown();
        return results.toArray();
    }
}


The Test
-------------------------------------------------------------------

import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;

public class FactoryExampleTest {

    private WebDriver driver;

    private boolean last;

    FactoryExampleTest(final WebDriver driver, final boolean imLast) {
        this.driver = driver;
        last = imLast;
    }

    @Test(singleThreaded = true)
    public void runTest() {

        // navigate to the next day and validate some data.

        if(last) {
            System.out.println("I'm the last test!!!");
            // Close the browser
        } else {
            System.out.println("I'm NOT the last test!!!");
            // Don't close the browser as other test need to use it
        }
    }
}

The output of multiple runs:
-----------------------------------

Run 1:
Running TestSuite
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm the last test!!!
I'm NOT the last test!!!

Run 2:
Running TestSuite
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm the last test!!!

Run 3:
Running TestSuite
I'm NOT the last test!!!
I'm the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!
I'm NOT the last test!!!

As you can see the ordering is non-deterministic as I had hoped it would be.

Any Help?

Thanks,
Chris



--
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Qaqghhf_oOkJ">testng-dev+...@googlegroups.com.
To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Qaqghhf_oOkJ">testn...@....
Visit this group at http://groups.google.com/group/testng-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google Groups "testng-dev" 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 http://groups.google.com/group/testng-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.