automation(一)

选择浏览器

 1 public class selectDriver {
 2     public WebDriver driverName(String browser) {
 3         if (browser.equalsIgnoreCase("firefox")) {
 4             System.setProperty("webdrider.firefox.marioette", "H:\\geckodriver.exe");
 5             return new FirefoxDriver();
 6         } else {
 7             System.setProperty("webdriver.chrome.driver", "H:\\chromedriver.exe");
 8             return new ChromeDriver();
 9         }
10     }
11 }
 1 public class driverBase {
 2     public WebDriver driver;
 3 
 4     public driverBase(String browser) {
 5         selectDriver sd = new selectDriver();
 6         this.driver = sd.driverName(browser);
 7     }
 8 
 9     public void stop() {
10         System.out.print("停止 browser");
11         driver.close();
12     }
13 }

BaseDriver

 1 public class BaseDriver {
 2     public WebDriver driver;
 3 
 4     public BaseDriver() {
 5         System.setProperty("webdriver.chrome.driver",
 6                 "C:\\Users\\cheng\\AppData\\Local\\Google\\Chrome\\Application\\chromedriver.exe");
 7         driver = new ChromeDriver();
 8     }
 9 
10     // 封装element
11     public WebElement Element(By by) {
12         WebElement el = driver.findElement(by);
13         return el;
14     }
15 
16     // 点击元素
17     public void click(WebElement element) {
18         if (element != null) {
19             element.click();
20         } else {
21             System.out.println(element + "元素不存在,点击失败");
22         }
23 
24     }
25 
26     // 输入
27     public void sendkeys(WebElement element, String value) {
28         if (element != null) {
29             element.sendKeys(value);
30         } else {
31             System.out.println(element + "元素不存在,输入失败");
32         }
33     }
34 
35     // 判断元素是否可见
36     public boolean elementIsShow(WebElement element) {
37         return element.isDisplayed();
38     }
39 
40     // 读取配置获取选择的元素
41     public By getSelect(String key) {
42         properties prop = new properties("element.properties");
43         String locator = prop.getPro(key);
44         String locatorType = locator.split(">")[0];
45         String locatoeValue = locator.split(">")[1];
46         if (locatorType.equals("id")) {
47             return By.id(locatoeValue);
48         } else if (locatorType.equals("name")) {
49             return By.name(locatoeValue);
50         } else if (locatorType.equals("classname")) {
51             return By.className(locatoeValue);
52         } else {
53             return By.xpath(locatoeValue);
54         }
55     }
56 }

 

读取配置

 1 public class properties {
 2     private Properties prop;
 3     private String filepath;
 4 
 5     // 获取路径
 6     public properties(String path) {
 7         this.filepath = path;
 8         try {
 9             this.prop = readProperties();
10         } catch (Exception e) {
11             e.printStackTrace();
12         }
13     }
14 
15     // 读取配置
16     public Properties readProperties() throws Exception {
17         prop = new Properties();
18         try {
19             FileInputStream path = new FileInputStream(filepath);
20             BufferedInputStream bi = new BufferedInputStream(path);
21             prop.load(bi);
22         } catch (Exception e) {
23             e.printStackTrace();
24         }
25         return prop;
26     }
27 
28     //获取key
29     public String getPro(String key) {
30         if (prop.containsKey(key)) {
31             String username = prop.getProperty(key);
32             return username;
33         } else {
34             System.out.print("获取的key不对");
35             return "";
36         }
37 
38     }
39 }

 Login

 1 public class Login extends BaseDriver {
 2     public Login() {
 3         driver.get("http://www.baidu.com");
 4         WebElement word = Element(getSelect("word"));
 5         word.sendKeys("ronle");
 6         WebElement btn = Element(getSelect("baidu"));
 7         btn.click();
 8         System.out.println("搜索完成 ");
 9     }
10 }

