软件测试-Test Selenium

 

1、    安装SeleniumIDE插件

打开firefox 浏览器,进入官方网址:http://seleniumhq.org/download/

 

 

找到selenium IDE的下载链接,firefox会有提示安装插件,点击安装即可。

 

然后在菜单里就可以找到selenuim点击即可使用

2、    学会使用SeleniumIDE录制脚本和导出脚本

点击左侧红色的录制按钮,在浏览器中打开一个新的标签--输入百度网址--在搜索栏输入selenium  点击搜索按钮,右键点击

Show all available commands->assert……

 

 

录制完成后,点击红色的按钮,结束掉录制。可以点击绿色的三角按钮回放录制的脚本。

 

 

点击Export Test Case As   -> JAVA/JUnit4/WebDrive ,

即可保存Test case.

3、编写Selenium Java WebDriver程序,测试inputgit.csv表格中的学号和git地址的对应关系是否正确

首先按照以上的方法获取到测试inputgit.csv表格中的学号和git地址的对应关系的一个java文件。

我们需要对这个文件进行修改:

public void setUp() throws Exception {
        System.setProperty("webdriver.firefox.bin", "E:/firefox/firefox.exe");//需要加入这句话,打开firebox
        driver = new FirefoxDriver();
        baseUrl = "http://121.193.130.195:8080";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

然后我们需要进行对CSV文件的读取:

public void test() throws Exception {
        
        String[] str = { "" };
        String inString = "";
        String tmpString = "";
        String[] sourceStrArray;
        int num=0;
        File inFile = new File(
                "C://Users/mjm/Desktop/作业/软件测试/lab2/inputgit.csv");//读取文件
        try {
            BufferedReader reader = new BufferedReader(new FileReader(inFile));
            CsvReader creader = new CsvReader(reader, ',');
            while (creader.readRecord()) {
                inString = creader.getRawRecord();// 读取一行数据
                int maxSplit = 3;
                sourceStrArray = inString.split(",", maxSplit);//按照,分割字符串
                String a = sourceStrArray[0];//获取到学号
                if (a.length() > 6) {
                    String b = a.substring(a.length() - 6, a.length());//获取学号后六位为密码
                }
                
            }
            creader.close();
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

然后对上一步进行进一步的加入代码:我们这里加入的是对浏览器进行操作的代码,使用driver

public void test() throws Exception {
        
        String[] str = { "" };
        String inString = "";
        String tmpString = "";
        String[] sourceStrArray;
        int num=0;
        File inFile = new File(
                "C://Users/mjm/Desktop/作业/软件测试/lab2/inputgit.csv");//读取文件
        try {
            BufferedReader reader = new BufferedReader(new FileReader(inFile));
            CsvReader creader = new CsvReader(reader, ',');
            while (creader.readRecord()) {
                inString = creader.getRawRecord();// 读取一行数据
                int maxSplit = 3;
                sourceStrArray = inString.split(",", maxSplit);//按照,分割字符串
                String a = sourceStrArray[0];//获取到学号
                if (a.length() > 6) {
                    String b = a.substring(a.length() - 6, a.length());//获取学号后六位为密码
                    driver.get(baseUrl + "/");
                    driver.findElement(By.id("pwd")).clear();
                    driver.findElement(By.id("name")).clear();
                    driver.findElement(By.id("name")).sendKeys(sourceStrArray[0]);
                    driver.findElement(By.id("pwd")).sendKeys(b);
                    driver.findElement(By.id("submit")).click();
                    assertEquals(sourceStrArray[2],driver.findElement(By.xpath("//tbody/tr[3]/td[2]")).getText());
                }
                
            }
            creader.close();
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }

我们想要获取到网站中的Git账号需要使用到Xpath,详情见:http://www.w3school.com.cn/xpath

运行代码即可!

 

posted @ 2017-03-24 16:37  Hormone  阅读(234)  评论(0编辑  收藏  举报