多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Selenium PageFactory使用-自动化测试

更新时间:2022-04-20 09:03:49 作者:多测师 浏览:154

  通过FindBy查找元素

  @FindBy(id="notice01")

  private WebElement username;

  相当于driver.findElement(By.id("notice01"));

Selenium PageFactory使用-自动化测试

  示例:

  (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);

  }

  }

  以上内容为大家介绍了自动化测试中的Selenium PageFactory使用,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部