1. How to solve the problem of CSV file garbled
The CSV file is opened with UltraEdit-32 software, and in the bottom status bar there is an identity that changes the encoding type to Utf-8
2. Combining CSV data-driven code
CSV file
Package cn.gloryroad;
Import Org.testng.annotations.Test;
Import Org.testng.annotations.BeforeMethod;
Import Org.testng.annotations.AfterMethod;
Import Org.testng.annotations.DataProvider;
Import Java.io.BufferedReader;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.concurrent.TimeUnit;
Import Org.testng.Assert;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebDriver.Navigation;
Import Org.openqa.selenium.ie.InternetExplorerDriver;
Import org.openqa.selenium.support.ui.ExpectedCondition;
Import org.openqa.selenium.support.ui.WebDriverWait;
@Test
public class Testdatadrivenbycsvfile {
public Webdriver driver;
String baseUrl = "http://www.baidu.com/";
@DataProvider (name = "TestData")
public static object[][] Words () throws IOException {
Return Gettestdata ("D:\\testdata.csv");
}
@Test (dataprovider= "TestData")
public void Testsearch (string searchWord1, String searchWord2,
String SearchResult) {
Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds);
String baseUrl = "http://www.baidu.com/";
Navigation Navigation = Driver.navigate ();
Navigation.to ("http://www.baidu.com");
Driver.findelement (By.id ("kw"))
. SendKeys (SearchWord1 + "" + SearchWord2);
Driver.findelement (By.id ("su")). Click ();
(New webdriverwait (Driver)). Until (new expectedcondition<boolean> () {
Public Boolean apply (Webdriver D) {
Return D.findelement (By.id ("foot")). GetText ()
. Contains ("?????????");
}
});
Assert.asserttrue (Driver.getpagesource (). Contains (SearchResult));
}
@BeforeMethod
public void Beforemethod () {
System.setproperty ("Webdriver.ie.driver", "D:\\iedriverserver.exe");
Driver = new Internetexplorerdriver ();
}
@AfterMethod
public void Aftermethod () {
Driver.quit ();
}
public static Object [] Gettestdata (String fileName) throws ioexception{
List<object[]> records = new ArrayList <Object[]> ();
String record;
BufferedReader file = new BufferedReader (new InputStreamReader (New FileInputStream (FileName), "UTF-8"));
File.readline ();
while ((Record=file.readline ())!=null) {
String fields[]=record.split (",");
Records.add (fields);
}
File.close ();
Object[][] results = new object[records.size () [];
for (int i=0;i<records.size (); i++) {
Results[i]=records.get (i);
System.out.println (Results[i]);
}
return results;
}
}
Selenium Webdriver: Data driven using TESTNG and CSV files