Write the Selenium Java webdriver program and test the correspondence between the number and the Git address in the Inputgit.csv table
Package Selenium2;import Java.util.regex.pattern;import Java.util.concurrent.timeunit;import org.junit.*;import Org.junit.runner.runwith;import Org.junit.runners.parameterized;import Org.junit.runners.parameterized.parameters;import static Org.junit.assert.*;import Static Org.hamcrest.corematchers.*;import Org.openqa.selenium.*;import Org.openqa.selenium.firefox.firefoxdriver;import Org.openqa.selenium.support.ui.select;import Java.io.bufferedreader;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.filereader;import Java.io.ioexception;import Java.util.Arrays;import java.util.Collection; @RunWith (parameterized.class) public class Selenium2 {private Webdriver driver; Private String BaseUrl; Private Boolean Acceptnextalert = true; Private StringBuffer verificationerrors = new StringBuffer (); private String name; private String password; private string Github;public selenium2 (string name, string password, string github) {this.name = name; This.pAssword = password; This.github = GitHub; } @Parameters public static Collection data () {object[][] oa = new OBJECT[117][3]; File datafile = new file ("d:\\ software Test \\lab2\\inputgit.csv"); if (datafile.exists () && datafile.isfile ()) {try {FileReader fr = new Filerea Der (datafile); BufferedReader br = new BufferedReader (FR); String content = Br.readline (); int count = 0; String[] pieces = new string[3]; while (content = Br.readline ())!=null) {pieces = Content.split (","); Oa[count][0] = pieces[0]; OA[COUNT][1] = pieces[0].substring (4); OA[COUNT][2] = pieces[2]; Count = count + 1; } br.close (); Fr.close (); }catch (FileNotFoundException e) {System.out.println ("Cann ' t find file, errorInfo: "+ e.getmessage ()); }catch (IOException e) {e.printstacktrace (); }} else {System.out.println ("Can not find file!"); } return Arrays.aslist (OA); } @Before public void SetUp () throws Exception {System.setproperty ("Webdriver.gecko.driver", "d:\\ junior lower \ \ Software Test \\lab2\\g Eckodriver.exe "); Driver = new Firefoxdriver (); BASEURL = "http://121.193.130.195:8080"; Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); } @Test public void Test () throws Exception {Driver.get (baseUrl + "/"); Driver.findelement (by.id ("name")). Clear (); Driver.findelement (by.id ("name")). SendKeys (name); Driver.findelement (By.id ("pwd")). Clear (); Driver.findelement (By.id ("pwd")). SendKeys (password); Driver.findelement (By.id ("submit")). Click (); Assertequals (GitHub, Driver.findelement (By.xpath ("//tbody[@id = ' table-main ']/tr[3]/td[2]"). GetText ()); } @After public void TearDown () throws Exception {driver.quit (); String verificationerrorstring = verificationerrors.tostring (); if (! "". Equals (verificationerrorstring)) {fail (verificationerrorstring); }} Private Boolean Iselementpresent (by) {try {driver.findelement (by); return true; } catch (Nosuchelementexception e) {return false; }} private Boolean isalertpresent () {try {Driver.switchto (). alert (); return true; } catch (Noalertpresentexception e) {return false; }} private String Closealertandgetitstext () {try {alert alert = Driver.switchto (). alert (); String Alerttext = Alert.gettext (); if (Acceptnextalert) {alert.accept (); } else {Alert.dismiss (); } return alerttext; } finally {Acceptnextalert = true; } }}
Attention:
1. The imported jar package is:
2. The FF used is version 40
3. When the JUnit test succeeds, 117 data is automatically verified and displayed successfully
Software Testing Selenium Java Webdriver