TestNG在循环

时间:2016-08-04 18:38:46

标签: java selenium selenium-webdriver testng

随处搜索但无法找到解决方法如何执行以下操作: 例如:

 for (int i = 0; i < 5; i++) {
@Test
public void test1(){
assertThat(assertvalue 1);
}

@Test
public void test2(){
assertThat(assertvalue 2);
}
@Test
public void test3(){
assertThat(assertvalue 3);
}
}

当然我可以使用dataProvider或invocationCount并在一个测试用例中执行所有断言,但如果一个断言将失败,整个测试用例将失败。对于3个小测试用例,我想要一个单独的大测试用例 任何帮助表示赞赏

4 个答案:

答案 0 :(得分:1)

不鼓励在测试用例之间存在依赖关系。您可以获得类似的结果 - 使用SoftAssert检查每个值1-5而不会发生短路并在2上发生故障后停止 -

SoftAssert soft = new SoftAssert();
soft.fail("Failure # 1\n");
soft.fail("Failure # 2");
soft.assertAll();

应输出:

  

以下断言失败:
  失败#1
  失败#2

答案 1 :(得分:0)

我尝试了以下代码:

public class SimpleTest {
    private String param = "";
    public SimpleTest(String param) {
        this.param = param;
    }

    @BeforeClass
    public void beforeClass() {
        System.out.println("Before SimpleTest class executed.");
    }

    @Test
    public void testMethod1() {
        System.out.println("testMethod parameter value is: " + param);
    }

    @Test(dependsOnMethods = "testMethod1")
    public void testMethod2() {
        System.out.println("testMethod parameter value is: " + param);
    }
}

public class SimpleTestFactory{
@Factory
public Object[] factoryMethod() {
    return new Object[] {
            new SimpleTest("one"),
            new SimpleTest("two")
    };
}

尝试使用以下代码,但不保留订单

答案 2 :(得分:-1)

使用“property”,如果要执行test1(),请添加“priority = 1”

       @Test
        public void test1(priority =1){
          System.out.println("execute frist");;
        }

