0.1---selenium+java自动化测试进阶02----项目实战之登录代码重构

一、测试登录功能实现

     以慕课网的登录为例,分析登录的功能需求,编写测试用例,找到要定位的元素以及需要的操作,编写登录功能的测试代码。代码实现如下:

 1 public static void main(String[] args) throws InterruptedException {   
 2         System.setProperty("webdriver.gecko.driver","c:\Program Files\Mozilla Firefox\geckodriver.exe");
 3         WebDriver driver=new FirefoxDriver();
 4         driver.get("https://www.imooc.com");
 5         driver.manage().window().setSize(new Dimension(1456,876));//设置窗口的尺寸
 6         driver.findElement(By.id("js-signin-btn")).click();//点击登录按钮
 7         Thread.sleep(2000);

10 driver.findElement(By.name("email")).sendKeys("*********"); 12 driver.findElement(By.name("password")).sendKeys("******"); 13 14 driver.findElement(By.cssSelector(".moco-btn")).click(); 15 Thread.sleep(2000); 16 17 Actions a=new Actions(driver);//鼠标移动,perform()执行所有Actions中存储的行为,可以理解成对整个操作事件的提交动作。 18 a.moveToElement(driver.findElement(By.cssSelector("#header-avator>img"))).perform(); 19 20 String s=driver.findElement(By.className("name")).getText();//获取标签属性 21 String t=driver.getTitle(); 22 String v=driver.getCurrentUrl(); 23 System.out.println(s+t+v); 24 assertEquals(s,"搁浅的青木"); //验证失败时该测试将停止。vertify:失败时,该测试将继续执行,并将错误记入日显示屏。Waitfor用于等待某些条件变为真。可用于AJAX应用程序的测试。 25 if(s.equals("搁浅的青木")) { 26 System.out.println("登陆成功,请退出"); 27 driver.findElement(By.linkText("安全退出")).click(); 28 }else { 29 System.out.println("登录失败"); 30 } 31 driver.quit(); 32 }

二、代码重构之封装findElement()方法

    1.提取参数,包括:输入信息、定位属性、定位属性值

     2.封装findElement()方法

 1 public class moocloginOne {
 2     public WebDriver driver;
 3     public void initDriver() throws InterruptedException {
 4         System.setProperty("webdriver.gecko.driver","c:\Program Files\Mozilla Firefox\geckodriver.exe");
 5         driver=new FirefoxDriver();
 6         driver.get("https://www.imooc.com");
 7         driver.manage().window().setSize(new Dimension(1456,876));//设置窗口的尺寸
 8         Thread.sleep(3000);
 9         
10         driver.findElement(By.id("js-signin-btn")).click();
11     }
12     
13     public void loginScript() throws InterruptedException {
14         this.initDriver();
15         Thread.sleep(3000);
16         //1.提取定位参数,提高代码可用性
17         String userBy="name";
18         String userLocal="email";
19         String passowrdBy="name";
20         String PasswordLocal="password";
21         String loginbtnBy="cssSelector";
22         String loginbtnLocal=".moco-btn";
23         String userInfo="17839196010";
24         String passwordInfo="duhui619";
25         String userImgBy="cssSelector";
26         String userImgLocal="#header-avator>img";
27         String usernameBy="className";
28         String usernameLocal="name";
29         String usernameInfo="搁浅的青木";    
30         WebElement user=element(userBy,userLocal);
31         user.isDisplayed();
32         WebElement password=element(passowrdBy,PasswordLocal);
33         password.isDisplayed();
34         WebElement button=element(loginbtnBy,loginbtnLocal);
35         button.isDisplayed();        
36         user.sendKeys(userInfo);
37         password.sendKeys(passwordInfo);
38         button.click();
39         Thread.sleep(2000);
40         WebElement header=element(userImgBy,userImgLocal);
41         header.isDisplayed();
42         Actions a=new Actions(driver);
43         a.moveToElement(header).perform();
44         String s=element(usernameBy,usernameLocal).getText();
45         if(s.equals(usernameInfo)) {
46             System.out.println("登陆成功,请退出");
47         }else {
48             System.out.println("登录失败");
49         }
50         driver.quit();        
51     }
52     
53     //2.封装driver.findElement( By.id(local))
54      public WebElement element(String by,String local) {
55          WebElement element=null;
56          if(by.equals("id")) {
57             element=driver.findElement( By.id(local));
58          }else if(by.equals("name")) {
59             element=driver.findElement( By.name(local));
60          }else if(by.equals("className")) {
61             element=driver.findElement( By.className(local));
62          }else if(by.equals("linkText")) {
63             element=driver.findElement( By.linkText(local));
64          }else if(by.equals("xpath")) {
65             element=driver.findElement( By.xpath(local));
66          }else if(by.equals("cssSelector")) {
67             element=driver.findElement( By.cssSelector(local));
68          }else if(by.equals("tageName")){
69             element=driver.findElement( By.tagName(local));
70          }else {
71             element=driver.findElement( By.partialLinkText(local));
72          }
73          return element;
74      }    
75 
76     public static void main(String[] args) throws InterruptedException {
77         moocloginOne login=new   moocloginOne();
78         Thread.sleep(2000);
79         login.loginScript();
80     }

三、代码重构之封装读取配置文件的方法

   1.拆分findElement方法为by方法和element方法

   2.修改byStr函数中的代码,只返回by的方式 。
   3.将变量放到配置文件中,并编写读取文件类及方法-----ProUtil类。
   4.编写读取配置文件函数-----调用ProUtil类,读取配置文件。
   5.在byStr函数中调用读取文件函数--getproperties(),并修改byStr传入参数。

主函数:

 1 public class moocloginTwo {
 2      public WebDriver driver;
 3         public void initDriver() throws InterruptedException {
 4             System.setProperty("webdriver.gecko.driver","c:\Program Files\Mozilla Firefox\geckodriver.exe");
 5             driver=new FirefoxDriver();
 6             driver.get(this.getproperties("Url"));
 7             driver.manage().window().setSize(new Dimension(1456,876));//设置窗口的尺寸
 8             Thread.sleep(3000);
 9             element(this.byStr("closeLocal")).click();    
10             element(this.byStr("loginLocal")).click();
11         }
12         
13         public void loginScript() throws InterruptedException {
14             this.initDriver();
15             Thread.sleep(3000);        
16            //3.将变量放到配置文件中,并编写读取文件类及方法-----ProUtil类
17             WebElement user=this.element(this.byStr("userLocal"));
18             user.isDisplayed();
19         
20             WebElement password=this.element(this.byStr("PasswordLocal"));
21             password.isDisplayed();
22     
23             WebElement button=this.element(this.byStr("loginbtnLocal"));
24             button.isDisplayed();
25             user.sendKeys(this.getproperties("userInfo"));
26             password.sendKeys(this.getproperties("passwordInfo"));
27             button.click();
28             Thread.sleep(3000);
29             
30             WebElement header=this.element(this.byStr("userImgLocal"));
31             header.isDisplayed();
32             Actions a=new Actions(driver);
33             a.moveToElement(header).perform();
34             Thread.sleep(3000);
35         
36             String s=this.element(this.byStr("usernameLocal")).getText();//获取标签属性
37             System.out.println(s);
38         
39             if(s.equals(this.getproperties("usernameInfo"))) {
40                 System.out.println("登陆成功,请退出");
41                 this.element(this.byStr("logOut")).click();
42             }else {
43                 System.out.println("登录失败");
44             }
45             //driver.quit();        
46         }
47         
48          public By byStr(String local) {
49             //5.在byStr函数中调用读取文件函数--getproperties(),并修改byStr传入参数
50              String locator=this.getproperties(local);
51              String locatorType=locator.split(">")[0];
52              String locatorValue=locator.split(">")[1];
53             //2.修改byStr函数中的代码,只返回by的方式    
54              if(locatorType.equals("id")) {             
55                 return By.id(locatorValue);
56              }else if(locatorType.equals("name")) {
57                 return By.name(locatorValue);
58              }else if(locatorType.equals("className")) {
59                 return By.className(locatorValue);
60              }else if(locatorType.equals("linkText")) {
61                 return By.linkText(locatorValue);
62              }else if(locatorType.equals("xpath")) {
63                 return By.xpath(locatorValue);
64              }else if(locatorType.equals("cssSelector")) {
65                 return By.cssSelector(locatorValue);
66              }else if(locatorType.equals("tageName")){
67                 return By.tagName(locatorValue);
68              }else {
69                 return By.partialLinkText(locatorValue);
70              }
71 
72          }
73         //1.封装 driver.findElenment(),编写element函数————将byStr函数中的 driver.findElenment()与by.方式()拆开
74          public WebElement element(By location) {
75              return driver.findElement(location);
76          }
77          //4.编写读取配置文件函数-----调用ProUtil类,读取配置文件
78          public String getproperties(String local) {
79              ProUtil properties=new ProUtil("E:\eclipse\workspace\mypro\src\mooc\day01\element.properties");
80              String locator=properties.getPro(local);
81              return locator;
82          }
83         public static void main(String[] args) throws InterruptedException {
84         
85             moocloginTwo login=new  moocloginTwo();
86             login.loginScript();
87         }
88 }

读取配置文件函数:

 1 public class ProUtil {
 2     private Properties properties;
 3     private String filePath;
 4     public ProUtil(String filePath) {
 5         this.filePath=filePath;
 6         this.properties=readProperties();
 7     }
 8     
 9     public Properties readProperties() {
10         Properties pro=new Properties();    
11         try {
12         InputStreamReader inputstream=new InputStreamReader(new FileInputStream(filePath),"UTF-8" );//考虑到编码格式
13         BufferedReader in=new BufferedReader(inputstream);
14         pro.load(in);    
15         } catch (IOException e) {
16             e.printStackTrace();
17         }
18         return pro;
19         
20     }
21     
22     public String getPro(String key) {
23         
24         if(properties.containsKey(key)) {
25         String value=properties.getProperty(key);
26         return value;
27     }else {
28         System.out.println("获取的键值不对");
29         return "  ";
30     }
31       
32     }
33 }

配置文件:

 1 #-----------登录页面元素部分:----------
 2 #登录网址
 3 Url=https://www.imooc.com
 4 #登录连接的位置
 5 loginLinkLocal=id>js-signin-btn
 6 #输入登录账号
 7 userLocal=name>email
 8 #输入登录密码
 9 PasswordLocal=name>password
10 #点击登录按钮
11 loginbtnLocal=cssSelector>.moco-btn
12 #个人头像
13 userImgLocal=cssSelector>#header-avator
14 #个人用户名
15 usernameLocal=className>name
16 #安全退出
17 logOut=linkText>安全退出
18 #--------登录参数部分:--------
19 #登录账号
20 userInfo=*********
21 #登录密码
22 passwordInfo=*******
23 #用于比对的用户名
24 usernameInfo=搁浅的青木
原文地址:https://www.cnblogs.com/geqiandeqingmu/p/12711420.html