【问题标题】:Selenium JUnit script shows errors and TestNG does not show errorsSelenium JUnit 脚本显示错误,而 TestNG 不显示错误
【发布时间】:2014-08-21 13:09:20
【问题描述】:

我有一个运行 Selenium JUnit 的好脚本:

<pre>
package logowanieExcel;

import java.io.File;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.junit.rules.ErrorCollector;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;


public class logowanieExcel {
private WebDriver driver;
private StringBuffer verificationErrors = new StringBuffer();

@Rule
public ErrorCollector errCollector = new ErrorCollector();

@Test
public void testLogowanieExcel() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Workbook wBook = Workbook.getWorkbook(new File("U:\\import.xls"));
Sheet sheet = wBook.getSheet(0);

for(int i=0; i<sheet.getRows(); i++){  

    driver.get("http://url");
    assertEquals("text", driver.getTitle());
    driver.findElement(By.id("login")).sendKeys(sheet.getCell(0, i).getContents());
    driver.findElement(By.id("password")).sendKeys(sheet.getCell(1, i).getContents());
    driver.findElement(By.cssSelector("span.triangle")).click();

    try{
    assertEquals("Profile", driver.findElement(By.cssSelector("span.top.profile")).getText());;
    }catch(Throwable t){
        System.out.println("ERROR");
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("U:\\login"+     System.currentTimeMillis()+".jpg"));
        System.out.println("Make SCREENSHOT " + i);
        errCollector.addError(t);
    }
    System.out.println("user " + i);

    driver.findElement(By.cssSelector("span.triangle")).click();
}
driver.close();
driver.quit();
}



@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
  fail(verificationErrorString);

`

脚本正确抛出错误:

org.openqa.selenium.NoSuchElementException: 无法定位元素:{"method":"css selector","selector":"span.top.profile"}

转换为 TestNG 的相同脚本不再引发错误。为什么? 在 JUnit 中如何工作?有什么想法吗?

脚本 TNG:

package logExcel;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.Assert;
import org.testng.AssertJUnit;
import java.io.File;
import java.util.concurrent.TimeUnit;

import jxl.Sheet;
import jxl.Workbook;
import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.junit.rules.ErrorCollector;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;


public class testNG {
private WebDriver driver;
private StringBuffer verificationErrors = new StringBuffer();

@Rule
public ErrorCollector errCollector = new ErrorCollector();

@Test
public void testLogowanieExcel() throws Exception {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Workbook wBook = Workbook.getWorkbook(new File("U:\\import.xls"));
Sheet sheet = wBook.getSheet(0);

for(int i=0; i<sheet.getRows(); i++){  

    driver.get("http://url");
    AssertJUnit.assertEquals("text", driver.getTitle());
    driver.findElement(By.id("login")).sendKeys(sheet.getCell(0, i).getContents());
    driver.findElement(By.id("password")).sendKeys(sheet.getCell(1, i).getContents());
    driver.findElement(By.cssSelector("span.triangle")).click();

    try{
     AssertJUnit.assertEquals("Profile",driver.findElement(By.cssSelector("span.top.profile")).getText());;
    }catch(Throwable t){
        System.out.println("ERROR");
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshot, new File("U:\\login"+ System.currentTimeMillis()+".jpg"));
        System.out.println("Make SCREENSHOT " + i);
        errCollector.addError(t);
    }
    System.out.println("user " + i);

    driver.findElement(By.cssSelector("span.triangle")).click();

}
driver.close();
driver.quit();
}



@AfterMethod
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
  Assert.fail(verificationErrorString);
}
}
}

【问题讨论】:

    标签: java selenium junit testng


    【解决方案1】:

    TestNG 将@Before@After 和其他机制视为脚手架的一部分。脚手架不能失败。只有测试会失败。您正在脚手架内进行测试,这对于 TestNG 来说非常正确,不会报告为错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      相关资源
      最近更新 更多