[Question] Parameter + DataProvider

103 vues
Accéder directement au premier message non lu

Keith Monsanto

non lue,
3 mars 2015, 13:30:4503/03/2015
à testng...@googlegroups.com
Hello,

I have a question or looking for suggestion on how to use both efficiently.

My current setup:
testng.xml contains 25 parameters and the testcase.
I created DataProviderClass.class where I create many DataProvider for my test (blanklogin, wrongusernamelogin, wrongpasswordlogin, wronguserwrongpassword, correctlogindetails).

Each dataprovider have a static text containing all parameter names intended for that test which I then split and put into a hashmap. The hashmap is then put inside the object[][] as dataprovider doesn't work with hashmap. So I now use the hashmap values for my test.

My concern is that testng.xml will become loaded with parameters and might be difficult to organize them. Is there a way to import parameters outside testng.xml?

parameters for login goes to login.xml
parameters for operations goes to operations.xml
etc.

Many thanks,
Keith

Hubert Grzeskowiak

non lue,
4 mars 2015, 11:08:1304/03/2015
à testng...@googlegroups.com
Hi Keith,

it seems you're using one testcase for what usually is multiple test cases. Even though the steps seem same, the expectation is different, so you might want to create test cases like testBlankLogin, testWrongUserNameLogin etc. To prevent duplicated code you could use a generic method loginUsing(String, String) in all of the test cases. This solution is more verbose, since the test name tells you what worked and what not. If you do this, there's probably no need for any data providers anymore - you can hardcode the credentials in the tests. If you have more than a few parameters for each test case, you might want to further shift those to the class (set once for all test cases) or to property files. In my company we're using different testing environments and we set those on the command line as property (also possible through build system and any IDE). The testing base class then reads a property file depending on that setting. The test cases can then freely read their parameters from that resprectively.

Hope this helps
Hubert

Keith Monsanto

non lue,
4 mars 2015, 21:07:5004/03/2015
à testng...@googlegroups.com
Hi Hubert,

Thank you for the response. I'm new to Selenium so excuse my ignorance on some practices.

I'm testing a web interface. I hardcode my locators, login details, expected messages, etc. in xml and then call them in dataprovider. Some of them are reused when passed to dataproviders. We don't have build system yet and all test are manual. I want to make my life easier :)

My 2nd question is, is there a way to pass a parameter to dataprovider function? I have 5 dataprovider for my 5 testcase and the code is the same, the difference is just on the string I passed. See example below.

    @DataProvider(name="blankuserblankpass")
    public static Object[][] blankuserblankpass(ITestContext context) {
        String uiString = ""
                + "url.landingpage,"
                + "url.login,"
                + "title.landingpage,"
                + "button.landingpage.login.locator,"
                + "textfield.login.username.locator,"
                + "text.login.username.blank,"
                + "textfield.login.password.locator,"
                + "text.login.password.blank,"
                + "button.login.login.locator,"
                + "url.login.fail,"
                + "text.loginfailed.failedmessage.blank";
        String[] uiObj = uiString.split(",");
        HashMap stringList = lib.genHashMap(uiObj, context);
        Object[][] result = new Object[][] {{ stringList }};
        return result;
    }

First option is to use condition using method name but the function would become too big.
2nd option is to hardcode the strings to a parameter name (but that would then increase my already big parameters in testng.xml) and use the first option to call it.

There is no wrong/right answer. Just let me know what you think. Many thanks for your suggestion.

Br,
Keith

Keith Monsanto

non lue,
4 mars 2015, 22:52:2304/03/2015
à testng...@googlegroups.com
Hi,

Would it be better if I use pagefactory instead? I think it's what I needed: a way to separate each objects into their own files. By doing so, my testng.xml is clean.

Hubert Grzeskowiak

non lue,
5 mars 2015, 12:37:4105/03/2015
à testng...@googlegroups.com
Hey Keith,

I'm not entirely sure what the UI objects strings mean, but you probably want to encapsulate them in a method or field, not in a Data Provider. Data Providers are used to test the same test case with slightly varying inputs, e.g. you could have 3 invalid user names in your testWrongLogin test method so that the same method would run three times.

Using page objects and page factories is always a good idea as it makes the abstractions of web pages reusable. When I'd write a test case that tests login I'd write something like this:

@Test(dataProvider = "wrongCredentials")
public void testWrongLogin(String user, String pass)
{
    LoginPage page = new LoginPage(driver).get();
    page.loginUsing(user, pass);
    assert page.wrongCredentialsErrorVisible()
}

@Test
public void testEmptyLogin()
{
    ...

All logic that has to do with typing in username, password, submitting and detecting errors or correct page or whatever is encapsulated in PageObject classes. You can read about it in the WebDriver/Selenium Docs.


Cheers
Hubert Grzeskowiak

Keith Monsanto

non lue,
5 mars 2015, 19:36:4905/03/2015
à testng...@googlegroups.com
Hi,

String uiString contains parameter names that is declared in my testng.xml that I want to use in a specific dataprovider. It is then split, stored in hashmap as key, and then it's parameter value in the hashmap value. This is then added in Object[][] so dataprovider will work. But scratch this as I've found a very easy way.

I'm now using pagefactory on my test and it simplified everything with regard to UI interaction. 
I'm no longer using parameter and dataprovider as I'm now using variable.class which contains all the variable.

I'll try to play around in this setup.

Many thanks for your help.

Br,
Keith
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message