精简版 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();
    }
}

再是定义的 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)

@Target(value = {ElementType.TYPE, ElementType.METHOD})

@Documented
public @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 分类执行呢?

 这个类就是解析 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);
            }
        }
    }
}

精简版 Selenium PageFactory, Annotation 实例的更多相关文章

  1. Vue精简版风格指南

    前面的话 Vue官网的风格指南按照优先级(依次为必要.强烈推荐.推荐.谨慎使用)分类,且代码间隔较大,不易查询.本文按照类型分类,并对部分示例或解释进行缩减,是Vue风格指南的精简版 组件名称 [组件 ...

  2. NETCore Bootstrap Admin 通用后台管理权限 [3]: 精简版任务调度模块

    前言 NETCore 里说到任务调度,大家首先想到的应该是大名鼎鼎的 QuartzNET 与 Hangfire,然而本篇介绍的却都不是,而是 Bootstrap Admin(以下简称 BA)通用后台权 ...

  3. Linux上oracle精简版客户端快速部署

    RHEL6 + Oracle 11g客户端快速部署 需求:只是用到客户端的sqlplus, sqlldr功能. 方案:用精简版实现客户端的快速部署 1.上传oracle精简版客户端到服务器/tmp目录 ...

  4. ArcGIS10.2.1精简版、ArcGIS_Desktop10_Tutorial、破解文件等下载地址

    原版ArcGIS for Desktop的ISO文件一般都在4.5G以上,一般人用不上里面很多工具,下载回来又浪费时间,现推出ArcGIS10.2.1精简版(里面只包含主程序.Data Interop ...

  5. TeamViewer12.0.71503(远程控制软件)精简版 单文件企业版介绍

    TeamViewer 是一款能在任何防火墙和 NAT 代理的后台用于远程控制,桌面共享和文件传输的简单且快速的解决方案.为了连接到另一台计算机,只需要在两台计算机上同时运行 TeamViewer 即可 ...

  6. Log4j快速使用精简版

    Log4j快速使用精简版 1.导入log4j-1.2.17.jar包 2.在src根目录下创建log4j.properties文件 log4j.rootLogger=INFO, CONSOLE, FI ...

  7. VMware10.06精简版安装后台运行

    VMware10.06精简版安装时会出现一个安装功能选择菜单,里面有一条后台运行必选功能,一般人会跳过条.当你打算在服务器上用vmware时,一定要安装后台运行服务,否则你无法换出正在运行的后台虚拟机 ...

  8. [异常解决] ubuntu上安装虚拟机遇到的问题(vmware坑了,virtual-box简单安装,在virtual-box中安装精简版win7)

    利用周末时间将整个电脑格式化,换成了ubuntu系统- 所谓:扫清屋子再请客! 但是有些软件只在win上有,于是还是考虑装个虚拟机来个——逐步过度策略,一点点地从win上转移到linux上 我的系统是 ...

  9. 电脑公司最新GHOST WIN7系统32,64位优化精简版下载

    系统来自系统妈:http://www.xitongma.com 电脑公司最新GHOST win7系统32位优化精简版V2016年3月 系统概述 电脑公司ghost win7 x86(32位)万能装机版 ...

随机推荐

  1. 抗D十招:十个方法完美解决DDoS攻击防御难题

    可以说,DDoS是目前最凶猛.最难防御的网络攻击之一.现实情况是,这个世界级难题还没有完美的.彻底的解决办法,但采取适当的措施以降低攻击带来的影响.减少损失是十分必要的.将DDoS防御作为整体安全策略 ...

  2. pg数据库data文件夹达到32G

    最近工作中遇到pg数据库data文件夹大小达到32G,大致的排查流程如下: 1.首先查看是哪个文件或者那些文件比较大: 2.根据文件的名称找到对应的数据表a: 3.确认数据表a中数据实际上只有3000 ...

  3. mysoft

    @@a8649fbb56349908b5ca6708fb94b3ddabcf6b97381a9797d3dfb139b8749287117@@##74e02e1207e5a0a8996ba89f1d6 ...

  4. 韦东山嵌入式Linux学习笔记02--如何给开发板烧录程序

    购买韦东山嵌入式开发板jz2440 v3会标配两根usb线和一根网线,OpenJtag需要单独购买, 我暂时还没买到该工具. 下面介绍usb烧录以及通过网线烧录程序. 1.usb烧录程序: 借助DNW ...

  5. jQuery获取当前按钮的id

    function test(e) { $(e.target).attr('id') } // 记得带上点击事件

  6. CAS JDK 证书错误学习笔记

    通过之前生产上发现的问题总结得出以下结论:  问题现象就是:由F5 进行分发到cas 两个服务端 导致 客户端访问时  (时好时坏的现象 ) 通过在服务端的查看apahce 的访问日志得出的结论  发 ...

  7. kotlin面向对象之枚举、印章类

    枚举: 由于这个比较简单,直接上代码: 下面使用一下: 印章类[Sealed class]: 听着挺新鲜的,下面以一个具体的场景来对它进行学习: 动物园里有三个动物如下,在天黑时它们污污的在做“游戏” ...

  8. Python 实用爬虫-04-使用 BeautifulSoup 去水印下载 CSDN 博客图片

    Python 实用爬虫-04-使用 BeautifulSoup 去水印下载 CSDN 博客图片 其实没太大用,就是方便一些,因为现在各个平台之间的图片都不能共享,比如说在 CSDN 不能用简书的图片, ...

  9. fedora 29 安装ALSA声音驱动

    centos系列解决 安装utils时遇到的问题 configure: error: this packages requires a curses library yum install ncurs ...

  10. 一例基于thinkphp,jquery和bootstrap渲染的查询数据分页器

    对于某些查询记录很多的结果,web页面不得不采用分页器,现在奉上一例代码,其主要逻辑是:由页面的dom 节点发起ajax请求,返回的查询结果根据页面布局需要进行切片:并根据总记录数和页面展现的条数算出 ...