selenium中分离页面元素(二)

  将页面元素封装,外部通过对应方法来调用,看如下代码,本文使用java编写

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

public class BaiduSeparatePage {
	// 定义百度首页搜索的输入框
	@FindBy(id = "kw")
	@CacheLookup
	private WebElement keyword_input;

	// 定义百度首页搜索按扭
	@FindBy(id = "su")
	@CacheLookup
	private WebElement search_button;
	
	//定义测试地址
	private final String url = "http://www.baidu.com";
	
	WebDriver driver;
	
	//创建一个构造函数,并启动firefox浏览器
	public BaiduSeparatePage() {
	    driver = new FirefoxDriver();
	    PageFactory.initElements(driver, this);		
	}
	
	//打开网址
	public void openUrl() {
		driver.get(url);
	}
	
	//关闭浏览器
	public void close() {
		driver.quit();
	}
	
	//输入关键字selenium并点击百度一下按扭
	public void searchByKeyword() {
		keyword_input.sendKeys("selenium");
		search_button.click();
	}	
}

测试类调用方法

import org.testng.annotations.Test;
import com.suning.encapsulation.BaiduSeparatePage;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;

public class BaiduSeparatePageTest {
  @Test
  public void f() {
	  //实例化页面对象模型
	  BaiduSeparatePage hsp = new BaiduSeparatePage();
     
      //调用方法
	  hsp.openUrl();
	  hsp.searchByKeyword();
	  hsp.close();	  
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值