元素方法

 1 /*
 2      * 获取元素
 3      */
 4     public WebElement element(By by) {
 5         WebElement el = driver.findTheDriver(by);
 6         return el;
 7     }
 8     /*
 9      * 点击
10      */
11 
12     public void click(WebElement element) {
13         if (element != null) {
14             element.click();
15         } else {
16             System.out.println(element + "元素不存在,点击失败");
17         }
18 
19     }
20     /*
21      * 输入
22      */
23 
24     public void sendkeys(WebElement element, String value) {
25         if (element != null) {
26             element.sendKeys(value);
27         } else {
28             System.out.println(element + "元素不存在,输入失败");
29         }
30     }
31     /*
32      * 判断元素是否可见
33      */
34 
35     public boolean elementIsShow(WebElement element) {
36         return element.isDisplayed();
37     }

循环点击页面元素

 1 public class forList {
 2 
 3     public WebDriver driver;
 4 
 5     @Test
 6     public void testList() {
 7         System.setProperty("webdriver.chrome.driver", "H:\\chromedriver.exe");
 8         driver = new ChromeDriver();
 9     }
10 
11     @Test(dependsOnMethods = { "testList" })
12     public void courseList() {
13         driver.get("http://bbs.3dmgame.com/forum.php");
14         driver.manage().window().maximize();
15         List<String> listString = this.listelement();
16         for (int i = 0; i < listString.size(); i++) {
17             driver.findElement(By.xpath("//*[contains(text(),'" + listString.get(i) + "')]")).click();
18             driver.navigate().back();
19         }
20     }
21 
22     /*
23      * 获取所有的列表
24      */
25     public List<String> listelement() {
26         List<String> listString = new ArrayList<String>();
27         WebElement element = driver.findElement(By.className("flg"));
28         List<WebElement> list = element.findElements(By.tagName("dt"));
29         for (WebElement el : list) {
30             System.out.println(el.findElement(By.tagName("a")).getText());
31             listString.add(el.findElement(By.tagName("a")).getText());
32         }
33         return listString;
34     }
35 }

 设置和获取cookie

 1 public class handleCookie {
 2     public static WebDriver driver;
 3     public static properties pro;
 4 
 5     public void initCookie() throws InterruptedException {
 6         System.setProperty("webdriver.chrome.driver", "H:\\chromedriver.exe");
 7         driver = new ChromeDriver();
 8 
 9         pro = new properties("element.properties");
10         String value = pro.getPro("apsid");
11         Cookie ck = new Cookie("apsid", value, ".imooc.com", "/", null, true);
12         // Cookie ck = new Cookie("apsid", value);
13         driver.get("https://www.imooc.com/learn/1021");
14         // 增加cookie
15         setCookie(ck);
16         driver.get("https://www.imooc.com/learn/1021");
17         // 获取cookie
18         String ckname = driver.manage().getCookieNamed("apsid").getValue();
19         System.out.println(ckname);
20     }
21 
22     public static void setCookie(Cookie cookie) {
23         driver.manage().addCookie(cookie);
24     }
25 
26     public static void main(String[] args) throws InterruptedException {
27         handleCookie hc = new handleCookie();
28         hc.initCookie();
29     }
30 }

 发送邮件

 1 public class sendEmail {
 2 
 3     public static void main(String[] args) {
 4         sendEmail se = new sendEmail();
 5         se.sendTheEmail();
 6     }
 7 
 8     public void sendTheEmail() {
 9         SimpleEmail email = new SimpleEmail();
10         email.setHostName("smtp.163.com");// 设置使用发电子邮件的邮件服务器,这里以qq邮箱为例(其它例如:【smtp.163.com】,【smtp.sohu.com】)
11         email.setSmtpPort(25);
12         // 邮箱服务器身份验证 这里使用授权码,而不是邮箱原来的密码
13         email.setAuthentication("***@163.com", "***");
14         try {
15             // 发件人邮箱
16             email.setFrom("***@163.com");
17             // 收件人邮箱
18             email.addTo("***@qq.com");
19             // 邮件主题
20             email.setSubject("这是测试邮箱");
21             // 邮件内容
22             email.setContent("<h3>测试邮箱是否可以发送</h3>", "text/html;charset=utf-8");
23             // 发送邮件
24             email.send();
25             System.out.println("发送完毕");
26         } catch (EmailException ex) {
27             ex.printStackTrace();
28         }
29 
30     }
31 }

 断言

1 public class AssertDemo  {
2 
3     public static void main(String[] args) {
4         //String s = null;
5         Assert.assertEquals("1", "2", "2个值不一样");
6         //Assert.assertNotNull(s, "数据为空");
7     }
8 }

