0

I'm implementing selenium tests. In the scenario, there are same tests in different pages. So I tried to use 'DataProvider'. It works, but I think there could be a better way.

I need to run 'mostView' and 'mostMemo' method in a page. And do the same thing in other pages.

enter image description here

Easy way is just merge them in a method. But I need to add more tests in that way. So I'd prefer to separate them. Any suggestions appreciate.

1

You can solve this by switching from data provider for a hierarchy of test.

public abstract class BaseCommonPageTest {
   @Test
   public void mostMemo(){
    BasePage page = this.getPage();
    String name = this.getName();
    ...
   }

    @Test
    public void mostView(){
    ...
    }

    ...

    protected abstract BasePage getPage();
    protected abstract String getName();
}


public class MNewsPoliticsPageTest extends BaseCommonPageTest {

    @Override
    protected String getPage() {
        return new MNewsPoliticsPage();
    }

    @Override
    protected String getName() {
        return "...";
    }

}

public class OtherPageTest extends BaseCommonPageTest {

    @Override
    protected String getPage() {
        return new OtherPage();
    }

    @Override
    protected String getName() {
        return "sdasdadw";
    }

}

If you want to test a new page, you just need to create a new class and define the page object and name.

If you want to add a new test to all pages, you just need to define it on the base class.

With time, you will probably discover that some tests do not fit on some pages. When this happens, you can break the base class, following the Interface Segregation Principle.

  • I’ll try this way – junghan Jul 17 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.