Selenium Webdriver: Data driven using TESTNG and CSV files

Source: Internet
Author: User
Tags testng

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.