selenium面向对象之findBy()的使用--基于Java的selenium程序

从开始学习用webDriver和java进行编程,面向对象编程就成为了一种必然。方便结构化,更利于代码的管理。

这里列举了登陆、登出、新闻的新增、删除操作。使用面向对象将页面操作进行分离

查找元素,通过使用了findBy().如果项目中的元素随着开发的开发而改变,使用它,就可以方便查找并进行修改。



登陆页面-登陆操作(LoginPage2):

定义页面元素及方法

[java]  view plain  copy
  1. import java.util.concurrent.TimeUnit;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.support.FindBy;  
  6. import org.openqa.selenium.support.How;  
  7.   
  8. /** 
  9. @author tester 
  10. @version :2016年9月29日下午5:38:10 
  11. **/  
  12. public class LoginPage2 {  
  13.   
  14.     WebDriver driver;  
  15.       
  16.     @FindBy(how =How.NAME,name="UserName")  
  17.     WebElement username;//用户名  
  18.     @FindBy(how =How.NAME,name="Password")  
  19.     WebElement password;//密码  
  20.     @FindBy(how =How.CLASS_NAME,className="btn-default")  
  21.     WebElement loginbutton;//登录按钮  
  22.       
  23.     public LoginPage2(WebDriver driver){  
  24.         this.driver=driver;  
  25.     }  
  26.       
  27.     public void login(String userName,String passWord){  
  28.         username.sendKeys(userName);  
  29.         password.sendKeys(passWord);  
  30.         loginbutton.click();  
  31.         driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  
  32.     }  
  33. }  

主页--注销操作

[java]  view plain  copy
  1. import java.util.concurrent.TimeUnit;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.support.FindBy;  
  6. import org.openqa.selenium.support.How;  
  7.   
  8. /** 
  9. @author tester 
  10. @version :2016年9月29日下午5:38:32 
  11. **/  
  12. public class LogoutPage2 {  
  13.    WebDriver driver;  
  14.      
  15.    @FindBy(how=How.XPATH,xpath="//*[@id='navbar-container']/div[2]/div/li/a/span/small")  
  16.    WebElement linkbutton;//欢迎你,XXX  
  17.    @FindBy(how=How.XPATH,xpath="//*[@id='navbar-container']/div[2]/div/li/ul/li[3]/a/i")  
  18.    WebElement logoutbutton;//注销按钮  
  19.      
  20.    public LogoutPage2(WebDriver driver){  
  21.        this.driver=driver;  
  22.    }  
  23.   public void logout(){  
  24.          
  25.        linkbutton.click();  
  26.        logoutbutton.click();  
  27.        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  
  28.    }  
  29. }  

将登陆操作和注销操作进行封装

[java]  view plain  copy
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.support.PageFactory;  
  3.   
  4. /** 
  5. @author tester 
  6. @version :2016年9月29日下午5:38:50 
  7. **/  
  8. public class MainPage2 {  
  9.   WebDriver driver;  
  10.     
  11.     
  12.   public MainPage2(WebDriver driver){  
  13.       this.driver=driver;  
  14.         
  15.   }  
  16.   public void openMainPage(String url){  
  17.       driver.get(url);  
  18.         
  19.   }  
  20.   public void login(String userName2,String passWord2){  
  21.      LoginPage2 loginpage=PageFactory.initElements(driver, LoginPage2.class);  
  22.      loginpage.login(userName2, passWord2);  
  23.   }  
  24.   public void logout(){  
  25.       LogoutPage2 logoutpage=PageFactory.initElements(driver, LogoutPage2.class);  
  26.       logoutpage.logout();  
  27.   }  
  28. }  

同理,新闻的新增

[java]  view plain  copy
  1. import java.util.concurrent.TimeUnit;  
  2.   
  3. import org.openqa.selenium.WebDriver;  
  4. import org.openqa.selenium.WebElement;  
  5. import org.openqa.selenium.support.FindBy;  
  6. import org.openqa.selenium.support.How;  
  7.   
  8. /** 
  9. @author tester 
  10. @version :2016年9月29日下午5:39:17 
  11. **/  
  12. public class SendMessagePage2 {  
  13.    
  14.     WebDriver driver;  
  15.       
  16.     @FindBy(how=How.NAME,name="TZBT")  
  17.     WebElement title;  
  18.     @FindBy(how=How.NAME,name="LYDW")  
  19.     WebElement unit;  
  20.     @FindBy(how=How.NAME,name="TZNR")  
  21.     WebElement content;  
  22.     @FindBy(how=How.XPATH,xpath="//input[@type='button']")  
  23.     WebElement saveButton;  
  24.       
  25.     public SendMessagePage2(WebDriver driver){  
  26.         this.driver=driver;  
  27.     }  
  28.       
  29.     public void sendNewMessage(String title2,String unit2,String content2){  
  30.         title.sendKeys(title2);  
  31.         unit.sendKeys(unit2);  
  32.         content.sendKeys(content2);  
  33.         saveButton.click();  
  34.         driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  
  35.     }  
  36. }  

新闻的删除

