简体   繁体   English

硒:在超类中重写setUp方法并调用super超级方法

[英]selenium: override setUp method in super class and call super super method

I have a setUp() in my SuperTest class. 我的SuperTest类中有一个setUp()。 Now in my ChildTest class, I want to set up another setUp(). 现在在ChildTest类中,我想设置另一个setUp()。 This one will run specifically for ChildTest only. 这将仅针对ChildTest运行。

So I currently have this 所以我现在有这个

    //SuperClass
    protected void setUp(ITestContext context) {
        ...
    }

    //SubClass extends SuperClass
    @BeforeMethod
    protected void setUp(ITestContext context) {
        super.setUp(context);
        ...
    }

    //ChildClass extends SubClass
    @Override
    @BeforeMethod
    protected void setUp(ITestContext context) {
        super.setUp(context);
        ...
    }

The problem is, when I run ChildTest, it runs both of the setUp() from SubClass and it's own... How can I get it so it'll only run it's own setUp()? 问题是,当我运行ChildTest时,它同时运行了SubClass的两个setUp()并拥有它自己的...如何获得它,使其仅运行它自己的setUp()?

No it should not run both the setup methods, if you have extended the parent class and followed all the rules of overriding a method then there in no way your parent class method is going to get executed. 不,它不应该同时运行两个设置方法,如果您已经扩展了父类并遵循了覆盖方法的所有规则,那么您的父类方法就不会被执行。 Consider the following scenario. 请考虑以下情形。 I have two TestNG classes with Abc as the parent class and Def as the child class 我有两个TestNG类,其中Abc是父类,Def是子类

public class Abc{

@BeforeMethod
public void beforeMethod(ITestContext context) {
    System.out.println("PARENT");
}

} }

public class Def extends Abc{

@BeforeMethod
public void beforeMethod(ITestContext context) {
    System.out.println("CHILD");
}

@Test
public void test(){
    System.out.println("TEST");
}

} }

Now if I execute the Def class as TestNg Test, it is definitely going to execute beforeMethod of the child class and then after that the test method of the child class 现在,如果我将Def类作为TestNg Test执行,则肯定要执行子类的beforeMethod,然后执行子类的test方法

Reference to this answer: https://stackoverflow.com/a/3456599 引用此答案: https : //stackoverflow.com/a/3456599

I ended up adding a method in the SubClass that calls the setUp of SuperClass 我最终在子类中添加了一个调用SuperClass的setUp的方法。

    //SubClass extends SuperClass
    protected void getSuperSuper((ITestContext context) {
        super.setUp(context);
    }

    //ChildClass extends SubClass
    protected void setUp(ITestContext context) {
        super.getSuperSuper(context);
        ...
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM