I. Experimental requirements
1. Install Seleniumide plug-in
2. Learn to use Seleniumide to record scripts and export scripts
3, Access http://www.ncfxy.com Use the number of login system (account name is the school number, the password for the school number after 6), enter the system can see the user's mailbox.
4, write Selenium Java webdriver program, test info.csv form the number and the correspondence between the mailbox is correct.
Submit the test code on GitHub
Two Experiment Preparation
Operating system: Windows 8.1 64x
IDE:Eclipse-kepler 32x
Three Experimental steps
1. Installing the Seleniumide Plugin
Find add components in the Firefox menu-search selenium--See all results-search seleniumide, find seleniumide and download the installation.
After reboot, you can find the corresponding seleniumide icon in the upper right corner of Firefox browser, such as:
2. Recording scripts and exporting scripts using Seleniumide
2.1 Recording Scripts
Open the browser and click on the Seleniumide plugin so that the red dot represents being recorded.
Then use the browser to operate, the whole process selenium in the recording, after the end of the operation, click the red dot again, end the recording, the results are as follows:
Once the recording is complete, we can click Play and we can see that seleniumide repeats the action we just made.
2.2 Export Scripts
Menu Bar--File--export Test case as ... --and then we can see that there are a lot of export options,
Here we choose Java/junit 4/webdriver to change the exported file to a. java file, and then we can open and edit the file with an IDE such as Eclipse, as follows:
3. Visit http://www.ncfxy.com, write selenium Java webdriver program, test the correct relationship between the number and the mailbox in the Info.csv table.
3.1 Establishment of the project
There is no difference between building a project and a common project, but it is necessary to import some related jar packages, such as
3.2 Visit the target site
Here I use the Firefox browser, so the corresponding Dirver is Firefox,
First we need to set up the location of the Firefox system, and then simply visit the target site.
1 System.setproperty ("Webdriver.firefox.bin", "C:\\Program Files (x86) \\Mozilla Firefox\\firefox.exe"); 2 New firefoxdriver (); 3 4 Driver.get ("http://www.ncfxy.com/");
3.3 Login and access to information
Here you need to get elements element on the page to manipulate the page.
For example, for the landing screen of the page, we right-click, select View page elements, you can find the following information:
So we find the ID of the target we need to get, so we can find them in the program and set their values, and manipulate the form submission,
1 Webelement Usernameele = driver.findelement (by.id ("name")); 2 Webelement Passwordele = driver.findelement (by.id ("pwd")); 3 Usernameele.sendkeys (user); 4 Passwordele.sendkeys (pwd); 5 Usernameele.submit ();
The last line of submit () causes the form to be submitted.
On the next page, we want to get the mailbox information, but the structure of the mailbox information in the page element is as follows:
And there are attributes such as ID or name that we can get directly, here we need to use XPath to get the target element.
In simple terms, XPath locates the target element based on the structure of the element.
For example, this mailbox [email protected] content, we can use the following XPath to get,
Webelement Emailele = driver.findelement (By.xpath ("//tbody[@id = ' table-main ']/tr[1]/td[2]"); = Emailele.gettext ();
In tbody, the parent element of the target element is found with the ID, and then the label and array are used to locate it.
3.4 reading a. csv file
. csv read there are many ways, here I chose a relatively concise one, simple to open the file, simple to read,
1 Try { 2BufferedReader reader =NewBufferedReader (NewFileReader ("Info.csv"));//Replace with your file name3 4String line =NULL; 5 while((Line=reader.readline ())! =NULL){ 6String item[] = Line.split (",");//csv format file is a comma delimiter file, which is separated by commas7 //System.out.println (line);8String user = Item[0]; 9String address = item[1];//This is the data you need.Ten One } A - returnarrays.aslist (o); -}Catch(Exception e) { the e.printstacktrace (); - return NULL; -}
Reader.readline () Gets the contents of each row because the data in the. csv is separated by a ', ', comma-delimited, and then fetched from the array of results.
3.5 Parametric testing
We're going to test every piece of data in a. csv file, so how many data are there, how many test cases are there, and I use parameterization here, the complete code is as follows:
1 Packagecn.tju.scs.thy;2 3 Import Staticorg.junit.assert.*;4 5 ImportJava.io.BufferedReader;6 ImportJava.io.FileReader;7 Importjava.util.ArrayList;8 Importjava.util.Arrays;9 Importjava.util.Collection;Ten Importjava.util.List; One A ImportOrg.junit.Before; - Importorg.junit.Test; - ImportOrg.junit.runner.RunWith; the Importorg.junit.runners.Parameterized; - Importorg.junit.runners.Parameterized.Parameters; - -@RunWith (parameterized.class) + Public classSeleniumTestTest2 { - + PrivateString input1; A PrivateString Input2; at PrivateString expected; - - Privateseleniumtest sel; - - PublicSeleniumTestTest2 (String input1,string input2,string expected) { - This. INPUT1 =input1; in This. Input2 =Input2; - This. Expected =expected; to } + - @Before the Public voidsetUp () { *SEL =Newseleniumtest (); $ }Panax Notoginseng - @Parameters the Public StaticCollection<object[]>GetData () { + A Try { theBufferedReader reader =NewBufferedReader (NewFileReader ("Info.csv"));//Replace with your file name + - $object[][] o =Newobject[109][]; $ object[] oo; - intindex = 0; -String line =NULL; the while((Line=reader.readline ())! =NULL&& index<109){ -String item[] = Line.split (",");//csv format file is a comma delimiter file, which is separated by commasWuyi //System.out.println (line); theString user = Item[0]; -String pwd = user.substring (4); Wu -String address = item[1];//This is the data you need. About $OO =Newobject[]{user,pwd,address}; -O[index] =Oo; - -index++; A } + the returnarrays.aslist (o); -}Catch(Exception e) { $ e.printstacktrace (); the return NULL; the } the the } - @Test in Public voidTest () { theAssertequals ( This. expected,sel.getemailaddress ( This. INPUT1, This. Input2)); the } About the}
So, we run our test code and we see that the program performs the tests we've set for each piece of data.
Tju_scs_ Software Test _lab2_selenium