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

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

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

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



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

定义页面元素及方法

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

/**
@author tester
@version :2016年9月29日下午5:38:10
**/
public class LoginPage2 {

    WebDriver driver;
    
    @FindBy(how =How.NAME,name="UserName")
    WebElement username;//用户名
    @FindBy(how =How.NAME,name="Password")
    WebElement password;//密码
    @FindBy(how =How.CLASS_NAME,className="btn-default")
    WebElement loginbutton;//登录按钮
    
    public LoginPage2(WebDriver driver){
        this.driver=driver;
    }
    
    public void login(String userName,String passWord){
        username.sendKeys(userName);
        password.sendKeys(passWord);
        loginbutton.click();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
}

主页--注销操作

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

/**
@author tester
@version :2016年9月29日下午5:38:32
**/
public class LogoutPage2 {
   WebDriver driver;
   
   @FindBy(how=How.XPATH,xpath="//*[@id='navbar-container']/div[2]/div/li/a/span/small")
   WebElement linkbutton;//欢迎你,XXX
   @FindBy(how=How.XPATH,xpath="//*[@id='navbar-container']/div[2]/div/li/ul/li[3]/a/i")
   WebElement logoutbutton;//注销按钮
   
   public LogoutPage2(WebDriver driver){
	   this.driver=driver;
   }
  public void logout(){
	   
	   linkbutton.click();
	   logoutbutton.click();
	   driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
   }
}

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



import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;

/**
@author tester
@version :2016年9月29日下午5:38:50
**/
public class MainPage2 {
  WebDriver driver;
  
  
  public MainPage2(WebDriver driver){
	  this.driver=driver;
	  
  }
  public void openMainPage(String url){
	  driver.get(url);
	  
  }
  public void login(String userName2,String passWord2){
	 LoginPage2 loginpage=PageFactory.initElements(driver, LoginPage2.class);
	 loginpage.login(userName2, passWord2);
  }
  public void logout(){
	  LogoutPage2 logoutpage=PageFactory.initElements(driver, LogoutPage2.class);
	  logoutpage.logout();
  }
}

同理,新闻的新增


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

/**
@author tester
@version :2016年9月29日下午5:39:17
**/
public class SendMessagePage2 {
 
    WebDriver driver;
    
    @FindBy(how=How.NAME,name="TZBT")
    WebElement title;
    @FindBy(how=How.NAME,name="LYDW")
    WebElement unit;
    @FindBy(how=How.NAME,name="TZNR")
    WebElement content;
    @FindBy(how=How.XPATH,xpath="//input[@type='button']")
    WebElement saveButton;
    
    public SendMessagePage2(WebDriver driver){
        this.driver=driver;
    }
    
    public void sendNewMessage(String title2,String unit2,String content2){
        title.sendKeys(title2);
        unit.sendKeys(unit2);
        content.sendKeys(content2);
        saveButton.click();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    }
}

新闻的删除


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

/**
@author tester
@version :2016年9月29日下午5:39:50
**/
public class DeleteMessagePage2 {

	WebDriver driver;
	
	@FindBy(how=How.XPATH,xpath="//*[@id='main-container']/div[2]/div/div/div/div[2]/div[1]/div/div[1]/div/div[5]/button")
	WebElement deleteMessageButton;//删除按钮
	@FindBy(how=How.XPATH,xpath="//*[@id='publicinfo']/tbody/tr[1]")
    WebElement checkSelectMessage;//选中通知信息
	
	public DeleteMessagePage2(WebDriver driver){
		this.driver=driver;
	}
	
	public void deleteMessage(){
		checkSelectMessage.click();
		if(!checkSelectMessage.isSelected()){
			checkSelectMessage.click();
		}
		deleteMessageButton.click();
		
		WebElement confirmDeletePrompt=driver.findElement(By.xpath("//button[@type='button' and @i-id='ok']"));//删除弹出框的确认按钮
    	confirmDeletePrompt.click();
		
	}
}


封装新增和删除操作


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

import com.learningselenium.pageobject.normaluse.DeleteMessagePage1;
import com.learningselenium.pageobject.normaluse.SendMessagePage1;

/**
@author tester
@version :2016年9月29日下午5:40:05
**/
public class MessagePage2 {

	WebDriver driver;
	
	@FindBy(how=How.LINK_TEXT,linkText="通知消息")
	WebElement messageLink;//通知信息的链接
	
	@FindBy(how=How.XPATH,xpath="//*[@id='main-container']/div[2]/div/div/div/div[2]/div[1]/div/div[1]/div/div[2]/button")
	WebElement newMessage;//新增通知信息
	
	@FindBy(how=How.XPATH,xpath="//*[@id='sidebar-collapse']/i")
	WebElement menu;//菜单栏
	
	@FindBy(how=How.XPATH,xpath="//*[@id='sidebar']/div[1]/ul/li[7]/a/span")
	WebElement systemManagement;//系统管理
	
	
	public MessagePage2(WebDriver driver){
		this.driver=driver;
	}
	
	public void enterMessageLink(){
		
		menu.click();//点击打开菜单栏
		systemManagement.click();//点击系统管理
		
		messageLink.click();
		driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
	}
	  //新增通知信息
		public void sendMessage(String title1,String unit1,String content1){
			
			enterMessageLink();
			newMessage.click();
			SendMessagePage2 sendMessagePage=PageFactory.initElements(driver, SendMessagePage2.class);
			sendMessagePage.sendNewMessage(title1, unit1, content1);
		}
		//删除通知信息
		public void deleteMessage(){
			enterMessageLink();
			DeleteMessagePage2 deleteMessagePage=PageFactory.initElements(driver, DeleteMessagePage2.class);
			deleteMessagePage.deleteMessage();
		}
}


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


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

import com.learningselenium.pageobject.normaluse.MainPage1;
import com.learningselenium.pageobject.normaluse.MessagePage1;

/**
@author tester
@version :2016年9月29日下午5:41:21

@FindBy 可以用于替换driver.findElement()方法查找机制来定位页面元素
@FindBy(how=How.XPATH,xpath=""),结合@FindBy,同时可以使用How数组来替换By的作用
PageFactory替换传统的通过new来实例化对象的方式
**/
public class testMessageWithPageObject2 {

	public static void main(String[] args) {
		  WebDriver driver=new FirefoxDriver();
	      MainPage2 mainPage=PageFactory.initElements(driver, MainPage2.class);
	      MessagePage2 messagePage=PageFactory.initElements(driver, MessagePage2.class);
	      mainPage.openMainPage("http://xxxxxxxx");
	      mainPage.login("admin", "1234567");
	      messagePage.sendMessage("标题", "单位", "内容");
	     
	      
	      mainPage.logout();
	      
	      mainPage.openMainPage("http://xxxxxxxx");
	      mainPage.login("admin", "1234567");
	      messagePage.deleteMessage();
	      mainPage.logout();
	      driver.quit();
	}
}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值