Selenium-page-object-model
提供:Dev Guides
セレン-ページオブジェクトモデル
Seleniumは、ID、名前、XPathなどのプロパティの助けを借りてWeb要素に作用します。 組み込みのオブジェクトリポジトリ(OR)を持つQTPとは異なり、Seleniumには組み込みのORはありません。
したがって、必要に応じて保守可能でアクセス可能なORを構築する必要があります。 ページオブジェクトモデル(POM)は、クラスファイルを使用してこれらのwebelementsプロパティのそれぞれが作成されるオブジェクトリポジトリを作成する一般的なデザインパターンです。
利点
- POMは、テストオブジェクトと関数が互いに分離されているため、コードをクリーンに保つ実装です。
- オブジェクトは、テストスクリプトとは無関係に保持されます。 オブジェクトには1つ以上のテストスクリプトからアクセスできるため、POMはオブジェクトを1回作成して複数回使用するのに役立ちます。
- オブジェクトは一度作成されるため、オブジェクトの特定のプロパティに簡単にアクセスしたり更新したりできます。
POMフロー図
オブジェクトはページごとに作成され、メソッドはそれらのオブジェクトへのアクセス専用に開発されます。 同じことを理解するために、http://calculator.netを使用してみましょう。
それに関連付けられたさまざまな計算機があり、特定のページのそれらのオブジェクトのそれぞれは、静的メソッドとして個別のクラスファイルに作成され、それらはすべて、静的メソッドがオブジェクトにアクセスする「テスト」クラスファイルを通じてアクセスされます。
例
パーセント計算機テスト用のPOMを実装して、それを理解しましょう。
- ステップ1 *-パッケージ内に単純なクラス(page_objects_perc_calc.java)ファイルを作成し、以下に示すように、これらのオブジェクト識別子ごとにメソッドを作成します。
package PageObject;
import org.openqa.selenium.*;
public class PageObjectsPercCalc {
private static WebElement element = null;
//Math Calc Link
public static webElement lnk_math_calc(WebDriver driver) {
element = driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a"));
return element;
}
//Percentage Calc Link
public static webElement lnk_percent_calc(WebDriver driver) {
element = driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a"));
return element;
}
//Number 1 Text Box
public static webElement txt_num_1(WebDriver driver) {
element = driver.findElement(By.id("cpar1"));
return element;
}
//Number 2 Text Box
public static webElement txt_num_2(WebDriver driver) {
element = driver.findElement(By.id("cpar2"));
return element;
}
//Calculate Button
public static webElement btn_calc(WebDriver driver) {
element =
driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input"));
return element;
}
//Result
public static webElement web_result(WebDriver driver) {
element =
driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b"));
return element;
}
}
- ステップ2 *-mainでクラスを作成し、パッケージをインポートして、以下に示すように、それらのオブジェクト識別子のそれぞれに対してメソッドを作成します。
package PageObject;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PercentCalculator {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.calculator.net");
//Use page Object library now
page_objects_perc_calc.lnk_math_calc(driver).click();
page_objects_perc_calc.lnk_percent_calc(driver).click();
page_objects_perc_calc.txt_num_1(driver).clear();
page_objects_perc_calc.txt_num_1(driver).sendKeys("10");
page_objects_perc_calc.txt_num_2(driver).clear();
page_objects_perc_calc.txt_num_2(driver).sendKeys("50");
page_objects_perc_calc.btn_calc(driver).click();
String result = page_objects_perc_calc.web_result(driver).getText();
if(result.equals("5")) {
System.out.println(" The Result is Pass");
} else {
System.out.println(" The Result is Fail");
}
driver.close();
}
}
出力
テストが実行され、結果がコンソールに出力されます。 以下に同じもののスナップショットを示します。