如果文件在位置中不存在,则使TestNG通过测试失败

问题描述 投票:0回答:1

我有一个测试用例,可以将报告从我的网站下载到我的下载文件夹中。如果文件没有下载(即我的下载文件夹中不存在文件),我希望TestNG通过测试失败,但我无法使其失败。我是Java和TestNG的新手,我将在一周内休产假。请帮助一个女孩。这就是我到目前为止所得到的。谢谢!

package trailerreports;
import org.testng.annotations.Test;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;

public class TrailerSummaryReport {
@Test
public void Run() throws InterruptedException {

      System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");

        WebDriver driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.get("https://uat.mixtelematics.com/#/login");
        driver.findElement(By.name("userName")).sendKeys("[email protected]");       
        driver.findElement(By.name("password")).sendKeys("xxxxx");      
        driver.findElement(By.xpath("//*[@ng-class='buttonClass()']")).click();     
        driver.get("https://uat.mixtelematics.com/#/insight/reports/setup?orgId=3141559618424543932&path=%2FFM%2FTrailer%20Reports%2FTrailer%20Summary%20Report");                      driver.findElement(By.cssSelector(".first-item > div:nth-child(2) > div:nth-child(1) > div:nth-child(2)")).click();     
        driver.findElement(By.xpath("//BUTTON[@class='btn-wide btn-small btn-success btn ng-scope ng-binding'][text()='Next']")).click();       
        driver.findElement(By.cssSelector("a.btn-success:nth-child(1)")).click();
        driver.findElement(By.cssSelector("th.selection")).click();
        driver.findElement(By.xpath("//BUTTON[@class='btn ng-scope ng-binding btn-wide btn-success'][text()='Select']")).click();       
        Select dropdown = new Select(driver.findElement(By.cssSelector("select.span3")));
        dropdown.selectByVisibleText("Year to Date");
        driver.findElement(By.xpath("//BUTTON[@class='btn-wide btn-small btn-success btn ng-scope ng-binding'][text()='Next']")).click();
        Select dropdown5 = new Select(driver.findElement(By.cssSelector("div.form-inline:nth-child(2) > select:nth-child(2)")));
        dropdown5.selectByVisibleText("Download");
        Select dropdown6 = new Select(driver.findElement(By.cssSelector("span.report-parameter:nth-child(4) > span:nth-child(1) > div:nth-child(1) > div:nth-child(2) > select:nth-child(2)")));
        dropdown6.selectByVisibleText("PDF");
        driver.findElement(By.xpath("//BUTTON[@class='btn-wide btn-small btn-success btn ng-scope ng-binding'][text()='Run']")).click();      

  } 

  @Test
  public void Download() throws InterruptedException {

        File f = new File("C://Users//lisar//Downloads/Trailer Summary Report.pdf");
          if(f.exists()){
              System.out.println("File exists");

     }        
  }
}         

File doesn't existTestNG still passes Download test

selenium testng
1个回答
0
投票

文件对象将返回其有效路径与否。 f.exists()将返回true或false。由于该文件不存在于路径中,因此它将返回false,并且由于您尚未处理else部分,因此该文件也不会显示在控制台上。

如果要在读取文件时引发异常,则应使用FileInputStream

FileInputStream fis = new FileInputStream("path of file..");

如果路径中不存在文件,则将返回异常。

© www.soinside.com 2019 - 2024. All rights reserved.