如果该位置不存在文件,则让 TestNG 无法通过测试
Getting TestNG to fail a test if file does not exist in location
我有一个测试用例,它从我的网站下载一份报告到我的下载文件夹。如果文件未下载(即它不存在于我的下载文件夹中),我希望 TestNG 无法通过测试,但我无法让它失败。我是 Java AND 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("xx@mixtelematics.com");
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");
}
}
}
文件对象 return 无论其路径是否有效。 f.exists() 将 return 真或假。由于该文件不在路径中,因此 returning false 并且由于您没有处理其他部分,因此它不会显示在控制台上。
如果你想在读取文件时抛出异常,那么你应该使用 FileInputStream
FileInputStream fis = new FileInputStream("path of file..");
如果文件不在路径中,这将 return 异常。
我有一个测试用例,它从我的网站下载一份报告到我的下载文件夹。如果文件未下载(即它不存在于我的下载文件夹中),我希望 TestNG 无法通过测试,但我无法让它失败。我是 Java AND 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("xx@mixtelematics.com");
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");
}
}
}
文件对象 return 无论其路径是否有效。 f.exists() 将 return 真或假。由于该文件不在路径中,因此 returning false 并且由于您没有处理其他部分,因此它不会显示在控制台上。
如果你想在读取文件时抛出异常,那么你应该使用 FileInputStream
FileInputStream fis = new FileInputStream("path of file..");
如果文件不在路径中,这将 return 异常。