SobiborTreblinka
SobiborTreblinka

Reputation: 575

Design dependencies in JUnit Test Class

I am new to Junit and testing frameworks in general, here is my question.

I have the following code:

public class Login extends WebsiteTestCase
{   
    @Test
    public void testChromeLogin()
    {
        setDriver( getChromeDriver() );
        login( getDriver() );
    }

    public void login( WebDriver driver )
    {
        driver.get( getBaseURL() );

        WebElement username = driver.findElement( By.name("login_username") );
        WebElement password = driver.findElement( By.name("login_password") );
        WebElement submit = driver.findElement( By.cssSelector( "form#login input" ) ); 

        username.sendKeys( getUsername() );
        password.sendKeys( getPassword() );
        submit.submit();

        assertEquals( "Website HomePage", driver.getTitle() );
    }

    @After
    public void tearDown() throws Exception
    {
        getDriver().quit();
    }
}

and

public class WebsiteTestCase extends StockholmTestCase
{
    public String username = "login", password_login = "password";
    private String baseURL = "http://website.com";

    public String getUsername()
    {
        return this.username;
    }

    public String getPassword()
    {
        return this.password_login;
    }

    public String getBaseURL()
    {
        return this.baseURL;
    }
}

and

public class StockholmTestCase extends TestCase
{
    private WebDriver driver;

    public WebDriver getChromeDriver()
    {
        return new RemoteWebDriver( DesiredCapabilities.chrome() );
    }

    public WebDriver getFireFoxDriver()
    {
        return new RemoteWebDriver( DesiredCapabilities.firefox() );
    }

    public WebDriver getDriver()
    {
        return this.driver;
    }

    public void setDriver( WebDriver driver )
    {
        this.driver = driver;
    }
}

As you can see in the WebsiteTestClass, I am hard-coding the current login username and password, and baseURL which works perfectly fine for my tests. But if I want to "programatically" set an environment for testing (perhaps for different programmers different login, password, different baseurl, ex: "http://Bob.dev.website.com"), then instead, I would like to be able to create an instance of WebsiteTestCase with different properties for login credentials, with a constructor.

like

public class Stockholm
{
    public static void main( String args[] )
    {
        JUnitCore junit = new JUnitCore();
        Result result = junit.run( new Login( /* credentials here, settings */ ) );
    }
}

as opposed to

public class Stockholm
{
    public static void main( String args[] )
    {
        JUnitCore junit = new JUnitCore();
        Result result = junit.run( Login.class );
    }
}

I understand in the above code that I am not passing an instance of Login to junit.run, so how would I design code to accomplish those things in this context?

Upvotes: 0

Views: 798

Answers (1)

Prasanna
Prasanna

Reputation: 3771

You can have property file in the project, like webdriver-test-dev.properties and have the login credentials & URL defined in it. You can load that property file and inject properties during the test.

Environment can be passed as command line argument like -Denv=dev and using this value the right property file can be picked considering the file name if of a predefined format like webdriver-<env>.properties.

Also on a related not, for a test case it is better to use one common test login instead of having each programmer use his or her own login.

Upvotes: 2

Related Questions