Selenium PageFactory使用
2021-05-15 17:13
169 查看
通过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); } }
执行后:
相关文章推荐
- Selenium PageFactory使用
- Selenium之使用PageFactory初始化pageobject
- Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide
- PageFactory和@FindBy注解的使用
- Selenium的POM和PAGEFACTORY的理解
- Selenium的PageFactory & PageObject 在大型项目中的应用
- Selenium PageFactory页面工厂
- Selenium框架源码解析-1 PageFactory
- Selenium PageFactory
- Selenium源码之PageFactory
- Selenium的PageFactory & PageObject 在大型项目中的应用
- Selenium PageFactory页面工厂
- Python3-Selenium3使用PO设计模式(Page Object)实现简单的页面登录操作
- Selenium的PageFactory & PageObject 在大型项目中的应用
- selenium page object & Page Factory
- Selenium的PageFactory
- 使用页面对象模型(pageFactory)
- Appium使用PageFactory初始化对象时报空指针错误
- Selenium的PageFactory在大型项目中的应用
- selenium使用IE 浏览器问题