selenium面向对象之findBy()的使用

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

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

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



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

定义页面元素及方法

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

主页--注销操作

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

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

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

同理,新闻的新增

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

新闻的删除

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


封装新增和删除操作

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


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

  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. import com.learningselenium.pageobject.normaluse.MainPage1;
  7. import com.learningselenium.pageobject.normaluse.MessagePage1;
  8. /**
  9. @author tester
  10. @version :2016年9月29日下午5:41:21
  11. @FindBy 可以用于替换driver.findElement()方法查找机制来定位页面元素
  12. @FindBy(how=How.XPATH,xpath=""),结合@FindBy,同时可以使用How数组来替换By的作用
  13. PageFactory替换传统的通过new来实例化对象的方式
  14. **/
  15. public class testMessageWithPageObject2 {
  16. public static void main(String[] args) {
  17. WebDriver driver= new FirefoxDriver();
  18. MainPage2 mainPage=PageFactory.initElements(driver, MainPage2.class);
  19. MessagePage2 messagePage=PageFactory.initElements(driver, MessagePage2.class);
  20. mainPage.openMainPage( "http://xxxxxxxx");
  21. mainPage.login( "admin", "1234567");
  22. messagePage.sendMessage( "标题", "单位", "内容");
  23. mainPage.logout();
  24. mainPage.openMainPage( "http://xxxxxxxx");
  25. mainPage.login( "admin", "1234567");
  26. messagePage.deleteMessage();
  27. mainPage.logout();
  28. driver.quit();
  29. }
  30. }
  31. 转载。 https://blog.csdn.net/mine333/article/details/53002047
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值