This time we mainly use Selenium for automated testing , First we need to download Selenium-java dependencies.
If you are using MAVEN to manage your project, include the following dependencies in the. Pom File:
1 <dependency>2 <groupId>org.seleniumhq.selenium</groupId>3 < Artifactid>selenium-java</artifactid>4 <version>2.53.0</version>5 </ Dependency>
You can also download the jar package manually, you can download the above-mentioned website, and don't repeat it here.
Here's what we're experimenting with:
First, the purpose of the experiment
- Installing the Seleniumide Plugin
- Learn to use Seleniumide to record scripts and export scripts
- Access http://www.ncfxy.com Use the number login system (account name is the school number, the password is the number 6), after entering the system can see the user's mailbox.
- Write the Selenium Java webdriver program to test the correct relationship between the number and the mailbox in the Info.csv table.
Second, Java Webdriver code:
Package Lesson1; Import static org.junit.assert.*; Import Java.util.concurrent.TimeUnit; Import Org.junit.*;import Org.openqa.selenium.by;import Org.openqa.selenium.webdriver;import Org.openqa.selenium.webelement;import Org.openqa.selenium.firefox.firefoxdriver;import Org.openqa.selenium.support.ui.expectedcondition;import org.openqa.selenium.support.ui.WebDriverWait; Import Java.io.File; Import Java.io.FileInputStream; Import Java.io.ioexception;import Java.io.InputStream; Import JXL. Cell; Import JXL. Celltype; Import JXL. Sheet; Import JXL. Workbook; Import Jxl.write.Label; public class Exampleforfirefox {private Webdriver driver; Private String BaseUrl; @Before public void SetUp () throws Exception {driver = new firefoxdriver (); BASEURL = "http://www.ncfxy.com/"; Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); } @Test public void Test1 () throws Exception {Jxl. Workbook READWB= NULL; Build the Workbook object, read-only Workbook object//directly from the local file created workbook InputStream instream = new FileInputStream ("D:/info.xls "); READWB = Workbook.getworkbook (instream); System.out.println ("JJJJJJJ"); Sheet subscript is starting from 0//Get the first Sheet table Sheet readsheet = readwb.getsheet (0); Gets the total number of columns contained in the sheet table int rscolumns = Readsheet.getcolumns (); Gets the total number of rows contained in the sheet table int rsrows = Readsheet.getrows (); Gets the object reference for the specified cell for (int i = 0; I <rsRows; i++) {driver.get (BASEURL); Cell cell = Readsheet.getcell (0, I); String username = cell.getcontents (); String Password = username.substring (4, 10); Find the DOM webelement element = driver.findelement (by.id ("name") of input via ID; Webelement element1 = driver.findelement (by.id ("pwd")); System.out.println (Element.getsize ()); Enter keywords Element.sendkeys (username); Element1.sendkeys (password); Submit the Form element.submit () where input is located; Get the resulting mailbox webelement Element2 = driver.findelement (By.xpath ("//td[2]")); String mailbyweb = Element2.gettext (); String mailbyinfo = Readsheet.getcell (1,i). getcontents (); Assertequals (Mailbyinfo,mailbyweb); System.out.println (Element2.gettext ()); System.out.println (); }/*//Use the Excel workbook you have created to create a new writable Excel workbook Jxl.write.WritableWorkbook WWB = Workbook.createworkbook ( New File ("D:/info.xls"), READWB); Read the first sheet jxl.write.WritableSheet ws = Wwb.getsheet (0); Get the first Cell object Jxl.write.WritableCell WC = Ws.getwritablecell (0, 0); Determine the type of cell and make the appropriate conversion if (wc.gettype () = = Celltype.label) {LABEL L = (Label) WC; L.setstring ("new name"); }//write to Excel Object Wwb.write (); Wwb.close (); *///close Read inflow readwb.close (); }}
Third, experience
Through this experiment we learned how to use the Selenium IDE to implement simple automated tests, including the recording, writing, and running of tests, giving us a deeper understanding of the testing methodology and the importance of testing.
[Software Testing Fundamentals 2] Automated testing based on selenium