子类 Web 元素在父类中使用 PageFactory.initElements 进行初始化。

huangapple 未分类评论48阅读模式
英文:

Child class Web-elements initialized in Parent class using PageFactory.initElements

问题

当我在父类中使用PageFactory.initElements时,子类的元素也会被初始化。这是如何工作的?

public class PageBase {

    public PageBase(WebDriver dr) {
        this.dr = dr;
        PageFactory.initElements(dr, this);
    }
}

在PageBase(父类)中,'this' 是对PageBase(父类)的引用,对吗?那么为什么它会初始化下面子类中的元素呢?这是因为继承吗(即子类也会用父类初始化的内容进行初始化)?

public class LoginPage extends PageBase {
    private WebDriver dr;
    
    public LoginPage(WebDriver dr) {
        super(dr);
        this.dr = dr;
        // PageFactory.initElements(dr, this);
    }
    
    @FindBy(how = How.ID, using = "Bugzilla_login")
    WebElement weUserName;
    
    @FindBy(id = "Bugzilla_password")
    WebElement wepassword;
    
    @FindBy(how = How.XPATH, using = "//input[@id='log_in']")
    WebElement weLoginBtn;
}
英文:

When I use PageFactory.initElements in parent class, child class elements are initialized. How this is working?

public class PageBase {

	public PageBase(WebDriver dr)
	{
		this.dr=dr;
		PageFactory.initElements(dr, this);
	}
}

Here in PageBase(parent), 'this' is reference of PageBase(parent) right? then how its initializing elements in below child class? is this because of inheritance[i.e something like child class also will be initialized with parent]?

public class LoginPage extends PageBase{
    private WebDriver dr;
	public LoginPage(WebDriver dr)
	{
		super(dr);
		this.dr=dr;
		//PageFactory.initElements(dr, this);
	}
	@FindBy(how=How.ID,using="Bugzilla_login")
	WebElement weUserName;
	@FindBy(id="Bugzilla_password")
	WebElement wepassword;
	@FindBy(how=How.XPATH,using="//input[@id='log_in']")
	WebElement weLoginBtn;
} 

答案1

得分: 0

this 是对正在创建的 PageBase 子实例的引用;this 总是指向具体的运行时类。

我们可以通过一个简单的示例来证明。

public class Main {
    public static void main(String... args) {
        Parent aParent = new Child("foo");
    }

    static class Parent {
        Parent(String someArg) {
            Factory.initElements(someArg, this);
        }
    }

    static class Factory {
        static void initElements(String someArg, Parent p) {
            System.out.println("Parent 对象 'p' 是类的实例:" + p.getClass().getSimpleName());
        }
    }

    static class Child extends Parent {
        Child(String someArg) {
            super(someArg);
        }
    }
}

Parent 对象 'p' 是类的实例:Child

请注意,根据您的 IDE 设置,您可能会看到 Leaking this in constructor warning(构造函数中的 this 泄漏警告)。

英文:

this is a reference to the PageBase child instance being created; this always refers to the concrete runtime class.

We can prove it with a simple example.

public class Main {
    public static void main(String... args) {
        Parent aParent = new Child("foo");
    }

    static class Parent {
        Parent(String someArg) {
            Factory.initElements(someArg, this);
        }
    }

    static class Factory {
        static void initElements(String someArg, Parent p) {
            System.out.println( "Parent object 'p' is an instance of class: " + p.getClass().getSimpleName() );
        }
    }

    static class Child extends Parent {
        Child(String someArg) {
            super(someArg);
        }
    }
}

> Parent object 'p' is an instance of class: Child

Note that depending on your IDE settings, you may see a Leaking this in constructor warning.

huangapple
  • 本文由 发表于 2020年4月10日 21:50:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61141702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定