Selenium PageFactory使用

发表于:2017-1-03 11:22

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:梦一针    来源:51Testing软件测试网采编

  通过FindBy查找元素
  @FindBy(id="notice01")
  private WebElement username;
  相当于driver.findElement(By.id("notice01"));
  示例:
  (1)测试类:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.train.loginPage;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.openqa.selenium.support.PageFactory;
public class test_01 {
public static WebDriver driver =null;
@BeforeClass
public void beforeClass() {
System.out.println("Begin");
}
@Test
public  void chooseSeat() throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
loginPage m =PageFactory.initElements(driver,loginPage.class);
driver.get("http://trains.ctrip.com/TrainBooking/SearchTrain.aspx###");
m.Search(driver,"上海");
}
@AfterClass
public void afterClass() {
System.out.println("end ");
}
}
  (2)元素类:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
public class loginPage {
//通过id查找元素
@FindBy(id="notice01")
private WebElement username;
public  static WebElement element = null;
public static WebDriver driver =null;
public loginPage(WebDriver driver){
this.driver =driver;
}
public void  Search(WebDriver driver,String leave){
this.username.sendKeys(leave);
}
}
  执行后:
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号