LiteのセレンPageFactory、注釈インスタンス。
ファーストクラス:ホームページ
パッケージcom.test。
輸入org.openqa.selenium.WebElement。
輸入org.openqa.selenium.support.FindBy;
パブリッククラスホームページ{
@FindBy(ID = "KW")
公衆WebElement SearchTextField。
@FindBy(ID = "SU")
公衆WebElement検索]。
公共ボイドclearSearchTextField(){
SearchTextField.clear()。
}
公共ボイドinputSearchTextFeild(文字列str){
SearchTextField.sendKeys(STR)。
}
公共ボイドclickSearchButton(){
SearchButton.click()。
}
}
輸入org.openqa.selenium.WebElement。
輸入org.openqa.selenium.support.FindBy;
パブリッククラスホームページ{
@FindBy(ID = "KW")
公衆WebElement SearchTextField。
@FindBy(ID = "SU")
公衆WebElement検索]。
公共ボイドclearSearchTextField(){
SearchTextField.clear()。
}
公共ボイドinputSearchTextFeild(文字列str){
SearchTextField.sendKeys(STR)。
}
公共ボイドclickSearchButton(){
SearchButton.click()。
}
}
注釈は、別のクラスの定義である、と呼ばれる:AnnotationFactory。(非常に重要であるRetentionPolicy.RUNTIME)
パッケージcom.test。
輸入java.lang.annotation.Documented;
輸入java.lang.annotation.Inherited;
輸入java.lang.annotation.Retention。
輸入java.lang.annotation.RetentionPolicy。
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Documented
公衆@interfaceのAnnotationFactory {
ストリングbatchName()。
testOrder()デフォルト0 int型。
文字列の作者()デフォルトの「アレン」。
}
輸入java.lang.annotation.Documented;
輸入java.lang.annotation.Inherited;
輸入java.lang.annotation.Retention。
輸入java.lang.annotation.RetentionPolicy。
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Documented
公衆@interfaceのAnnotationFactory {
ストリングbatchName()。
testOrder()デフォルト0 int型。
文字列の作者()デフォルトの「アレン」。
}
具体的にテストケース、注釈を追加PageFactoryホームページクラスのインスタンス化と、一方で
パッケージcom.test。
輸入java.util.concurrent.TimeUnit。
輸入org.junit.Test;
輸入org.openqa.selenium.WebDriver;
輸入org.openqa.selenium.firefox.FirefoxDriver;
輸入org.openqa.selenium.support.PageFactory;
@AnnotationFactory(batchName = "Smoketest1"、testOrder = 2、作成者= "マイク")
パブリッククラスSearchBD {
プライベートwebdriverをドライバ=新しいFirefoxDriver()。
プライベート文字列baseUrlに=「https://www.baidu.com/」。
ホームページホームページ= PageFactory.initElements(ドライバ、HomePage.class)。
@Test
公共ボイドtestHelloWorld()例外{スロー
driver.manageを()タイムアウト()implicitlyWait(30、TimeUnit.SECONDS)。
。driver.manage()ウィンドウ()(最大化)。
driver.get(baseUrlに)。
//使用ページファクトリとそのメソッド
homePage.clearSearchTextField();
homePage.inputSearchTextFeild( "こんにちは、世界");
homePage.clickSearchButton();
driver.quit();
}
}
だから、どのように我々は、これらの注釈を解決し、その後さらにbatchname、testOrder分類に従って、それを強制するのですか?
輸入java.util.concurrent.TimeUnit。
輸入org.junit.Test;
輸入org.openqa.selenium.WebDriver;
輸入org.openqa.selenium.firefox.FirefoxDriver;
輸入org.openqa.selenium.support.PageFactory;
@AnnotationFactory(batchName = "Smoketest1"、testOrder = 2、作成者= "マイク")
パブリッククラスSearchBD {
プライベートwebdriverをドライバ=新しいFirefoxDriver()。
プライベート文字列baseUrlに=「https://www.baidu.com/」。
ホームページホームページ= PageFactory.initElements(ドライバ、HomePage.class)。
@Test
公共ボイドtestHelloWorld()例外{スロー
driver.manageを()タイムアウト()implicitlyWait(30、TimeUnit.SECONDS)。
。driver.manage()ウィンドウ()(最大化)。
driver.get(baseUrlに)。
//使用ページファクトリとそのメソッド
homePage.clearSearchTextField();
homePage.inputSearchTextFeild( "こんにちは、世界");
homePage.clickSearchButton();
driver.quit();
}
}
このクラスは、アノテーションを解析することです:
パッケージcom.test。
輸入java.lang.annotation.Annotation。
パブリッククラスGetAnnotations {
公共の静的な無効メイン(文字列[]引数)はClassNotFoundExceptionが{スロー
getAnnotationData( "com.test.SearchBDを");
}
プライベート静的ボイドgetAnnotationData(文字列caseName)はClassNotFoundExceptionが{スロー
するSystem.out.printlnを( "---開始----")。
注釈[]注釈= Class.forNameの(caseName).getAnnotations();
(注釈注釈注釈)用
{
IF(注釈のinstanceof AnnotationFactory)
{
AnnotationFactory AF =(AnnotationFactory)注釈。
ストリングbatchName = af.batchName()。
INT testOrder = af.testOrder()。
ストリングの作成者= af.author()。
System.out.println(batchName + "" + testOrder + "" +作者)。
}
}
}
}
輸入java.lang.annotation.Annotation。
パブリッククラスGetAnnotations {
公共の静的な無効メイン(文字列[]引数)はClassNotFoundExceptionが{スロー
getAnnotationData( "com.test.SearchBDを");
}
プライベート静的ボイドgetAnnotationData(文字列caseName)はClassNotFoundExceptionが{スロー
するSystem.out.printlnを( "---開始----")。
注釈[]注釈= Class.forNameの(caseName).getAnnotations();
(注釈注釈注釈)用
{
IF(注釈のinstanceof AnnotationFactory)
{
AnnotationFactory AF =(AnnotationFactory)注釈。
ストリングbatchName = af.batchName()。
INT testOrder = af.testOrder()。
ストリングの作成者= af.author()。
System.out.println(batchName + "" + testOrder + "" +作者)。
}
}
}
}