[java]  view plain  copy
  1. import org.openqa.selenium.By;  
  2. import org.openqa.selenium.WebDriver;  
  3. import org.openqa.selenium.WebElement;  
  4. import org.openqa.selenium.support.FindBy;  
  5. import org.openqa.selenium.support.How;  
  6.   
  7. /** 
  8. @author tester 
  9. @version :2016年9月29日下午5:39:50 
  10. **/  
  11. public class DeleteMessagePage2 {  
  12.   
  13.     WebDriver driver;  
  14.       
  15.     @FindBy(how=How.XPATH,xpath="//*[@id='main-container']/div[2]/div/div/div/div[2]/div[1]/div/div[1]/div/div[5]/button")  
  16.     WebElement deleteMessageButton;//删除按钮  
  17.     @FindBy(how=How.XPATH,xpath="//*[@id='publicinfo']/tbody/tr[1]")  
  18.     WebElement checkSelectMessage;//选中通知信息  
  19.       
  20.     public DeleteMessagePage2(WebDriver driver){  
  21.         this.driver=driver;  
  22.     }  
  23.       
  24.     public void deleteMessage(){  
  25.         checkSelectMessage.click();  
  26.         if(!checkSelectMessage.isSelected()){  
  27.             checkSelectMessage.click();  
  28.         }  
  29.         deleteMessageButton.click();  
  30.           
  31.         WebElement confirmDeletePrompt=driver.findElement(By.xpath("//button[@type='button' and @i-id='ok']"));//删除弹出框的确认按钮  
  32.         confirmDeletePrompt.click();  
  33.           
  34.     }  
  35. }  


封装新增和删除操作

[java]  view plain  copy
  1. import java.util.concurrent.TimeUnit;  
  2.   
  3. import org.openqa.selenium.By;  
  4. import org.openqa.selenium.WebDriver;  
  5. import org.openqa.selenium.WebElement;  
  6. import org.openqa.selenium.support.FindBy;  
  7. import org.openqa.selenium.support.How;  
  8. import org.openqa.selenium.support.PageFactory;  
  9.   
  10. import com.learningselenium.pageobject.normaluse.DeleteMessagePage1;  
  11. import com.learningselenium.pageobject.normaluse.SendMessagePage1;  
  12.   
  13. /** 
  14. @author tester 
  15. @version :2016年9月29日下午5:40:05 
  16. **/  
  17. public class MessagePage2 {  
  18.   
  19.     WebDriver driver;  
  20.       
  21.     @FindBy(how=How.LINK_TEXT,linkText="通知消息")  
  22.     WebElement messageLink;//通知信息的链接  
  23.       
  24.     @FindBy(how=How.XPATH,xpath="//*[@id='main-container']/div[2]/div/div/div/div[2]/div[1]/div/div[1]/div/div[2]/button")  
  25.     WebElement newMessage;//新增通知信息  
  26.       
  27.     @FindBy(how=How.XPATH,xpath="//*[@id='sidebar-collapse']/i")  
  28.     WebElement menu;//菜单栏  
  29.       
  30.     @FindBy(how=How.XPATH,xpath="//*[@id='sidebar']/div[1]/ul/li[7]/a/span")  
  31.     WebElement systemManagement;//系统管理  
  32.       
  33.       
  34.     public MessagePage2(WebDriver driver){  
  35.         this.driver=driver;  
  36.     }  
  37.       
  38.     public void enterMessageLink(){  
  39.           
  40.         menu.click();//点击打开菜单栏  
  41.         systemManagement.click();//点击系统管理  
  42.           
  43.         messageLink.click();  
  44.         driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  
  45.     }  
  46.       //新增通知信息  
  47.         public void sendMessage(String title1,String unit1,String content1){  
  48.               
  49.             enterMessageLink();  
  50.             newMessage.click();  
  51.             SendMessagePage2 sendMessagePage=PageFactory.initElements(driver, SendMessagePage2.class);  
  52.             sendMessagePage.sendNewMessage(title1, unit1, content1);  
  53.         }  
  54.         //删除通知信息  
  55.         public void deleteMessage(){  
  56.             enterMessageLink();  
  57.             DeleteMessagePage2 deleteMessagePage=PageFactory.initElements(driver, DeleteMessagePage2.class);  
  58.             deleteMessagePage.deleteMessage();  
  59.         }  
  60. }  


最后,一个主方法进行调用并实现。

[java]  view plain  copy
  1. import org.openqa.selenium.WebDriver;  
  2. import org.openqa.selenium.firefox.FirefoxDriver;  
  3. import org.openqa.selenium.support.FindBy;  
  4. import org.openqa.selenium.support.How;  
  5. import org.openqa.selenium.support.PageFactory;  
  6.   
  7. import com.learningselenium.pageobject.normaluse.MainPage1;  
  8. import com.learningselenium.pageobject.normaluse.MessagePage1;  
  9.   
  10. /** 
  11. @author tester 
  12. @version :2016年9月29日下午5:41:21 
  13.  
  14. @FindBy 可以用于替换driver.findElement()方法查找机制来定位页面元素 
  15. @FindBy(how=How.XPATH,xpath=""),结合@FindBy,同时可以使用How数组来替换By的作用 
  16. PageFactory替换传统的通过new来实例化对象的方式 
  17. **/  
  18. public class testMessageWithPageObject2 {  
  19.   
  20.     public static void main(String[] args) {  
  21.           WebDriver driver=new FirefoxDriver();  
  22.           MainPage2 mainPage=PageFactory.initElements(driver, MainPage2.class);  
  23.           MessagePage2 messagePage=PageFactory.initElements(driver, MessagePage2.class);  
  24.           mainPage.openMainPage("http://xxxxxxxx");  
  25.           mainPage.login("admin""1234567");  
  26.           messagePage.sendMessage("标题""单位""内容");  
  27.            
  28.             
  29.           mainPage.logout();  
  30.             
  31.           mainPage.openMainPage("http://xxxxxxxx");  
  32.           mainPage.login("admin""1234567");  
  33.           messagePage.deleteMessage();  
  34.           mainPage.logout();  
  35.           driver.quit();  
  36.     }  

  1. }  

转自: https://blog.csdn.net/mine333/article/details/53002047

猜你喜欢

转载自blog.csdn.net/weixin_41585557/article/details/80786559