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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…