JSON解析

  1 /**
  2  * User测试类
  3  */
  4 class User {
  5     private String username;
  6     private String password;
  7 
  8     public User() {
  9     }
 10 
 11     public User(String username, String password) {
 12         this.username = username;
 13         this.password = password;
 14     }
 15 
 16     public String getUsername() {
 17         return username;
 18     }
 19 
 20     public void setUsername(String username) {
 21         this.username = username;
 22     }
 23 
 24     public String getPassword() {
 25         return password;
 26     }
 27 
 28     public void setPassword(String password) {
 29         this.password = password;
 30     }
 31 
 32     @Override
 33     public String toString() {
 34         return "User [username=" + username + ", password=" + password + "]";
 35     }
 36 }
 37 
 38 /**
 39  * 用户组测试类
 40  */
 41 class UserGroup {
 42     private String name;
 43     private List<User> users = new ArrayList<User>();
 44 
 45     public UserGroup() {
 46     }
 47 
 48     public UserGroup(String name, List<User> users) {
 49         this.name = name;
 50         this.users = users;
 51     }
 52 
 53     public String getName() {
 54         return name;
 55     }
 56 
 57     public void setName(String name) {
 58         this.name = name;
 59     }
 60 
 61     public List<User> getUsers() {
 62         return users;
 63     }
 64 
 65     public void setUsers(List<User> users) {
 66         this.users = users;
 67     }
 68 
 69     @Override
 70     public String toString() {
 71         return "UserGroup [name=" + name + ", users=" + users + "]";
 72     }
 73 }
 74 
 75 /**
 76  * java对象转 json字符串
 77  */
 78 class TestFastJosn {
 79 
 80     @Test
 81     public void objectTOJson() {
 82         // 简单java类转json字符串
 83         User user = new User("dmego", "123456");
 84         String UserJson = JSON.toJSONString(user);
 85         System.out.println("简单java类转json字符串:" + UserJson);
 86 
 87         // List<Object>转json字符串
 88         User user1 = new User("zhangsan", "123123");
 89         User user2 = new User("lisi", "321321");
 90         List<User> users = new ArrayList<User>();
 91         users.add(user1);
 92         users.add(user2);
 93         String ListUserJson = JSON.toJSONString(users);
 94         System.out.println("List<Object>转json字符串:" + ListUserJson);
 95 
 96         // 复杂java类转json字符串
 97         UserGroup userGroup = new UserGroup("userGroup", users);
 98         String userGroupJson = JSON.toJSONString(userGroup);
 99         System.out.println("复杂java类转json字符串:" + userGroupJson);
100 
101     }
102 
103     /**
104      * json字符串转java对象 注:字符串中使用双引号需要转义 (" --> \"),这里使用的是单引号
105      */
106     @Test
107     public void JsonToObject() {
108         /*
109          * json字符串转简单java对象 字符串:{"password":"123456","username":"dmego"}
110          */
111 
112         String jsonStr1 = "{'password':'123456','username':'dmego'}";
113         User user = JSON.parseObject(jsonStr1, User.class);
114         System.out.println("json字符串转简单java对象:" + user.toString());
115 
116         /*
117          * json字符串转List<Object>对象
118          * 字符串:[{"password":"123123","username":"zhangsan"},{"password":"321321",
119          * "username":"lisi"}]
120          */
121         String jsonStr2 = "[{'password':'123123','username':'zhangsan'},{'password':'321321','username':'lisi'}]";
122         List<User> users = JSON.parseArray(jsonStr2, User.class);
123         System.out.println("json字符串转List<Object>对象:" + users.toString());
124 
125         /*
126          * json字符串转复杂java对象
127          * 字符串:{"name":"userGroup","users":[{"password":"123123","username":"zhangsan"},
128          * {"password":"321321","username":"lisi"}]}
129          */
130         String jsonStr3 = "{'name':'userGroup','users':[{'password':'123123','username':'zhangsan'},{'password':'321321','username':'lisi'}]}";
131         UserGroup userGroup = JSON.parseObject(jsonStr3, UserGroup.class);
132         System.out.println("json字符串转复杂java对象:" + userGroup);
133     }
134 }

 

转载于:https://www.cnblogs.com/ronle/p/9844177.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值