51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

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

Selenium PageFactory页面工厂

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-1-24 16:07:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Selenium PageFactory页面工厂
使用Selenium PageFactory页面工厂的好处是:
当页面元素的位置发生变化时,
我们只需要去修改id或者xpath,
而不用去修改测试用例。

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNG {
    private WebDriver driver;

    @FindBy(xpath = ".//*[@id='kw']")
    private WebElement inputBox;
    //输入框

    @FindBy(xpath = ".//*[@id='su']")
    private WebElement searchButton;
    //搜索按钮

    @BeforeMethod
    public void beforeMethod() throws InterruptedException {
        System.setProperty("webdriver.firefox.marionette",
                "src/main/resourcec/geckodriver.exe");
        String baiduHomePage;
        baiduHomePage = "https://www.baidu.com/";

        driver = new FirefoxDriver();
        PageFactory.initElements(driver, this);
        //构造函数,初始化PageFactory对象
        driver.manage().window().maximize();
        driver.get(baiduHomePage);
        Thread.sleep(2000);
        Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
    }

    @Test
    public void testNG() throws InterruptedException {
        inputBox.clear();
        inputBox.sendKeys("Selenium");

        searchButton.click();
        Thread.sleep(3000);

        Reporter.log("搜索Selenium的测试用例");
        Assert.assertEquals(driver.getTitle(), "Selenium_百度搜索");
    }

    @AfterMethod
    public void afterMethod(){
        driver.close();
        driver.quit();
    }

}


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

使用道具 举报

本版积分规则

关闭

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

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

GMT+8, 2024-5-3 12:37 , Processed in 0.062773 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

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