码迷,mamicode.com
首页 > 其他好文 > 详细

软件测试Lab2 Selenium及自动化测试

时间:2016-04-16 23:04:27      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:

安装SeleniumIDE插件

打开Fire Fox浏览器

点击附加组件

技术分享

之后搜索Selenium IDE

 

技术分享

安装

 

技术分享

安装,即可完成Selenium的安装

 

录制导出脚本

打开SeleniumIDE,

 

技术分享

技术分享

输入网页之后,将信息填至相应的位置,单击确定。

技术分享

我们发现已经录制完成,导出时文件->export test case as -> Java/junit4 webdriver即可得到相应的java文件

 

编写测试代码

import java.io.File;
import java.nio.charset.Charset;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import static org.junit.Assert.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.csvreader.CsvReader;
public class test {
    static Thread th = new Thread(); 
    public static void main(String[] args) throws Exception {
        WebDriver driver = new FirefoxDriver();        
        CsvReader r = new CsvReader("D://info.csv", ,,Charset.forName("utf-8"));
        r.readHeaders();
        while (r.readRecord()) {
            String name = r.get("id");
            String password = name.substring(4);
            String email = r.get("e-mail");
            driver.get("http://www.ncfxy.com/");  
            th.sleep(100);
            WebElement txtbox1 = driver.findElement(By.id("name"));
            txtbox1.sendKeys(name);
            WebElement txtbox2 = driver.findElement(By.id("pwd"));
            txtbox2.sendKeys(password);
            WebElement btn = driver.findElement(By.id("submit"));
            btn.click();
            th.sleep(100);
            WebElement text = driver.findElement(By.cssSelector("#table-main tr:first-child td:last-child"));
            String email2 = text.getText();
            assertEquals(email,email2);
        }
        r.close();
    }

}

这里因为本地运行速度有一些不明原因,导致如果直接运行代码会发生找不到元素的问题,设置一个新线程每次sleep100ms即可。

对info.csv进行判断,这里更改了info.csv为了配合csvreader

添加表头id和e-mail用于取出学号和邮箱。

 

运行完毕后发现测试全部通过,至此完成本次任务。

 

全部代码工程文件:

https://github.com/FomalhautYWT/SoftwareTest/tree/master/STLab2

 

软件测试Lab2 Selenium及自动化测试

标签:

原文地址:http://www.cnblogs.com/Durandal/p/5399552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!