51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1665|回复: 4
打印 上一主题 下一主题

Selenium页面工厂+数据驱动测试框架

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-1-24 16:09:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Selenium页面工厂+数据驱动测试框架
工程的目录结构:


pom.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>shanghai</groupId>
    <artifactId>frame</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.8.1</version>
        </dependency>

        <dependency>
            <groupId>org.uncommons</groupId>
            <artifactId>reportng</artifactId>
            <version>1.1.4</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.1.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

textng.xml文件:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="百度搜索的测试套件">

    <test verbose="2" preserve-order="true" name="百度搜索">
        <classes>
            <class name="BaiduSearchCase" />
        </classes>
    </test>

    <listeners>
        <listener class-name="org.uncommons.reportng.HTMLReporter" />
        <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
    </listeners>

</suite>

页面工厂: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;

public class PageFactorys {
    //页面工厂

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

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

    @FindBy(xpath = ".//*[@id='1']/h3/a")
    private WebElement searchResult;
    //搜索结果第一行

    private WebDriver driver;

    public PageFactorys(){
        //构造函数,生成浏览器对象,初始化PageFactory对象
        System.setProperty("webdriver.firefox.marionette",
                "src/main/resourcec/geckodriver.exe");
        driver = new FirefoxDriver();
        PageFactory.initElements(driver, this);
        driver.manage().window().maximize();
    }

    public void open(){
        //打开百度
        String baiduUrl = "https://www.baidu.com/";
        driver.get(baiduUrl);
    }

    public void refresh(){
        //刷新浏览器
        driver.navigate().refresh();
    }

    public void quit(){
        //退出浏览器
        driver.close();
        driver.quit();
    }

    public void search(String value){
        //输入并搜索
        inputBox.clear();
        inputBox.sendKeys(value);
        searchButton.click();
    }

    public String text(){
        //获取搜索结果第一行的文本
        return searchResult.getText();
    }

}

读取Csv文件的工具类:import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ReadCSV {

    public static Object [][] readCSV(String fileName)
            throws IOException {
        //读取CSV文件的方法
        List<Object[]> records = new ArrayList<Object[]>();
        String record;
        BufferedReader file = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(fileName),
                "UTF-8"));
        file.readLine();
        while ((record=file.readLine())!=null){
            String fields[] =  record.split(",");
            records.add(fields);
        }
        file.close();

        Object[][] results = new Object[records.size()][];
        for (int i=0; i<records.size();i++){
            results = records.get(i);
        }
        return results;
    }

}

Csv文件:

关键字 预期的搜索结果 测试用例的名称
中国,中国_百度百科,百度搜索中国的测试用例
美国,美国_百度百科,百度搜索美国的测试用例
英国,英国_百度百科,百度搜索英国的测试用例
法国,法国_百度百科,百度搜索法国的测试用例

测试用例:import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;

public class BaiduSearchCase {

    private PageFactorys pageFactorys = new PageFactorys();

    @BeforeClass
    public void beforeClass() throws InterruptedException {
        pageFactorys.open();
        Thread.sleep(2000);
    }

    @Test(dataProvider = "keyword")
    //百度搜索的测试用例
    public void baiduSearchCase(String word, String result, String case_1)
            throws InterruptedException {
        pageFactorys.search(word);
        Thread.sleep(2000);
        Assert.assertEquals(pageFactorys.text(), result);
        Reporter.log(case_1);
        pageFactorys.refresh();
        Thread.sleep(2000);
    }

    @AfterClass
    public void afterClass(){
        pageFactorys.quit();
    }

    @DataProvider(name = "keyword")
    public Object[][] dp() throws IOException {
        return ReadCSV.readCSV("src/main/resources/keyword.csv");
    }

}

测试报告:




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

使用道具 举报

该用户从未签到

2#
发表于 2018-2-24 16:03:20 | 只看该作者
现在的自动化概念 就只有这么初级的吗?
回复 支持 反对

使用道具 举报

  • TA的每日心情
    奋斗
    2018-6-18 17:17
  • 签到天数: 4 天

    连续签到: 1 天

    [LV.2]测试排长

    3#
    发表于 2018-2-27 09:57:34 | 只看该作者
    这个是用 maven 搭的框架吗?在eclipse 里面还是哪个?
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
     楼主| 发表于 2018-3-10 15:46:07 来自手机 | 只看该作者
    这只是我把百度搜索作为一个例子来写,你看着不舒服可以不看!
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-4-25 23:03 , Processed in 0.064710 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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