51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1452|回复: 0
打印 上一主题 下一主题

Selenium PageFactory使用

[复制链接]
  • TA的每日心情
    奋斗
    2021-8-16 14:04
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    跳转到指定楼层
    1#
    发表于 2018-7-6 15:01:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    通过FindBy查找元素
    @FindBy(id="notice01")
    private WebElement username;
    相当于driver.findElement(By.id("notice01"));
    示例:
    (1)测试类:
    1. import org.openqa.selenium.WebDriver;
    2. import org.openqa.selenium.chrome.ChromeDriver;
    3. import org.testng.Assert;
    4. import org.testng.annotations.Test;
    5. import com.train.loginPage;
    6. import org.testng.annotations.BeforeClass;
    7. import org.testng.annotations.AfterClass;
    8. import org.openqa.selenium.support.PageFactory;
    9. public class test_01 {
    10.      
    11.     public static WebDriver driver =null;
    12.      
    13.     @BeforeClass
    14.     public void beforeClass() {
    15.         System.out.println("Begin");
    16.          
    17.     }
    18.      
    19.     @Test
    20.     public  void chooseSeat() throws InterruptedException{
    21.         System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
    22.         driver = new ChromeDriver();
    23.         loginPage m =PageFactory.initElements(driver,loginPage.class);
    24.         driver.get("http://trains.ctrip.com/TrainBooking/SearchTrain.aspx###");
    25.             m.Search(driver,"上海");
    26.          
    27.     }
    28.      
    29.     @AfterClass
    30.     public void afterClass() {
    31.         System.out.println("end ");

    32.     }

    33. }
    复制代码


    (2)元素类:

    1. import org.openqa.selenium.By;
    2. import org.openqa.selenium.WebDriver;
    3. import org.openqa.selenium.WebElement;
    4. import org.openqa.selenium.support.FindBy;
    5. public class loginPage {
    6.     //通过id查找元素
    7.     @FindBy(id="notice01")
    8.     private WebElement username;
    9.    
    10.     public  static WebElement element = null;
    11.     public static WebDriver driver =null;
    12.     public loginPage(WebDriver driver){
    13.         this.driver =driver;
    14.         
    15.     }
    16.     public void  Search(WebDriver driver,String leave){
    17.         this.username.sendKeys(leave);   
    18. }

    19. }
    复制代码


    执行后:




    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏1
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-4-20 12:24 , Processed in 0.069012 second(s), 24 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表