精简版 Selenium PageFactory, Annotation 实例
精简版 Selenium PageFactory, Annotation 实例。
先是类: HomePage
package com.test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class HomePage {
@FindBy(id="kw")
public WebElement SearchTextField;
@FindBy(id="su")
public WebElement SearchButton;
public void clearSearchTextField() {
SearchTextField.clear();
}
public void inputSearchTextFeild(String str) {
SearchTextField.sendKeys(str);
}
public void clickSearchButton(){
SearchButton.click();
}
}
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class HomePage {
@FindBy(id="kw")
public WebElement SearchTextField;
@FindBy(id="su")
public WebElement SearchButton;
public void clearSearchTextField() {
SearchTextField.clear();
}
public void inputSearchTextFeild(String str) {
SearchTextField.sendKeys(str);
}
public void clickSearchButton(){
SearchButton.click();
}
}
再是定义的 Annotation 类,起名叫:AnnotationFactory。(其中的 RetentionPolicy.RUNTIME 很重要)
package com.test;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Documentedpublic @interface AnnotationFactory {
String batchName();
int testOrder() default 0;
String author() default "allen";
}
具体的 Test Case, 用 PageFactory 实例化 HomePage 类,同时也添加了 annotation
package com.test;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
@AnnotationFactory(batchName="Smoketest1", testOrder = 2, author = "Mike")
public class SearchBD {
private WebDriver driver = new FirefoxDriver();
private String baseUrl = "https://www.baidu.com/";
HomePage homePage = PageFactory.initElements(driver, HomePage.class);
@Test
public void testHelloWorld() throws Exception {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(baseUrl);
//Use page factory and its methods
homePage.clearSearchTextField();
homePage.inputSearchTextFeild("Hello world");
homePage.clickSearchButton();
driver.quit();
}
}
那么我们如何解析那些 annotation, 然后进一步根据 batchname, testOrder 分类执行呢?import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
@AnnotationFactory(batchName="Smoketest1", testOrder = 2, author = "Mike")
public class SearchBD {
private WebDriver driver = new FirefoxDriver();
private String baseUrl = "https://www.baidu.com/";
HomePage homePage = PageFactory.initElements(driver, HomePage.class);
@Test
public void testHelloWorld() throws Exception {
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(baseUrl);
//Use page factory and its methods
homePage.clearSearchTextField();
homePage.inputSearchTextFeild("Hello world");
homePage.clickSearchButton();
driver.quit();
}
}
这个类就是解析 annotation 的:
package com.test;
import java.lang.annotation.Annotation;
public class GetAnnotations {
public static void main(String[] args) throws ClassNotFoundException {
getAnnotationData("com.test.SearchBD");
}
private static void getAnnotationData(String caseName) throws ClassNotFoundException {
System.out.println("---start----");
Annotation[] annotations = Class.forName(caseName).getAnnotations();
for(Annotation annotation : annotations)
{
if(annotation instanceof AnnotationFactory)
{
AnnotationFactory af = (AnnotationFactory) annotation;
String batchName = af.batchName();
int testOrder = af.testOrder();
String author = af.author();
System.out.println(batchName + " " + testOrder + " " + author);
}
}
}
}
import java.lang.annotation.Annotation;
public class GetAnnotations {
public static void main(String[] args) throws ClassNotFoundException {
getAnnotationData("com.test.SearchBD");
}
private static void getAnnotationData(String caseName) throws ClassNotFoundException {
System.out.println("---start----");
Annotation[] annotations = Class.forName(caseName).getAnnotations();
for(Annotation annotation : annotations)
{
if(annotation instanceof AnnotationFactory)
{
AnnotationFactory af = (AnnotationFactory) annotation;
String batchName = af.batchName();
int testOrder = af.testOrder();
String author = af.author();
System.out.println(batchName + " " + testOrder + " " + author);
}
}
}
}