51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 2556|回复: 3
打印 上一主题 下一主题

用junit4+selenium+Java的时候,发现Assert.assertEquals的问题,请高人指点,谢谢!

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2015-7-3 16:45:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用junit4+selenium+Java的时候,发现Assert.assertEquals函数一旦fail,case就中断执行了,怎么让他继续执行呢?就算其中一个点fail了,比如   Assert.assertEquals("not equal",2, lwo_username.size());,我还是想让他继续执行。

代码如下:
TestScript.Java
package Test;

import java.io.FileInputStream;

import java.util.List;


import org.junit.Assert;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class TestScript {
       
        WebDriver driver;  


        public Object GetData(String excelInput, String SheetName,int rownum,int cellnum){
       
                Object result = null;
                System.out.println(excelInput);
               
                try{
                        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelInput));
                        HSSFWorkbook workbook = new HSSFWorkbook(fs);
                        Sheet sheet=workbook.getSheet(SheetName);
                        Row row=sheet.getRow(rownum);
                        Cell cell=row.getCell(cellnum);
                       
                //cell.getStringCellValue();
                        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                        CellValue cellValue = evaluator.evaluate(cell);
               
                //System.out.println(cellValue.getCellType());
               
                if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                        result = cellValue.getBooleanValue();
                        } else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                        result = cellValue.getNumberValue();
                        } else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) {
                        result = cellValue.getStringValue();
                        }
               

                }
               
                catch(Exception e){
                        e.printStackTrace();
                        }
               
                return result;
        }
       
       
       
            public void OpenIE(String URL) throws InterruptedException {

                        System.setProperty("webdriver.ie.driver", "G:\\Selenium\\IEDriverServer.exe");
                driver = new InternetExplorerDriver();
                driver.get(URL);
                Thread.sleep(2000);

            }


            public void login(Object username2, Object password2){
                WebElement username;
                WebElement password;
                WebElement Enterbutton;
                WebElement Text;
                
                
                List<WebElement> lwo_username;
                lwo_username=driver.findElements(By.xpath("//Input[@name='j_username']"));
                Assert.assertEquals("not equal",2, lwo_username.size());
                
                
                username=driver.findElement(By.xpath("//Input[@name='j_username']"));
                username.sendKeys(username2.toString());
                
                password=driver.findElement(By.xpath("//Input[@name='j_password']"));                
                password.sendKeys(password2.toString());
                
                Enterbutton=driver.findElement(By.xpath("//Input[@value='Enter']"));
                Enterbutton.click();                
                
                Text=driver.findElement(By.xpath("//Span[contains (text(),'Your last successful sign in was on')]"));
                Assert.assertNotNull("Pass", Text);
         

            }
            
}

TestScriptTest.java
package Test;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestScriptTest  {
       
        public static TestScript a= new TestScript();
        String URL="http://172.20.88.99";
        Object Username=a.GetData("G:/Selenium/Test1.xls", "Login", 1, 1);               
        Object Password=a.GetData("G:/Selenium/Test1.xls", "Login", 1, 2);       
       
        @Before       
        public void setUp() throws Exception {
                try {
                        a.OpenIE(URL);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        @After
        public void tearDown() throws Exception {
        }

        @Test
        public void testLogin() {
                a.login(Username, Password);
               
        }

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

使用道具 举报

  • TA的每日心情
    无聊
    2020-12-8 11:20
  • 签到天数: 605 天

    连续签到: 1 天

    [LV.9]测试副司令

    2#
    发表于 2015-7-6 13:33:33 | 只看该作者
    断言是这样的,出错就中断执行,你这样的写检查点不是个好的策略
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    3#
     楼主| 发表于 2015-7-6 13:47:22 | 只看该作者
    joykao 发表于 2015-7-6 13:33
    断言是这样的,出错就中断执行,你这样的写检查点不是个好的策略

    谢谢,那如果要写几个检查点用什么方法比较好呢?
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    无聊
    2020-12-8 11:20
  • 签到天数: 605 天

    连续签到: 1 天

    [LV.9]测试副司令

    4#
    发表于 2015-7-7 10:35:40 | 只看该作者
    donkey0702 发表于 2015-7-6 13:47
    谢谢,那如果要写几个检查点用什么方法比较好呢?

    我是这么想的,你可以把登录抽象成公用方法,然后呢要测试登录直接call,把检查点设在具体的测试用例中,如果你的检查点是随着动态变化的尽量不要用完全匹配,可以参考下http://www.51testing.com/html/02/241902-849343.html
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-5-25 04:38 , Processed in 0.065235 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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