您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源功能測(cè)試工具 > Selenium
Selenium PageFactory使用
作者:夢(mèng)一針 發(fā)布時(shí)間:[ 2017/1/3 11:29:31 ] 推薦標(biāo)簽:功能測(cè)試 Selenium

  通過FindBy查找元素
  @FindBy(id="notice01")
  private WebElement username;
  相當(dāng)于driver.findElement(By.id("notice01"));
  示例:
  (1)測(cè)試類:
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);
}
}
  執(zhí)行后:


 

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd