Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

In Selenium using Java is it possible to write only one test method in testNG and form multiple tests from it?

I have one scenario where i would have only one test method in my testNG test class and i have to form multiple tests from it the reason i'm writting only one test method is that i don't know how many tests i would need, because that number depends on the number of URL's which i'm fetching from a excel sheet.

So basically i will have only one testNG test method which will fetch different URL from excel each time and execute the test and according to test result my listener will mark the test as pass or fail. Say for example i have 20 URL's in my excel sheet so can one testNG test method execute these 20 URL's one by one and give pass/fail results 20 times. Means at the end i should see 20 tests executed with there pass/fail results. How can i achieve this ?

question from:https://stackoverflow.com/questions/65932987/in-selenium-using-java-is-it-possible-to-write-only-one-test-method-in-testng-an

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Basically You would like to run your test with different data sets,

To do so, you may implement a @Factory annotated constructor and tie this constructor's @Factory annotation to a @DataProvider annotated data provider method

In addition you may use Apache POI to fetch the urls from the given excel file.

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.testng.Assert.assertNotNull;

public class SampleTest {

  private final String url;

  @Factory(dataProvider = "getUrls")
  public SampleTest(String url) {
    this.url = url;
  }

  @DataProvider(name = "getUrls")
  public static Iterator<Object[]> loadUrls() throws IOException {
    InputStream inputStream =
        SampleTest.class.getClassLoader().getResourceAsStream("urls-sheet.xlsx");

    try (Workbook workbook = new XSSFWorkbook(inputStream)) {
      Iterable<Row> iterable = () -> workbook.getSheetAt(0).iterator();
      Stream<Row> rows = StreamSupport.stream(iterable.spliterator(), false);

      return rows.map(row -> new Object[] {row.getCell(0).getStringCellValue()}).iterator();
    }
  }

  @Test
  public void testProvidedUrl() {
    assertNotNull(url, "url should have a value");
  }
}

Demo source code

See

TestNG @DataProvider – Test parameters example

How to run multiple test cases in testng with different set of test data from excel file?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...