抽象化と再利用のために、Webページ上の要素をクラスとしてモデル化する理想的な方法は何でしょうか。
現在、私が作成したのは、次のメンバー変数を含むBaseElement.javaクラスです。
WebDriver webdriver;
コンストラクター:
protected BaseElement(WebDriver driver)
{
//Initialization.
}
次に、このクラスを、Webページ上の特定の要素を表す他のクラスに拡張します。
たとえば、BaseElement.javaを拡張するButton.javaクラスがあります。
public class ButtonElement extends BaseElement {
String locator;
By by;
WebElement button;
protected ButtonElement(WebDriver driver, String locator, By by) {
super(driver);
this.by = by;
this.locator = locator;
//code to find the element using driver.findElement()
// I am not sure how to use By in here depending upon what I pass in the constructor when I create an object of this class. Any suggestions?
}
public void click() {
button.click();
}
//Other methods which will interact with the element in other ways.
}
したがって、私のPageObjectクラスでは、次のように上記を利用します。
Button loginButton = new Button(driver,"btn-Login",By.id);
loginButton.click();
これは正しいアプローチですか?Webドライバーの観点から、抽象化し、複雑なロジックを単一のクラスで非表示にし、ボタン、テキストボックス、選択リストなどの要素を再利用するためのより良い方法はありますか?前もって感謝します!