        @Test
        public void test2(priority =2){
          assertThat(assertvalue 2);
        }
        @Test
        public void test3(priority =3){
          assertThat(assertvalue 3);
        }

答案 3 :(得分:-1)

@Test
  public void main() throws IOException,InterruptedException
{
    FileInputStream fil = new FileInputStream(new File("C:\\Users\\V-DKumaravelu\\ECM Data\\ECM-Data-Sheet.xlsx"));
    XSSFWorkbook workbook=new XSSFWorkbook(fil);
    XSSFSheet sheet=workbook.getSheet("ECM");
    int count=sheet.getLastRowNum();
    System.out.println(count);

        for (int i=1;i<=5;i++){

            Snapshot sc = new Snapshot();

        XSSFRow row = sheet.getRow(i);
        XSSFCell cell = row.getCell(0);
        String ServiceDate=cell.getStringCellValue();
        System.out.println("DOSate is "+ ServiceDate);
        XSSFCell cell1=row.getCell(1);
        String MemberID=cell1.getStringCellValue();
        System.out.println("Member "+MemberID);
        XSSFCell cell2 = row.getCell(2);
        String FacilityCode=cell2.getStringCellValue();
        System.out.println("Facility Code "+FacilityCode);
        XSSFCell cell3=row.getCell(3);
        String ReleaseOfInfo=cell3.getStringCellValue();
        System.out.println("ROI " + ReleaseOfInfo);
        XSSFCell cell4 = row.getCell(4);
        String ContactName=cell4.getStringCellValue();
        System.out.println("Contact Name " + ContactName);
        XSSFCell cell5=row.getCell(5);
        String ContactPhoneAreaCode=cell5.getStringCellValue();
        System.out.println("ContactPhoneAreaCode " + ContactPhoneAreaCode);
        XSSFCell cell6 = row.getCell(6);
        String ContactPhonePrefix=cell6.getStringCellValue();
        System.out.println("ContactPhoePrefix " + ContactPhonePrefix);
        XSSFCell cell7=row.getCell(7);
        String ContactPhoneLine=cell7.getStringCellValue();
        System.out.println("ContactPhoneLine " + ContactPhoneLine);
        XSSFCell cell8=row.getCell(8);
        String ContactPhoneExt=cell8.getStringCellValue();
        System.out.println("ContactPhoneExt " + ContactPhoneExt);
        XSSFCell cell9 = row.getCell(9);
        String ServiceType=cell9.getStringCellValue();
        System.out.println("ServiceType " + ServiceType);
        XSSFCell cell10=row.getCell(10);
        String ServiceLevel=cell10.getStringCellValue();
        System.out.println("ServiceLevel " + ServiceLevel);
        XSSFCell cell11=row.getCell(11);
        String PRIS=cell11.getStringCellValue();
        System.out.println("PRIS " + PRIS);
        XSSFCell cell12 = row.getCell(12);
        String DiagnosisCode=cell12.getStringCellValue();
        System.out.println("Diagosis " + DiagnosisCode);
        XSSFCell cell13=row.getCell(13);
        String ProcedureCode=cell13.getStringCellValue();
        System.out.println("Procedure Code " + ProcedureCode);
        XSSFCell cell14=row.getCell(14);
        String unitsProcedure=cell14.getStringCellValue();
        System.out.println("Procedure Units " + unitsProcedure);


        System.setProperty("webdriver.chrome.driver", "C:\\Users\\V-DKumaravelu\\eclipse-workspace\\ECM Portal\\driver\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://10.32.52.79:8888/ECM/clientlogin.aspx");
        driver.findElement(By.id("txtUserId")).sendKeys("203861P");
        driver.findElement(By.id("txtPassword")).sendKeys("RETINA01");          
        driver.findElement(By.id("btnLogin")).click();
        driver.findElement(By.xpath("//*[@id=\'_ctl7_lbNewPA\']")).click();
        driver.findElement(By.id("outpatientServices2")).click();
        driver.findElement(By.id("serviceDate")).sendKeys(ServiceDate);
        driver.findElement(By.id("memberID")).sendKeys(MemberID);
        sc.main(driver);
        driver.findElement(By.id("nextLink1")).click();
        Select select = new Select(driver.findElement(By.id("facilityCode")));
        select .selectByVisibleText(FacilityCode);
        Select select1 = new Select(driver.findElement(By.id("releaseOfInfo")));
        select1 .selectByVisibleText(ReleaseOfInfo);
        driver.findElement(By.id("ContactName")).sendKeys(ContactName);
        driver.findElement(By.id("ContactPhoneAreaCode")).sendKeys(ContactPhoneAreaCode);
        driver.findElement(By.id("ContactPhonePrefix")).sendKeys(ContactPhonePrefix);
        driver.findElement(By.id("ContactPhoneLine")).sendKeys(ContactPhoneLine);
        driver.findElement(By.id("ContactPhoneExt")).sendKeys(ContactPhoneExt);
        sc.main(driver);
        driver.findElement(By.id("nextLink1")).click();
        Select select2 = new Select(driver.findElement(By.xpath("//*[@id='serviceType']")));
        select2 .selectByVisibleText(ServiceType);
        Select select3 = new Select(driver.findElement(By.xpath("//*[@id=\'levelOfService\']")));
        select3 .selectByVisibleText(ServiceLevel);
        driver.findElement(By.id("facilityPris")).sendKeys(PRIS);
        driver.findElement(By.id("diagnosisCode")).sendKeys(DiagnosisCode);
        driver.findElement(By.id("addDiagnosis")).click();
        driver.findElement(By.id("codeProcedure")).sendKeys(ProcedureCode);
        driver.findElement(By.id("unitsProcedure")).sendKeys(unitsProcedure);
        driver.findElement(By.id("addProcedure")).click();
        sc.main(driver);
        driver.findElement(By.id("nextLink2")).click();
        driver.findElement(By.id("btnSubmit")).click(); 
        sc.main(driver);
        Assert.assertNotNull(driver.findElement(By.xpath("//*[@id=\'Table1\']/tbody/tr[5]/td[6]")),"Failed");
driver.close();
}}

}